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);
66 del_aubio_pitchyinfast (aubio_pitchyinfast_t * o)
69 del_fvec (o->tmpdata);
72 del_fvec (o->samples_fft);
73 del_fvec (o->kernel_fft);
74 del_aubio_fft (o->fft);
78 /* all the above in one */
80 aubio_pitchyinfast_do (aubio_pitchyinfast_t * o, const fvec_t * input, fvec_t * out)
82 const smpl_t tol = o->tol;
84 const uint_t length = yin->length;
85 uint_t B = o->tmpdata->length;
86 uint_t W = o->yin->length; // B / 2
87 fvec_t tmp_slice, kernel_ptr;
88 smpl_t *yin_data = yin->data;
93 // compute r_t(0) + r_t+tau(0)
95 fvec_t *squares = o->tmpdata;
96 fvec_weighted_copy(input, input, squares);
98 for (tau = 0; tau < W; tau++) {
99 tmp_slice.data = squares->data + tau;
100 tmp_slice.length = W;
101 o->sqdiff->data[tau] = fvec_sum(&tmp_slice);
104 tmp_slice.data = squares->data;
105 tmp_slice.length = W;
106 o->sqdiff->data[0] = fvec_sum(&tmp_slice);
107 for (tau = 1; tau < W; tau++) {
108 o->sqdiff->data[tau] = o->sqdiff->data[tau-1];
109 o->sqdiff->data[tau] -= squares->data[tau-1];
110 o->sqdiff->data[tau] += squares->data[W+tau-1];
113 fvec_add(o->sqdiff, o->sqdiff->data[0]);
115 // compute r_t(tau) = -2.*ifft(fft(samples)*fft(samples[W-1::-1]))
117 fvec_t *compmul = o->tmpdata;
118 fvec_t *rt_of_tau = o->samples_fft;
119 aubio_fft_do_complex(o->fft, input, o->samples_fft);
120 // build kernel, take a copy of first half of samples
121 tmp_slice.data = input->data;
122 tmp_slice.length = W;
123 kernel_ptr.data = o->kernel->data + 1;
124 kernel_ptr.length = W;
125 fvec_copy(&tmp_slice, &kernel_ptr);
127 fvec_rev(&kernel_ptr);
128 // compute fft(kernel)
129 aubio_fft_do_complex(o->fft, o->kernel, o->kernel_fft);
130 // compute complex product
131 compmul->data[0] = o->kernel_fft->data[0] * o->samples_fft->data[0];
132 for (tau = 1; tau < W; tau++) {
133 compmul->data[tau] = o->kernel_fft->data[tau] * o->samples_fft->data[tau];
134 compmul->data[tau] -= o->kernel_fft->data[B-tau] * o->samples_fft->data[B-tau];
136 compmul->data[W] = o->kernel_fft->data[W] * o->samples_fft->data[W];
137 for (tau = 1; tau < W; tau++) {
138 compmul->data[B-tau] = o->kernel_fft->data[B-tau] * o->samples_fft->data[tau];
139 compmul->data[B-tau] += o->kernel_fft->data[tau] * o->samples_fft->data[B-tau];
141 // compute inverse fft
142 aubio_fft_rdo_complex(o->fft, compmul, rt_of_tau);
143 // compute square difference r_t(tau) = sqdiff - 2 * r_t_tau[W-1:-1]
144 for (tau = 0; tau < W; tau++) {
145 yin_data[tau] = o->sqdiff->data[tau] - 2. * rt_of_tau->data[tau+W];
149 // now build yin and look for first minimum
150 fvec_set_all(out, 0.);
152 for (tau = 1; tau < length; tau++) {
153 tmp2 += yin_data[tau];
155 yin->data[tau] *= tau / tmp2;
160 if (tau > 4 && (yin_data[period] < tol) &&
161 (yin_data[period] < yin_data[period + 1])) {
162 out->data[0] = fvec_quadratic_peak_pos (yin, period);
166 out->data[0] = fvec_quadratic_peak_pos (yin, fvec_min_elem (yin) );
172 aubio_pitchyinfast_get_confidence (aubio_pitchyinfast_t * o) {
173 o->confidence = 1. - fvec_min (o->yin);
174 return o->confidence;
178 aubio_pitchyinfast_set_tolerance (aubio_pitchyinfast_t * o, smpl_t tol)
185 aubio_pitchyinfast_get_tolerance (aubio_pitchyinfast_t * o)