source: src/pitch/pitchyinfft.c @ 5d16185

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 5d16185 was a5f4b7d, checked in by Paul Brossier <piem@piem.org>, 12 years ago

src/pitch/pitchyinfft.c: simplify, add some comment, update copyright

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