Ticket #7: pitchyinfft.c

File pitchyinfft.c, 5.1 KB (added by ciacciax@yahoo.com, 16 years ago)

improved version of pitchyinfft without polar coordinates

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#include "aubio_priv.h"
20#include "sample.h"
21#include "mathutils.h"
22#include "fft.h"
23#include "pitchyinfft.h"
24
25/** pitch yinfft structure */
26struct _aubio_pitchyinfft_t {
27  uint_t bufsize;     /**< buffer size */
28  fvec_t * win;       /**< temporal weighting window */
29  fvec_t * winput;    /**< windowed spectrum */
30  fvec_t * sqrmag;    /**< square difference function */
31  fvec_t * weight;    /**< spectral weighting window (psychoacoustic model) */
32  fft_data_t *spec;       /**< Fourier transform output */
33  aubio_fft_t * fft;  /**< fft object to compute square difference function */
34  fvec_t * yinfft;    /**< Yin function */
35};
36
37static const smpl_t freqs[] = {0., 20., 25., 31.5, 40., 50., 63., 80., 100.,
38        125., 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250.,
39        1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000.,
40        12500., 15000., 20000.,  25100};
41
42static const smpl_t weight[] = {-75.8, -70.1, -60.8, -52.1, -44.2, -37.5,
43        -31.3, -25.6, -20.9, -16.5, -12.6, -9.6, -7.0, -4.7, -3.0, -1.8, -0.8,
44        -0.2, -0.0, 0.5, 1.6, 3.2, 5.4, 7.8, 8.1, 5.3, -2.4, -11.1, -12.8,
45        -12.2, -7.4, -17.8, -17.8, -17.8};
46
47aubio_pitchyinfft_t * new_aubio_pitchyinfft (uint_t bufsize)
48{
49  aubio_pitchyinfft_t * p = AUBIO_NEW(aubio_pitchyinfft_t);
50  p->bufsize      = bufsize;
51  p->winput       = new_fvec(bufsize,1);
52  p->fft          = new_aubio_fft(bufsize);
53  p->spec         = (fft_data_t*)malloc(bufsize*sizeof(fft_data_t));
54  p->sqrmag       = new_fvec(bufsize,1);
55  p->yinfft       = new_fvec(bufsize/2+1,1);
56  p->win          = new_fvec(bufsize,1);
57  aubio_window(p->win->data[0], bufsize, aubio_win_hanningz);
58  p->weight      = new_fvec(bufsize/2+1,1);
59  {
60          uint_t i = 0, j = 1;
61          smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0;
62          for (i=0; i<p->weight->length; i++) {
63                  freq = (smpl_t)i/(smpl_t)bufsize*(smpl_t)44100.;
64                  while (freq > freqs[j]) {
65                          j +=1;
66                  }
67                  a0 = weight[j-1];
68                  f0 = freqs[j-1];
69                  a1 = weight[j];
70                  f1 = freqs[j];
71                  if (f0 == f1) { // just in case
72                          p->weight->data[0][i] = a0;
73                  } else if (f0 == 0) { // y = ax+b
74                          p->weight->data[0][i] = (a1-a0)/f1*freq + a0;
75                  } else {
76                          p->weight->data[0][i] = (a1-a0)/(f1-f0)*freq + 
77                                  (a0 - (a1 - a0)/(f1/f0 - 1.));
78                  }
79                  while (freq > freqs[j]) {
80                          j +=1;
81                  }
82                  //AUBIO_DBG("%f\n",p->weight->data[0][i]);
83                  p->weight->data[0][i] = DB2LIN(p->weight->data[0][i]);
84                  //p->weight->data[0][i] = SQRT(DB2LIN(p->weight->data[0][i]));
85          }
86  }
87  return p;
88}
89
90smpl_t aubio_pitchyinfft_detect(aubio_pitchyinfft_t * p, fvec_t * input, smpl_t tol) {
91  uint_t tau, l = 0;
92  uint_t halfperiod;
93  smpl_t tmp = 0, sum = 0;
94  fvec_t * yin = (fvec_t *)p->yinfft;
95  for (l=0; l < input->length; l++){
96          p->winput->data[0][l] = p->win->data[0][l] * input->data[0][l];
97  }
98  aubio_fft_do(p->fft,p->winput->data[0], p->spec, p->bufsize);
99  // index at 0 and N/2 are purely real
100  p->sqrmag->data[0][0] = (smpl_t)(p->spec[0] * p->spec[0]);
101  p->sqrmag->data[0][p->bufsize/2] = (smpl_t)(p->spec[p->bufsize/2] * p->spec[p->bufsize/2]);
102  for (l=0; l < p->bufsize/2; l++){
103      smpl_t re = (smpl_t)p->spec[l];
104      smpl_t im = (smpl_t)p->spec[p->bufsize-l];
105      smpl_t mag = (re*re + im*im) * p->weight->data[0][l];
106          p->sqrmag->data[0][l] = mag;
107          p->sqrmag->data[0][p->bufsize-l] = mag;
108  }
109
110  for (l=0; l < p->sqrmag->length/2+1; l++) {
111          sum += p->sqrmag->data[0][l];
112  }
113  sum *= 2.;
114  aubio_fft_do(p->fft, p->sqrmag->data[0], p->spec, p->bufsize);
115  yin->data[0][0] = 1.; 
116  for (tau=1; tau < yin->length; tau++) {
117          yin->data[0][tau] = sum - (smpl_t)p->spec[tau];
118          tmp += yin->data[0][tau];
119          yin->data[0][tau] *= tau/tmp;
120  }
121  tau = vec_min_elem(yin); 
122  if (yin->data[0][tau] < tol) {
123          /* no interpolation */
124          //return tau;
125          /* 3 point quadratic interpolation */
126          //return vec_quadint_min(yin,tau,1);
127          /* additional check for (unlikely) octave doubling in higher frequencies */
128          if (tau>35) {
129                  return vec_quadint_min(yin,tau,1);
130          } else {
131                  /* should compare the minimum value of each interpolated peaks */
132                  halfperiod = FLOOR(tau/2+.5);
133                  if (yin->data[0][halfperiod] < tol)
134                          return vec_quadint_min(yin,halfperiod,1);
135                  else
136                          return vec_quadint_min(yin,tau,1);
137          }
138  } else
139          return 0;
140}
141
142void del_aubio_pitchyinfft(aubio_pitchyinfft_t *p){
143        del_fvec(p->win);
144        del_aubio_fft(p->fft);
145        del_fvec(p->yinfft);
146        del_fvec(p->sqrmag);
147        free(p->spec);
148        del_fvec(p->winput);
149        del_fvec(p->weight);
150        AUBIO_FREE(p);
151}