- YIN estimator improvements:

pp [2008-03-23 17:38:12]
- YIN estimator improvements:
  - minimum detection now operates on interpolated values
  - period calculation now interpolates raw difference function, as interpolation of the normalized function is said to be slightly biased
  - added high threshold parameter, above which the result is ignored (no note detected)


git-svn-id: https://lampka.siedziba.pl:790/svn/repos/lingot-win32@314 455248ca-bdda-0310-9134-f4ebb693071a
Filename
src/lingot-config.c
src/lingot-config.h
src/lingot-core.c
src/lingot-yin.c
src/lingot-yin.h
diff --git a/src/lingot-config.c b/src/lingot-config.c
index e228540..898c56f 100644
--- a/src/lingot-config.c
+++ b/src/lingot-config.c
@@ -35,12 +35,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", "YIN_THRESHOLD",
-      "ESTIMATOR", NULL // NULL terminated array
+      "PEAK_REJECTION_RELATION", "DFT_NUMBER", "DFT_SIZE", "YIN_THRESHOLD_LOW",
+      "YIN_THRESHOLD_HIGH", "ESTIMATOR", NULL // NULL terminated array
     };

 // print/scan param formats.
-const char* format = "sddffdffffddfddfd";
+const char* format = "sddffdffffddfddffd";

 //----------------------------------------------------------------------------

@@ -59,10 +59,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->yin_threshold,
-          &config->estimator };
+          &config->dft_number, &config->dft_size, &config->yin_threshold_low,
+          &config->yin_threshold_high, &config->estimator };

-    memcpy(config->param, c_param, 17*sizeof(void*));
+    memcpy(config->param, c_param, 18*sizeof(void*));

     return config;
   }
@@ -100,7 +100,8 @@ void lingot_config_reset(LingotConfig* config)

     config->vr = -0.45; // near to minimum

-    config->yin_threshold = 0.1;
+    config->yin_threshold_low = 0.1;
+    config->yin_threshold_high = 0.2;
     config->estimator = 0;

     lingot_config_update_internal_params(config);
diff --git a/src/lingot-config.h b/src/lingot-config.h
index dcdc2bf..1d0fcaa 100644
--- a/src/lingot-config.h
+++ b/src/lingot-config.h
@@ -35,7 +35,7 @@ typedef struct _LingotConfig LingotConfig;

 struct _LingotConfig
   {
-    void* param[17]; // parameter pointer array.
+    void* param[18]; // parameter pointer array.

     char audio_dev[80]; // default "/dev/dsp"
     unsigned int sample_rate; // soundcard sample rate.
@@ -83,7 +83,8 @@ struct _LingotConfig
     unsigned int dft_number; // number of DFTs.
     unsigned int dft_size; // samples of each DFT.

-    FLT yin_threshold;
+    FLT yin_threshold_low;
+    FLT yin_threshold_high;
     unsigned int estimator;

     // max iterations for Newton-Raphson algorithm.
diff --git a/src/lingot-core.c b/src/lingot-core.c
index 5f7584c..bef46c6 100644
--- a/src/lingot-core.c
+++ b/src/lingot-core.c
@@ -273,7 +273,7 @@ void lingot_core_process(LingotCore* core) {
 	//  ------------------------------------------
 	//

-	if (core->conf->estimator == 1) core->freq = core->conf->sample_rate / (FLT)core->conf->oversampling / yin(core);
+	if (core->conf->estimator == 1) yin(core);

 	// ----------------- TRANSFORMATION TO FREQUENCY DOMAIN ----------------

diff --git a/src/lingot-yin.c b/src/lingot-yin.c
index 05eaa59..42e478e 100644
--- a/src/lingot-yin.c
+++ b/src/lingot-yin.c
@@ -34,41 +34,59 @@ inline FLT yin_sqr(FLT x)
 	return x*x;
 }

-inline FLT yin_interpolate_tau(FLT y1, FLT y2, FLT y3)
+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))));
 }

-FLT yin(LingotCore* core) {
-	FLT dt_tau;
+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_right = 1.0/0.0, dpt_center, dpt_left;
-	FLT thr = core->conf->yin_threshold;
+	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_min_left, dpt_min_right;
-	int j, tau, tau_min;
+	FLT dpt_interpolated;
+	FLT tau_min;
+	int j, tau;
 	int len = core->conf->temporal_buffer_size / 2;

-	for (tau = 1; (tau < len) && ((dpt_center > thr) || (dpt_right < dpt_center) || (tau <= 2)); tau++)
+	for (tau = 1; tau < len; tau++)
 	{
-		dt_tau = 0;
+		dt_tau[0] = dt_tau[1];
+		dt_tau[1] = dt_tau[2];
+		dt_tau[2] = 0;
 		for (j = 0; j < len ; j++)
 		{
-			dt_tau += yin_sqr(core->temporal_buffer[j] - core->temporal_buffer[j+tau]);
+			dt_tau[2] += yin_sqr(core->temporal_buffer[j] - core->temporal_buffer[j+tau]);
 		}
-		dt_tau_sum += dt_tau;
+		dt_tau_sum += dt_tau[2];

-		dpt_left = dpt_center;
-		dpt_center = dpt_right;
-		dpt_right = dt_tau / ( 1.0 / tau * dt_tau_sum );
+		dpt[0] = dpt[1];
+		dpt[1] = dpt[2];
+		dpt[2] = dt_tau[2] / ( 1.0 / tau * dt_tau_sum );

-		if (dpt_center < dpt_min)
+		if (tau >= 3)
 		{
-			dpt_min_left = dpt_left;
-			dpt_min = dpt_center;
-			dpt_min_right = dpt_right;
-			tau_min = tau - 1;
+			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;
+				}
+			}
 		}
 	}
-	return (FLT)tau_min + yin_interpolate_tau(dpt_min_left, dpt_min, dpt_min_right);
+	if ((tau_min > 1) && (dpt_min < thr_high)) core->freq = core->conf->sample_rate / (FLT)core->conf->oversampling / tau_min; else core->freq = 0;
 }
diff --git a/src/lingot-yin.h b/src/lingot-yin.h
index 1109c22..040ef0d 100644
--- a/src/lingot-yin.h
+++ b/src/lingot-yin.h
@@ -26,6 +26,6 @@
 #include "lingot-core.h"

 // returns detected period as a number of samples
-FLT yin(LingotCore* core);
+void yin(LingotCore* core);

 #endif
ViewGit