From c77651dcdf11427071d69821d63b8de80874b7a2 Mon Sep 17 00:00:00 2001 From: pp Date: Sat, 22 Mar 2008 16:56:08 +0000 Subject: - experimental, very simple implementation of YIN fundamental frequency estimator git-svn-id: https://lampka.siedziba.pl:790/svn/repos/lingot-win32@309 455248ca-bdda-0310-9134-f4ebb693071a --- src/Makefile.am | 2 ++ src/lingot-config.c | 13 +++++++--- src/lingot-config.h | 5 +++- src/lingot-core.c | 6 +++++ src/lingot-yin.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lingot-yin.h | 31 ++++++++++++++++++++++ 6 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 src/lingot-yin.c create mode 100644 src/lingot-yin.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 97a8b32..9eaf214 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,6 +29,8 @@ lingot_SOURCES = \ lingot-filter.h\ lingot-signal.c\ lingot-signal.h\ + lingot-yin.c\ + lingot-yin.h\ lingot.c\ lingot-i18n.h diff --git a/src/lingot-config.c b/src/lingot-config.c index 7164a29..a46e372 100644 --- a/src/lingot-config.c +++ b/src/lingot-config.c @@ -34,11 +34,12 @@ char* token[] = { "AUDIO_DEV", "SAMPLE_RATE", "OVERSAMPLING", "ROOT_FREQUENCY_ERROR", "MIN_FREQUENCY", "FFT_SIZE", "TEMPORAL_WINDOW", "NOISE_THRESHOLD", "CALCULATION_RATE", "VISUALIZATION_RATE", "PEAK_NUMBER", "PEAK_ORDER", - "PEAK_REJECTION_RELATION", "DFT_NUMBER", "DFT_SIZE", NULL // NULL terminated array + "PEAK_REJECTION_RELATION", "DFT_NUMBER", "DFT_SIZE", "YIN_THRESHOLD", + "ESTIMATOR", NULL // NULL terminated array }; // print/scan param formats. -const char* format = "sddffdffffddfdd"; +const char* format = "sddffdffffddfddfd"; //---------------------------------------------------------------------------- @@ -57,9 +58,10 @@ LingotConfig* lingot_config_new() &config->noise_threshold_db, &config->calculation_rate, &config->visualization_rate, &config->peak_number, &config->peak_order, &config->peak_rejection_relation_db, - &config->dft_number, &config->dft_size }; + &config->dft_number, &config->dft_size, &config->yin_threshold, + &config->estimator }; - memcpy(config->param, c_param, 15*sizeof(void*)); + memcpy(config->param, c_param, 17*sizeof(void*)); return config; } @@ -97,6 +99,9 @@ void lingot_config_reset(LingotConfig* config) config->vr = -0.45; // near to minimum + config->yin_threshold = 0.1; + config->estimator = 0; + lingot_config_update_internal_params(config); } diff --git a/src/lingot-config.h b/src/lingot-config.h index 174730d..dcdc2bf 100644 --- a/src/lingot-config.h +++ b/src/lingot-config.h @@ -35,7 +35,7 @@ typedef struct _LingotConfig LingotConfig; struct _LingotConfig { - void* param[15]; // parameter pointer array. + void* param[17]; // parameter pointer array. char audio_dev[80]; // default "/dev/dsp" unsigned int sample_rate; // soundcard sample rate. @@ -83,6 +83,9 @@ struct _LingotConfig unsigned int dft_number; // number of DFTs. unsigned int dft_size; // samples of each DFT. + FLT yin_threshold; + unsigned int estimator; + // max iterations for Newton-Raphson algorithm. unsigned int max_nr_iter; diff --git a/src/lingot-core.c b/src/lingot-core.c index ef101ae..5f7584c 100644 --- a/src/lingot-core.c +++ b/src/lingot-core.c @@ -34,6 +34,7 @@ #include "lingot-signal.h" #include "lingot-core.h" #include "lingot-config.h" +#include "lingot-yin.h" LingotCore* lingot_core_new(LingotConfig* conf) { @@ -272,6 +273,8 @@ void lingot_core_process(LingotCore* core) { // ------------------------------------------ // + if (core->conf->estimator == 1) core->freq = core->conf->sample_rate / (FLT)core->conf->oversampling / yin(core); + // ----------------- TRANSFORMATION TO FREQUENCY DOMAIN ---------------- FLT _1_N2 = 1.0/(core->conf->fft_size*core->conf->fft_size); @@ -308,11 +311,14 @@ void lingot_core_process(LingotCore* core) { + core->fft_out[i].i*core->fft_out[i].i)*_1_N2; # endif + // representable piece memcpy(core->X, core->spd_fft, ((core->conf->fft_size > 256) ? (core->conf->fft_size >> 1) : 256) *sizeof(FLT)); + if (core->conf->estimator == 1 ) return; + // truncated 2nd derivative esteem, to enhance peaks core->diff2_spd_fft[0] = 0.0; for (i = 1; i < (core->conf->fft_size >> 1) - 1; i++) { diff --git a/src/lingot-yin.c b/src/lingot-yin.c new file mode 100644 index 0000000..617f625 --- /dev/null +++ b/src/lingot-yin.c @@ -0,0 +1,74 @@ +//-*- 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_tau(FLT y1, FLT y2, FLT y3) +{ + return ((y1 - y3)/(2.0*(y3 + y1 - (2.0*y2)))); +} + +FLT yin(LingotCore* core) { + FLT dt_tau; + FLT dt_tau_sum = 0; + FLT dpt_right = 1.0/0.0, dpt_center, dpt_left; + FLT thr = core->conf->yin_threshold; + FLT dpt_min = 1.0/0.0; + FLT dpt_min_left, dpt_min_right; + int j, tau, tau_min; + int len = core->conf->temporal_buffer_size / 2; + + for (tau = 1; (tau < len) && ((dpt_center > thr) || (dpt_right < dpt_center) || (tau <= 2)); tau++) + { + dt_tau = 0; + for (j = 0; j < len ; j++) + { + dt_tau += yin_sqr(core->temporal_buffer[j] - core->temporal_buffer[j+tau]); + } + dt_tau_sum += dt_tau; + + dpt_left = dpt_center; + dpt_center = dpt_right; + dpt_right = dt_tau / ( 1.0 / tau * dt_tau_sum ); + + if (dpt_center < dpt_min) + { + dpt_min_left = dpt_left; + dpt_min = dpt_center; + dpt_min_right = dpt_right; + tau_min = tau - 1; + } + } + return (FLT)tau_min + yin_interpolate_tau(dpt_left, dpt_center, dpt_right); +} diff --git a/src/lingot-yin.h b/src/lingot-yin.h new file mode 100644 index 0000000..1109c22 --- /dev/null +++ b/src/lingot-yin.h @@ -0,0 +1,31 @@ +//-*- 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 + */ + +#ifndef __LINGOT_YIN_H__ +#define __LINGOT_YIN_H__ + +#include "lingot-core.h" + +// returns detected period as a number of samples +FLT yin(LingotCore* core); + +#endif -- cgit v1.2.3