2 Copyright (C) 2003-2017 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 /* This algorithm was developed by A. de Cheveigné and H. Kawahara and
24 * de Cheveigné, A., Kawahara, H. (2002) "YIN, a fundamental frequency
25 * estimator for speech and music", J. Acoust. Soc. Am. 111, 1917-1930.
27 * see http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html
30 #include "aubio_priv.h"
32 #include "mathutils.h"
34 #include "spectral/fft.h"
35 #include "pitch/pitchyinfast.h"
37 struct _aubio_pitchyinfast_t
50 aubio_pitchyinfast_t *
51 new_aubio_pitchyinfast (uint_t bufsize)
53 aubio_pitchyinfast_t *o = AUBIO_NEW (aubio_pitchyinfast_t);
54 o->yin = new_fvec (bufsize / 2);
55 o->tmpdata = new_fvec (bufsize);
56 o->sqdiff = new_fvec (bufsize / 2);
57 o->kernel = new_fvec (bufsize);
58 o->samples_fft = new_fvec (bufsize);
59 o->kernel_fft = new_fvec (bufsize);
60 o->fft = new_aubio_fft (bufsize);
61 if (!o->yin || !o->tmpdata || !o->tmpdata || !o->sqdiff
62 || !o->kernel || !o->samples_fft || !o->kernel || !o->fft)
64 del_aubio_pitchyinfast(o);
73 del_aubio_pitchyinfast (aubio_pitchyinfast_t * o)
78 del_fvec (o->tmpdata);
84 del_fvec (o->samples_fft);
86 del_fvec (o->kernel_fft);
88 del_aubio_fft (o->fft);
92 /* all the above in one */
94 aubio_pitchyinfast_do (aubio_pitchyinfast_t * o, const fvec_t * input, fvec_t * out)
96 const smpl_t tol = o->tol;
98 const uint_t length = yin->length;
99 uint_t B = o->tmpdata->length;
100 uint_t W = o->yin->length; // B / 2
101 fvec_t tmp_slice, kernel_ptr;
106 // compute r_t(0) + r_t+tau(0)
108 fvec_t *squares = o->tmpdata;
109 fvec_weighted_copy(input, input, squares);
111 for (tau = 0; tau < W; tau++) {
112 tmp_slice.data = squares->data + tau;
113 tmp_slice.length = W;
114 o->sqdiff->data[tau] = fvec_sum(&tmp_slice);
117 tmp_slice.data = squares->data;
118 tmp_slice.length = W;
119 o->sqdiff->data[0] = fvec_sum(&tmp_slice);
120 for (tau = 1; tau < W; tau++) {
121 o->sqdiff->data[tau] = o->sqdiff->data[tau-1];
122 o->sqdiff->data[tau] -= squares->data[tau-1];
123 o->sqdiff->data[tau] += squares->data[W+tau-1];
126 fvec_add(o->sqdiff, o->sqdiff->data[0]);
128 // compute r_t(tau) = -2.*ifft(fft(samples)*fft(samples[W-1::-1]))
130 fvec_t *compmul = o->tmpdata;
131 fvec_t *rt_of_tau = o->samples_fft;
132 aubio_fft_do_complex(o->fft, input, o->samples_fft);
133 // build kernel, take a copy of first half of samples
134 tmp_slice.data = input->data;
135 tmp_slice.length = W;
136 kernel_ptr.data = o->kernel->data + 1;
137 kernel_ptr.length = W;
138 fvec_copy(&tmp_slice, &kernel_ptr);
140 fvec_rev(&kernel_ptr);
141 // compute fft(kernel)
142 aubio_fft_do_complex(o->fft, o->kernel, o->kernel_fft);
143 // compute complex product
144 compmul->data[0] = o->kernel_fft->data[0] * o->samples_fft->data[0];
145 for (tau = 1; tau < W; tau++) {
146 compmul->data[tau] = o->kernel_fft->data[tau] * o->samples_fft->data[tau];
147 compmul->data[tau] -= o->kernel_fft->data[B-tau] * o->samples_fft->data[B-tau];
149 compmul->data[W] = o->kernel_fft->data[W] * o->samples_fft->data[W];
150 for (tau = 1; tau < W; tau++) {
151 compmul->data[B-tau] = o->kernel_fft->data[B-tau] * o->samples_fft->data[tau];
152 compmul->data[B-tau] += o->kernel_fft->data[tau] * o->samples_fft->data[B-tau];
154 // compute inverse fft
155 aubio_fft_rdo_complex(o->fft, compmul, rt_of_tau);
156 // compute square difference r_t(tau) = sqdiff - 2 * r_t_tau[W-1:-1]
157 for (tau = 0; tau < W; tau++) {
158 yin->data[tau] = o->sqdiff->data[tau] - 2. * rt_of_tau->data[tau+W];
162 // now build yin and look for first minimum
165 for (tau = 1; tau < length; tau++) {
166 tmp2 += yin->data[tau];
168 yin->data[tau] *= tau / tmp2;
173 if (tau > 4 && (yin->data[period] < tol) &&
174 (yin->data[period] < yin->data[period + 1])) {
175 o->peak_pos = (uint_t)period;
176 out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
180 // use global minimum
181 o->peak_pos = (uint_t)fvec_min_elem (yin);
182 out->data[0] = fvec_quadratic_peak_pos (yin, o->peak_pos);
186 aubio_pitchyinfast_get_confidence (aubio_pitchyinfast_t * o) {
187 return 1. - o->yin->data[o->peak_pos];
191 aubio_pitchyinfast_set_tolerance (aubio_pitchyinfast_t * o, smpl_t tol)
198 aubio_pitchyinfast_get_tolerance (aubio_pitchyinfast_t * o)