# HG changeset patch # User Jamie Bullock # Date 1160385723 0 # Node ID 1aec087ddfcabfa8d9ff114c872625263b33c642 # Parent 81eb5810a301368320b9f9fc33dcf17f125df2c7 Added f0 estimation (based on AMDF) diff -r 81eb5810a301 -r 1aec087ddfca examples/puredata/xtract~.c --- a/examples/puredata/xtract~.c Sun Oct 08 15:31:40 2006 +0000 +++ b/examples/puredata/xtract~.c Mon Oct 09 09:22:03 2006 +0000 @@ -123,11 +123,14 @@ else if(tmp == gensym("sharpness")) x->feature = SHARPNESS; else if(tmp == gensym("slope")) x->feature = SLOPE; else if(tmp == gensym("f0")){ - x->feature = F0; + x->feature = F0; + x->argv = getbytes(sizeof(t_float)); + } + else if(tmp == gensym("hps"))x->feature = HPS; + else if(tmp == gensym("lowest_match")){ + x->feature = LOWEST_MATCH; x->argv = getbytes(sizeof(t_float)); - } - else if(tmp == gensym("hps"))x->feature = HPS; - else if(tmp == gensym("lowest_match"))x->feature = LOWEST_MATCH; + } else if(tmp == gensym("magnitude_spectrum")) x->feature = MAGNITUDE_SPECTRUM; else if(tmp == gensym("autocorrelation")) x->feature = AUTOCORRELATION; diff -r 81eb5810a301 -r 1aec087ddfca src/scalar.c --- a/src/scalar.c Sun Oct 08 15:31:40 2006 +0000 +++ b/src/scalar.c Mon Oct 09 09:22:03 2006 +0000 @@ -436,6 +436,25 @@ int xtract_f0(float *data, int N, void *argv, float *result){ - NOT_IMPLEMENTED; - + int M, sr, tau, n; + float f0, err_tau_1, err_tau_x; + + sr = *(float*)argv; + M = N >> 1; + err_tau_1 = 0; + for (n = 1; n < M; n++){ + err_tau_1 = err_tau_1 + fabs(data[n] - data[n+1]); + } + for (tau = 2; tau < M; tau++){ + err_tau_x = 0; + for (n = 1; n < M; n++){ + err_tau_x = err_tau_x + fabs(data[n] - data[n+tau]); + } + if (err_tau_x < err_tau_1) { + f0 = sr / (tau + (err_tau_x / err_tau_1)); + *result = f0; + return SUCCESS; + } + } + return NO_RESULT; } diff -r 81eb5810a301 -r 1aec087ddfca xtract/libxtract.h --- a/xtract/libxtract.h Sun Oct 08 15:31:40 2006 +0000 +++ b/xtract/libxtract.h Mon Oct 09 09:22:03 2006 +0000 @@ -109,7 +109,8 @@ SUCCESS, MALLOC_FAILED, BAD_ARGV, - BAD_VECTOR_SIZE + BAD_VECTOR_SIZE, + NO_RESULT }; /** diff -r 81eb5810a301 -r 1aec087ddfca xtract/xtract_scalar.h --- a/xtract/xtract_scalar.h Sun Oct 08 15:31:40 2006 +0000 +++ b/xtract/xtract_scalar.h Mon Oct 09 09:22:03 2006 +0000 @@ -288,8 +288,11 @@ * * \param *data: a pointer to the first element in an array of floats representing an audio vector * \param N: the number of elements to be considered - * \param *argv: a pointer to NULL + * \param *argv: a pointer to a float representing the sample rate of the vector * \param *result: the pitch of N values from the array pointed to by *data + * + * This algorithm is based on the AMDF and would benefit from further refinement + * */ int xtract_f0(float *data, int N, void *argv, float *result);