source: src/pitchyinfft.c @ b965fb1

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since b965fb1 was 650e39b, checked in by Paul Brossier <piem@altern.org>, 19 years ago

add yinfft method, make it default for pd plugin
add yinfft method, make it default for pd plugin

  • Property mode set to 100644
File size: 5.1 KB
Line 
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/* This algorithm was developped by A. de Cheveigne and H. Kawahara and
20 * published in:
21 *
22 * de Cheveigné, A., Kawahara, H. (2002) "YIN, a fundamental frequency
23 * estimator for speech and music", J. Acoust. Soc. Am. 111, 1917-1930. 
24 *
25 * see http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html
26 *
27 * This implementation is using an FFT to compute the square difference
28 * function, which allows spectral weighting
29 *
30 */
31
32#include "aubio_priv.h"
33#include "sample.h"
34#include "mathutils.h"
35#include "fft.h"
36#include "pitchyinfft.h"
37
38struct _aubio_pitchyinfft_t {
39        uint_t bufsize;
40        uint_t rate;
41        fvec_t * win;
42        fvec_t * winput;
43        cvec_t * res;
44        fvec_t * sqrmag;
45        fvec_t * weight;
46        cvec_t * fftout;
47        aubio_mfft_t * fft;
48        fvec_t * yinfft;
49};
50
51static const smpl_t freqs[] = {0., 20., 25., 31.5, 40., 50., 63., 80., 100.,
52        125., 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250.,
53        1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000.,
54        12500., 15000., 20000.,  25100};
55
56static const smpl_t weight[] = {-75.8, -70.1, -60.8, -52.1, -44.2, -37.5,
57        -31.3, -25.6, -20.9, -16.5, -12.6, -9.6, -7.0, -4.7, -3.0, -1.8, -0.8,
58        -0.2, -0.0, 0.5, 1.6, 3.2, 5.4, 7.8, 8.1, 5.3, -2.4, -11.1, -12.8,
59        -12.2, -7.4, -17.8, -17.8, -17.8};
60
61aubio_pitchyinfft_t * new_aubio_pitchyinfft (uint_t bufsize)
62{
63  aubio_pitchyinfft_t * p = AUBIO_NEW(aubio_pitchyinfft_t);
64  p->bufsize      = bufsize;
65  p->winput       = new_fvec(bufsize,1);
66  p->fft          = new_aubio_mfft(bufsize, 1);
67  p->fftout       = new_cvec(bufsize,1);
68  p->sqrmag       = new_fvec(bufsize,1);
69  p->res          = new_cvec(bufsize,1);
70  p->yinfft       = new_fvec(bufsize/2+1,1);
71  p->win          = new_fvec(bufsize,1);
72  aubio_window(p->win->data[0], bufsize, aubio_win_hanningz);
73  p->weight      = new_fvec(bufsize/2+1,1);
74  {
75          uint_t i = 0, j = 1;
76          smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0;
77          for (i=0; i<p->weight->length; i++) {
78                  freq = (smpl_t)i/(smpl_t)bufsize*(smpl_t)44100.;
79                  while (freq > freqs[j]) {
80                          j +=1;
81                  }
82                  a0 = weight[j-1];
83                  f0 = freqs[j-1];
84                  a1 = weight[j];
85                  f1 = freqs[j];
86                  if (f0 == f1) { // just in case
87                          p->weight->data[0][i] = a0;
88                  } else if (f0 == 0) { // y = ax+b
89                          p->weight->data[0][i] = (a1-a0)/f1*freq + a0;
90                  } else {
91                          p->weight->data[0][i] = (a1-a0)/(f1-f0)*freq + 
92                                  (a0 - (a1 - a0)/(f1/f0 - 1.));
93                  }
94                  while (freq > freqs[j]) {
95                          j +=1;
96                  }
97                  //AUBIO_DBG("%f\n",p->weight->data[0][i]);
98                  p->weight->data[0][i] = DB2LIN(p->weight->data[0][i]);
99                  //p->weight->data[0][i] = SQRT(DB2LIN(p->weight->data[0][i]));
100          }
101  }
102  return p;
103}
104
105smpl_t aubio_pitchyinfft_detect(aubio_pitchyinfft_t * p, fvec_t * input, smpl_t tol) {
106  uint_t tau, l = 0;
107  uint_t halfperiod;
108  smpl_t tmp = 0, sum = 0;
109  cvec_t * res = (cvec_t *)p->res;
110  fvec_t * yin = (fvec_t *)p->yinfft;
111  for (l=0; l < input->length; l++){
112          p->winput->data[0][l] = p->win->data[0][l] * input->data[0][l];
113  }
114  aubio_mfft_do(p->fft,p->winput,p->fftout);
115  for (l=0; l < p->fftout->length; l++){
116          p->sqrmag->data[0][l] = SQR(p->fftout->norm[0][l]);
117          p->sqrmag->data[0][(p->fftout->length-1)*2-l] = 
118                SQR(p->fftout->norm[0][l]);
119          p->sqrmag->data[0][l] *= p->weight->data[0][l]; 
120          p->sqrmag->data[0][(p->fftout->length-1)*2-l] *=
121                 p->weight->data[0][l];
122  }
123  for (l=0; l < p->sqrmag->length/2+1; l++) {
124          sum += p->sqrmag->data[0][l];
125  }
126  sum *= 2.;
127  aubio_mfft_do(p->fft,p->sqrmag,res);
128  yin->data[0][0] = 1.; 
129  for (tau=1; tau < yin->length; tau++) {
130          yin->data[0][tau] = sum -
131                  res->norm[0][tau+1]*COS(res->phas[0][tau+1]); 
132          tmp += yin->data[0][tau];
133          yin->data[0][tau] *= tau/tmp;
134  }
135  tau = vec_min_elem(yin); 
136  if (yin->data[0][tau] < tol) {
137          /* no interpolation */
138          //return tau+2;
139          /* 3 point quadratic interpolation */
140          //return vec_quadint_min(yin,tau,1);
141          /* additional check nlikely octave doubling in higher frequencies */
142          if (tau>35) {
143                  return vec_quadint_min(yin,tau,1)+1;
144          } else {
145                  /* should compare the minimum value of each interpolated peaks */
146                  halfperiod = FLOOR(tau/2+.5);
147                  if (yin->data[0][halfperiod] < tol)
148                          return vec_quadint_min(yin,halfperiod,1)+1;
149                  else
150                          return vec_quadint_min(yin,tau,1)+1;
151          }
152  } else
153          return 0;
154}
155
156void del_aubio_pitchyinfft(aubio_pitchyinfft_t *p){
157        del_fvec(p->win);
158        del_aubio_mfft(p->fft);
159        del_fvec(p->yinfft);
160        del_fvec(p->sqrmag);
161        del_cvec(p->res);
162        del_cvec(p->fftout);
163}
Note: See TracBrowser for help on using the repository browser.