shithub: aubio

Download patch

ref: fff2bee8ed03bff37c5f9c2189639a30a1ea6054
parent: 3fd2dc122c009180258df33d7e1179089ceab18d
author: Paul Brossier <piem@piem.org>
date: Tue Apr 17 17:59:15 EDT 2007

mathutils.c: add aubio_zero_crossing_rate at Amaury's request

--- a/src/mathutils.c
+++ b/src/mathutils.c
@@ -440,6 +440,23 @@
       return loudness;
 }
 
+smpl_t aubio_zero_crossing_rate(fvec_t * input) {
+  uint_t i=0,j;
+  uint_t zcr = 0;
+  for ( j = 1; j < input->length; j++ ) {
+    // previous was negative
+    if( input->data[i][j-1] <= 0. ) {
+      if ( input->data[i][j] > 0. ) {
+        zcr += 1;
+      }
+    //previous was positive
+    } else if ( input->data[i][j] <= 0. ){
+      zcr += 1;
+    }
+  }
+  return zcr/(smpl_t)input->length;
+}
+
 void aubio_autocorr(fvec_t * input, fvec_t * output){
         uint_t i = 0, j = 0, length = input->length;
         smpl_t * data = input->data[0];
--- a/src/mathutils.h
+++ b/src/mathutils.h
@@ -204,6 +204,10 @@
  */
 void aubio_autocorr(fvec_t * input, fvec_t * output);
 /**
+ * zero-crossing rate (number of zero cross per sample)
+ */
+smpl_t aubio_zero_crossing_rate(fvec_t * input);
+/**
  * clean up cached memory at the end of program
  *
  * use this function at the end of programs to purge all