//-*- C++ -*- /* * YIN pitch estimator * Copyright (C) 2008 Piotr Pawlow * * This file is part of lingot. * * lingot is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * lingot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with lingot; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Implementation of YIN algorithm, published in: * * A. de Cheveigne and H. Kawahara: YIN, an F0 estimator * J. Acoust. Soc. Am., Vol. 111, No. 4, April 2002 * http://www.ircam.fr/pcm/cheveign/pss/2002_JASA_YIN.pdf */ #include "lingot-yin.h" inline FLT yin_sqr(FLT x) { return x*x; } inline FLT yin_interpolate_x(FLT y1, FLT y2, FLT y3) { if ((y1 == y2) && (y2 == y3)) return 0; return ((y1 - y3)/(2.0*(y3 + y1 - (2.0*y2)))); } inline FLT yin_interpolate_y(FLT y1, FLT y2, FLT y3) { if ((y1 == y2) && (y2 == y3)) return y1; return (((6.0 * y2) - y1 + (3.0 * y3) - (4.0 * yin_sqr(y2 - y3) / (y3 - (2.0 * y2) + y1))) / 8.0); } void yin(LingotCore* core) { FLT dt_tau[3]; FLT dt_tau_sum = 0; FLT dpt[3]; FLT thr_low = core->conf->yin_threshold_low; FLT thr_high = core->conf->yin_threshold_high; FLT dpt_min = 1.0/0.0; FLT dpt_interpolated; FLT tau_min; int j, tau; int len = core->conf->temporal_buffer_size / 2; for (tau = 1; tau < len; tau++) { dt_tau[0] = dt_tau[1]; dt_tau[1] = dt_tau[2]; dt_tau[2] = 0; for (j = 0; j < len ; j++) { dt_tau[2] += yin_sqr(core->temporal_buffer[j] - core->temporal_buffer[j+tau]); } dt_tau_sum += dt_tau[2]; dpt[0] = dpt[1]; dpt[1] = dpt[2]; dpt[2] = dt_tau[2] / ( 1.0 / tau * dt_tau_sum ); if (tau >= 3) { if ((dpt[1] <= dpt[0]) && (dpt[1] <= dpt[2])) { // we have local minimum dpt_interpolated = yin_interpolate_y(dpt[0], dpt[1], dpt[2]); if (dpt_interpolated < dpt_min) { dpt_min = dpt_interpolated; tau_min = (FLT)tau - 1 + yin_interpolate_x(dt_tau[0], dt_tau[1], dt_tau[2]); if (dpt_min < thr_low) break; } } } } if ((tau_min > 1) && (dpt_min < thr_high)) core->freq = core->conf->sample_rate / (FLT)core->conf->oversampling / tau_min; else core->freq = 0; }