2 Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.h"
24 #include "mathutils.h"
25 #include "spectral/fft.h"
26 #include "pitch/pitchyinfft.h"
28 /** pitch yinfft structure */
29 struct _aubio_pitchyinfft_t
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 uint_t short_period; /** shortest period under which to check for octave error */
43 static const smpl_t freqs[] = {
44 0., 20., 25., 31.5, 40., 50., 63., 80., 100., 125.,
45 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250.,
46 1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000.,
47 12500., 15000., 20000., 25100
50 static const smpl_t weight[] = {
51 -75.8, -70.1, -60.8, -52.1, -44.2, -37.5, -31.3, -25.6, -20.9, -16.5,
52 -12.6, -9.60, -7.00, -4.70, -3.00, -1.80, -0.80, -0.20, -0.00, 0.50,
53 1.60, 3.20, 5.40, 7.80, 8.10, 5.30, -2.40, -11.1, -12.8, -12.2,
54 -7.40, -17.8, -17.8, -17.8
58 new_aubio_pitchyinfft (uint_t samplerate, uint_t bufsize)
61 smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0;
62 aubio_pitchyinfft_t *p = AUBIO_NEW (aubio_pitchyinfft_t);
63 p->winput = new_fvec (bufsize);
64 p->fft = new_aubio_fft (bufsize);
65 if (!p->fft) goto beach;
66 p->fftout = new_fvec (bufsize);
67 p->sqrmag = new_fvec (bufsize);
68 p->yinfft = new_fvec (bufsize / 2 + 1);
70 p->win = new_aubio_window ("hanningz", bufsize);
71 p->weight = new_fvec (bufsize / 2 + 1);
72 for (i = 0; i < p->weight->length; i++) {
73 freq = (smpl_t) i / (smpl_t) bufsize *(smpl_t) samplerate;
74 while (freq > freqs[j]) {
81 if (f0 == f1) { // just in case
82 p->weight->data[i] = a0;
83 } else if (f0 == 0) { // y = ax+b
84 p->weight->data[i] = (a1 - a0) / f1 * freq + a0;
86 p->weight->data[i] = (a1 - a0) / (f1 - f0) * freq +
87 (a0 - (a1 - a0) / (f1 / f0 - 1.));
89 while (freq > freqs[j]) {
92 //AUBIO_DBG("%f\n",p->weight->data[i]);
93 p->weight->data[i] = DB2LIN (p->weight->data[i]);
94 //p->weight->data[i] = SQRT(DB2LIN(p->weight->data[i]));
96 // check for octave errors above 1300 Hz
97 p->short_period = (uint_t)ROUND(samplerate / 1300.);
101 if (p->winput) del_fvec(p->winput);
107 aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, const fvec_t * input, fvec_t * output)
110 uint_t length = p->fftout->length;
112 fvec_t *fftout = p->fftout;
113 fvec_t *yin = p->yinfft;
114 smpl_t tmp = 0., sum = 0.;
116 fvec_weighted_copy(input, p->win, p->winput);
117 // get the real / imag parts of its fft
118 aubio_fft_do_complex (p->fft, p->winput, fftout);
119 // get the squared magnitude spectrum, applying some weight
120 p->sqrmag->data[0] = SQR(fftout->data[0]);
121 p->sqrmag->data[0] *= p->weight->data[0];
122 for (l = 1; l < length / 2; l++) {
123 p->sqrmag->data[l] = SQR(fftout->data[l]) + SQR(fftout->data[length - l]);
124 p->sqrmag->data[l] *= p->weight->data[l];
125 p->sqrmag->data[length - l] = p->sqrmag->data[l];
127 p->sqrmag->data[length / 2] = SQR(fftout->data[length / 2]);
128 p->sqrmag->data[length / 2] *= p->weight->data[length / 2];
129 // get sum of weighted squared mags
130 for (l = 0; l < length / 2 + 1; l++) {
131 sum += p->sqrmag->data[l];
134 // get the real / imag parts of the fft of the squared magnitude
135 aubio_fft_do_complex (p->fft, p->sqrmag, fftout);
137 for (tau = 1; tau < yin->length; tau++) {
138 // compute the square differences
139 yin->data[tau] = sum - fftout->data[tau];
140 // and the cumulative mean normalized difference function
141 tmp += yin->data[tau];
143 yin->data[tau] *= tau / tmp;
148 // find best candidates
149 tau = fvec_min_elem (yin);
150 if (yin->data[tau] < p->tol) {
151 // no interpolation, directly return the period as an integer
152 //output->data[0] = tau;
155 // 3 point quadratic interpolation
156 //return fvec_quadratic_peak_pos (yin,tau,1);
157 /* additional check for (unlikely) octave doubling in higher frequencies */
158 if (tau > p->short_period) {
159 output->data[0] = fvec_quadratic_peak_pos (yin, tau);
161 /* should compare the minimum value of each interpolated peaks */
162 halfperiod = FLOOR (tau / 2 + .5);
163 if (yin->data[halfperiod] < p->tol)
164 output->data[0] = fvec_quadratic_peak_pos (yin, halfperiod);
166 output->data[0] = fvec_quadratic_peak_pos (yin, tau);
169 output->data[0] = 0.;
174 del_aubio_pitchyinfft (aubio_pitchyinfft_t * p)
177 del_aubio_fft (p->fft);
178 del_fvec (p->yinfft);
179 del_fvec (p->sqrmag);
180 del_fvec (p->fftout);
181 del_fvec (p->winput);
182 del_fvec (p->weight);
187 aubio_pitchyinfft_get_confidence (aubio_pitchyinfft_t * o) {
188 o->confidence = 1. - fvec_min (o->yinfft);
189 return o->confidence;
193 aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol)
200 aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p)