source: src/pitch/pitchyinfft.c @ 74516f7

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 74516f7 was 407bba9, checked in by Paul Brossier <piem@piem.org>, 15 years ago

src/mathutils.c: use a string for window type, making enum private

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[650e39b]1/*
2   Copyright (C) 2003 Paul Brossier
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19#include "aubio_priv.h"
[6c7d49b]20#include "fvec.h"
21#include "cvec.h"
[650e39b]22#include "mathutils.h"
[32d6958]23#include "spectral/fft.h"
[2d8cffa]24#include "pitch/pitchyinfft.h"
[650e39b]25
[91879d9]26/** pitch yinfft structure */
[650e39b]27struct _aubio_pitchyinfft_t {
[91879d9]28  fvec_t * win;       /**< temporal weighting window */
29  fvec_t * winput;    /**< windowed spectrum */
30  cvec_t * res;       /**< complex vector to compute square difference function */
31  fvec_t * sqrmag;    /**< square difference function */
32  fvec_t * weight;    /**< spectral weighting window (psychoacoustic model) */
33  cvec_t * fftout;    /**< Fourier transform output */
[679441e]34  aubio_fft_t * fft;  /**< fft object to compute square difference function */
[91879d9]35  fvec_t * yinfft;    /**< Yin function */
[22d33e2]36  smpl_t tol;         /**< Yin tolerance */
[650e39b]37};
38
39static const smpl_t freqs[] = {0., 20., 25., 31.5, 40., 50., 63., 80., 100.,
[10cf306]40  125., 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250.,
41  1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000.,
42  12500., 15000., 20000.,  25100};
[650e39b]43
44static const smpl_t weight[] = {-75.8, -70.1, -60.8, -52.1, -44.2, -37.5,
[10cf306]45  -31.3, -25.6, -20.9, -16.5, -12.6, -9.6, -7.0, -4.7, -3.0, -1.8, -0.8,
46  -0.2, -0.0, 0.5, 1.6, 3.2, 5.4, 7.8, 8.1, 5.3, -2.4, -11.1, -12.8,
47  -12.2, -7.4, -17.8, -17.8, -17.8};
[650e39b]48
49aubio_pitchyinfft_t * new_aubio_pitchyinfft (uint_t bufsize)
50{
51  aubio_pitchyinfft_t * p = AUBIO_NEW(aubio_pitchyinfft_t);
[10cf306]52  p->winput = new_fvec(bufsize,1);
53  p->fft    = new_aubio_fft(bufsize, 1);
54  p->fftout = new_cvec(bufsize,1);
55  p->sqrmag = new_fvec(bufsize,1);
56  p->res    = new_cvec(bufsize,1);
57  p->yinfft = new_fvec(bufsize/2+1,1);
[22d33e2]58  p->tol    = 0.85;
[407bba9]59  p->win    = new_aubio_window("hanningz", bufsize);
[650e39b]60  p->weight      = new_fvec(bufsize/2+1,1);
61  {
[10cf306]62    uint_t i = 0, j = 1;
63    smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0;
64    for (i=0; i<p->weight->length; i++) {
65      freq = (smpl_t)i/(smpl_t)bufsize*(smpl_t)44100.;
66      while (freq > freqs[j]) {
67        j +=1;
68            }
69      a0 = weight[j-1];
70      f0 = freqs[j-1];
71            a1 = weight[j];
72      f1 = freqs[j];
73      if (f0 == f1) { // just in case
74        p->weight->data[0][i] = a0;
75      } else if (f0 == 0) { // y = ax+b
76        p->weight->data[0][i] = (a1-a0)/f1*freq + a0;
77      } else {
78        p->weight->data[0][i] = (a1-a0)/(f1-f0)*freq +
79          (a0 - (a1 - a0)/(f1/f0 - 1.));
80      }
81      while (freq > freqs[j]) {
82        j +=1;
83      }
84      //AUBIO_DBG("%f\n",p->weight->data[0][i]);
85      p->weight->data[0][i] = DB2LIN(p->weight->data[0][i]);
86      //p->weight->data[0][i] = SQRT(DB2LIN(p->weight->data[0][i]));
87    }
[650e39b]88  }
89  return p;
90}
91
[22d33e2]92void aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output) {
93  uint_t i, tau, l;
[650e39b]94  uint_t halfperiod;
[22d33e2]95  smpl_t tmp, sum;
[650e39b]96  cvec_t * res = (cvec_t *)p->res;
97  fvec_t * yin = (fvec_t *)p->yinfft;
[22d33e2]98  for (i=0; i < input->channels; i++){
99  l = 0; tmp = 0.; sum = 0.;
[650e39b]100  for (l=0; l < input->length; l++){
[22d33e2]101    p->winput->data[0][l] = p->win->data[0][l] * input->data[i][l];
[650e39b]102  }
[8b2dc90]103  aubio_fft_do(p->fft,p->winput,p->fftout);
[650e39b]104  for (l=0; l < p->fftout->length; l++){
[10cf306]105    p->sqrmag->data[0][l] = SQR(p->fftout->norm[0][l]);
106    p->sqrmag->data[0][l] *= p->weight->data[0][l];
[283d93f]107  }
108  for (l=1; l < p->fftout->length; l++){
[10cf306]109    p->sqrmag->data[0][(p->fftout->length-1)*2-l] =
110     SQR(p->fftout->norm[0][l]);
111    p->sqrmag->data[0][(p->fftout->length-1)*2-l] *=
112     p->weight->data[0][l];
[650e39b]113  }
114  for (l=0; l < p->sqrmag->length/2+1; l++) {
[10cf306]115    sum += p->sqrmag->data[0][l];
[650e39b]116  }
117  sum *= 2.;
[8b2dc90]118  aubio_fft_do(p->fft,p->sqrmag,res);
[10cf306]119  yin->data[0][0] = 1.;
[650e39b]120  for (tau=1; tau < yin->length; tau++) {
[10cf306]121    yin->data[0][tau] = sum -
122      res->norm[0][tau]*COS(res->phas[0][tau]);
123    tmp += yin->data[0][tau];
124    yin->data[0][tau] *= tau/tmp;
[650e39b]125  }
[2f64b0e]126  tau = fvec_min_elem(yin);
[22d33e2]127  if (yin->data[0][tau] < p->tol) {
[10cf306]128    /* no interpolation */
129    //return tau;
130    /* 3 point quadratic interpolation */
[5c4ec3c]131    //return fvec_quadint_min(yin,tau,1);
[10cf306]132    /* additional check for (unlikely) octave doubling in higher frequencies */
133    if (tau>35) {
[22d33e2]134      output->data[i][0] = fvec_quadint(yin,tau,1);
[10cf306]135    } else {
136      /* should compare the minimum value of each interpolated peaks */
137      halfperiod = FLOOR(tau/2+.5);
[22d33e2]138      if (yin->data[0][halfperiod] < p->tol)
139        output->data[i][0] = fvec_quadint(yin,halfperiod,1);
[10cf306]140      else
[22d33e2]141        output->data[i][0] = fvec_quadint(yin,tau,1);
[10cf306]142    }
[22d33e2]143  } else {
144    output->data[i][0] = 0.;
145  }
146  }
[650e39b]147}
148
149void del_aubio_pitchyinfft(aubio_pitchyinfft_t *p){
[10cf306]150  del_fvec(p->win);
151  del_aubio_fft(p->fft);
152  del_fvec(p->yinfft);
153  del_fvec(p->sqrmag);
154  del_cvec(p->res);
155  del_cvec(p->fftout);
156  del_fvec(p->winput);
157  del_fvec(p->weight);
158  AUBIO_FREE(p);
[650e39b]159}
[22d33e2]160
161uint_t aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol) {
162  p->tol = tol;
163  return 0;
164}
165
166smpl_t aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p) {
167  return p->tol;
168}
Note: See TracBrowser for help on using the repository browser.