2 Copyright (C) 2003-2009 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"
33 #include "pitch/pitchyin.h"
35 struct _aubio_pitchyin_t
42 /** compute difference function
44 \param input input signal
45 \param yinbuf output buffer to store difference function (half shorter than input)
48 void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf);
50 /** in place computation of the YIN cumulative normalised function
52 \param yinbuf input signal (a square difference function), also used to store function
55 void aubio_pitchyin_getcum (fvec_t * yinbuf);
57 /** detect pitch in a YIN function
59 \param yinbuf input buffer as computed by aubio_pitchyin_getcum
62 uint_t aubio_pitchyin_getpitch (const fvec_t * yinbuf);
65 new_aubio_pitchyin (uint_t bufsize)
67 aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t);
68 o->yin = new_fvec (bufsize / 2);
74 del_aubio_pitchyin (aubio_pitchyin_t * o)
80 /* outputs the difference function */
82 aubio_pitchyin_diff (fvec_t * input, fvec_t * yin)
86 for (tau = 0; tau < yin->length; tau++) {
89 for (tau = 1; tau < yin->length; tau++) {
90 for (j = 0; j < yin->length; j++) {
91 tmp = input->data[j] - input->data[j + tau];
92 yin->data[tau] += SQR (tmp);
97 /* cumulative mean normalized difference function */
99 aubio_pitchyin_getcum (fvec_t * yin)
104 //AUBIO_DBG("%f\t",yin->data[0]);
105 for (tau = 1; tau < yin->length; tau++) {
106 tmp += yin->data[tau];
107 yin->data[tau] *= tau / tmp;
108 //AUBIO_DBG("%f\t",yin->data[tau]);
114 aubio_pitchyin_getpitch (const fvec_t * yin)
118 if (yin->data[tau] < 0.1) {
119 while (yin->data[tau + 1] < yin->data[tau]) {
125 } while (tau < yin->length);
126 //AUBIO_DBG("No pitch found");
130 /* all the above in one */
132 aubio_pitchyin_do (aubio_pitchyin_t * o, const fvec_t * input, fvec_t * out)
134 const smpl_t tol = o->tol;
135 fvec_t* yin = o->yin;
136 const smpl_t *input_data = input->data;
137 const uint_t length = yin->length;
138 smpl_t *yin_data = yin->data;
141 smpl_t tmp, tmp2 = 0.;
144 for (tau = 1; tau < length; tau++) {
146 for (j = 0; j < length; j++) {
147 tmp = input_data[j] - input_data[j + tau];
148 yin_data[tau] += SQR (tmp);
150 tmp2 += yin_data[tau];
152 yin->data[tau] *= tau / tmp2;
157 if (tau > 4 && (yin_data[period] < tol) &&
158 (yin_data[period] < yin_data[period + 1])) {
159 out->data[0] = fvec_quadratic_peak_pos (yin, period);
163 out->data[0] = fvec_quadratic_peak_pos (yin, fvec_min_elem (yin));
169 aubio_pitchyin_get_confidence (aubio_pitchyin_t * o) {
170 o->confidence = 1. - fvec_min (o->yin);
171 return o->confidence;
175 aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol)
182 aubio_pitchyin_get_tolerance (aubio_pitchyin_t * o)