changeset 12:1aec087ddfca

Added f0 estimation (based on AMDF)
author Jamie Bullock <jamie@postlude.co.uk>
date Mon, 09 Oct 2006 09:22:03 +0000
parents 81eb5810a301
children 582330cfa6e5
files examples/puredata/xtract~.c src/scalar.c xtract/libxtract.h xtract/xtract_scalar.h
diffstat 4 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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;
--- 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;
 }
--- 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
 };
 
 /**
--- 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);