From: Paul Brossier Date: Tue, 17 Apr 2007 19:59:15 +0000 (+0200) Subject: mathutils.c: add aubio_zero_crossing_rate at Amaury's request X-Git-Tag: bzr2git~525 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=fff2bee8ed03bff37c5f9c2189639a30a1ea6054;p=aubio.git mathutils.c: add aubio_zero_crossing_rate at Amaury's request --- diff --git a/src/mathutils.c b/src/mathutils.c index dfb40195..e91f3e9f 100644 --- a/src/mathutils.c +++ b/src/mathutils.c @@ -440,6 +440,23 @@ smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold) { 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]; diff --git a/src/mathutils.h b/src/mathutils.h index dc130de3..9bf0f583 100644 --- a/src/mathutils.h +++ b/src/mathutils.h @@ -203,6 +203,10 @@ smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold); * calculate normalised autocorrelation function */ 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 *