mathutils.c: add aubio_spectral_centroid
authorPaul Brossier <piem@piem.org>
Tue, 17 Apr 2007 23:07:48 +0000 (01:07 +0200)
committerPaul Brossier <piem@piem.org>
Tue, 17 Apr 2007 23:07:48 +0000 (01:07 +0200)
src/mathutils.c
src/mathutils.h

index e91f3e9..d561fce 100644 (file)
@@ -457,6 +457,19 @@ smpl_t aubio_zero_crossing_rate(fvec_t * input) {
   return zcr/(smpl_t)input->length;
 }
 
+smpl_t aubio_spectral_centroid(cvec_t * spectrum, smpl_t samplerate) {
+  uint_t i=0, j;
+  smpl_t sum = 0., sc = 0.;
+  for ( j = 0; j < spectrum->length; j++ ) {
+    sum += spectrum->norm[i][j];
+  }
+  if (sum == 0.) return 0.;
+  for ( j = 0; j < spectrum->length; j++ ) {
+    sc += (smpl_t)j * spectrum->norm[i][j];
+  }
+  return sc / sum * samplerate / (smpl_t)(spectrum->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];
index 9bf0f58..75fc5d7 100644 (file)
@@ -208,6 +208,10 @@ void aubio_autocorr(fvec_t * input, fvec_t * output);
  */
 smpl_t aubio_zero_crossing_rate(fvec_t * input);
 /**
+ * spectrum centroid computed on a cvec
+ */
+smpl_t aubio_spectral_centroid(cvec_t * input, smpl_t samplerate);
+/**
  * clean up cached memory at the end of program
  *
  * use this function at the end of programs to purge all