changeset 210:c99c00e1c508

Fixed bugs in xtract_failsafe_f0(). Should now actually be failsafe. Only exception is silent input.
author Jamie Bullock <jamie@jamiebullock.com>
date Mon, 17 Mar 2014 16:39:30 +0000
parents 2bd416a4a0de
children ef80f7c52c6d b5bdd0154dd6
files src/scalar.c
diffstat 1 files changed, 8 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/scalar.c	Tue Mar 11 20:18:10 2014 +0000
+++ b/src/scalar.c	Mon Mar 17 16:39:30 2014 +0000
@@ -942,23 +942,27 @@
 int xtract_failsafe_f0(const double *data, const int N, const void *argv, double *result)
 {
 
-    double *spectrum = NULL, argf[2], *peaks = NULL, return_code, sr;
+    double *spectrum = NULL, argf[4], *peaks = NULL, return_code, sr;
 
     return_code = xtract_f0(data, N, argv, result);
 
-    if(return_code == XTRACT_NO_RESULT)
+    if(return_code == XTRACT_NO_RESULT || *result == 0)
     {
         sr = *(double *)argv;
         if(sr == 0)
             sr = 44100.0;
         spectrum = (double *)malloc(N * sizeof(double));
         peaks = (double *)malloc(N * sizeof(double));
-        argf[0] = sr;
+        argf[0] = sr / (double)N;
         argf[1] = XTRACT_MAGNITUDE_SPECTRUM;
+        argf[2] = 0.f; /* DC component not Nyquist */
+        argf[3] = 1.f; /* Normalize */
         xtract_spectrum(data, N, argf, spectrum);
-        argf[1] = 10.0;
+        argf[1] = 50.0; /* Peak threshold is 70% of maximum peak found */
         xtract_peak_spectrum(spectrum, N >> 1, argf, peaks);
         argf[0] = 0.0;
+        
+        /* Assume the peak with the lowest frequency is the fundamental */
         xtract_lowest_value(peaks+(N >> 1), N >> 1, argf, result);
 
         free(spectrum);