From 0dbdb401e562c7e0359415653ec1d5276daa7787 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Wed, 18 Apr 2007 01:07:48 +0200 Subject: [PATCH] mathutils.c: add aubio_spectral_centroid --- src/mathutils.c | 13 +++++++++++++ src/mathutils.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/mathutils.c b/src/mathutils.c index e91f3e9f..d561fced 100644 --- a/src/mathutils.c +++ b/src/mathutils.c @@ -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]; diff --git a/src/mathutils.h b/src/mathutils.h index 9bf0f583..75fc5d7e 100644 --- a/src/mathutils.h +++ b/src/mathutils.h @@ -207,6 +207,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); +/** + * 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 * -- 2.26.2