From 7e204d01af966224f8823e68d5cc4eb114e9f3e0 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Fri, 16 May 2008 00:50:38 +0200 Subject: [PATCH] src/mathutils.c: change zero crossing function to split at >=0/<0; tests/python/src/temporal/zero_crossing_rate.py: add some tests for zero crossing --- src/mathutils.c | 16 +++--- .../python/src/temporal/zero_crossing_rate.py | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 tests/python/src/temporal/zero_crossing_rate.py diff --git a/src/mathutils.c b/src/mathutils.c index 1ecb2fd6..5a6e9bac 100644 --- a/src/mathutils.c +++ b/src/mathutils.c @@ -419,14 +419,18 @@ 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. ) { + // previous was strictly negative + if( input->data[i][j-1] < 0. ) { + // current is positive or null + if ( input->data[i][j] >= 0. ) { + zcr += 1; + } + // previous was positive or null + } else { + // current is strictly negative + 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; diff --git a/tests/python/src/temporal/zero_crossing_rate.py b/tests/python/src/temporal/zero_crossing_rate.py new file mode 100644 index 00000000..16932410 --- /dev/null +++ b/tests/python/src/temporal/zero_crossing_rate.py @@ -0,0 +1,54 @@ +import unittest + +from aubio.aubiowrapper import * + +buf_size = 2048 +channels = 1 + +class zero_crossing_rate_unit(unittest.TestCase): + + def setUp(self): + self.vector = new_fvec(buf_size, channels) + + def tearDown(self): + del_fvec(self.vector) + + def test(self): + """ create and delete fvec """ + pass + + def test_zeroes(self): + """ check zero crossing rate on a buffer of 0. """ + self.assertEqual(0., aubio_zero_crossing_rate(self.vector)) + + def test_ones(self): + """ check zero crossing rate on a buffer of 1. """ + for index in range(buf_size): + for channel in range(channels): + fvec_write_sample(self.vector, 1., channel, index) + self.assertEqual(0., aubio_zero_crossing_rate(self.vector)) + + def test_impulse(self): + """ check zero crossing rate on a buffer with an impulse """ + fvec_write_sample(self.vector, 1., 0, buf_size / 2) + self.assertEqual(0., aubio_zero_crossing_rate(self.vector)) + + def test_negative_impulse(self): + """ check zero crossing rate on a buffer with a negative impulse """ + fvec_write_sample(self.vector, -1., 0, buf_size / 2) + self.assertEqual(2./buf_size, aubio_zero_crossing_rate(self.vector)) + + def test_single(self): + """ check zero crossing rate on single crossing """ + fvec_write_sample(self.vector, +1., 0, buf_size / 2 - 1) + fvec_write_sample(self.vector, -1., 0, buf_size / 2) + self.assertEqual(2./buf_size, aubio_zero_crossing_rate(self.vector)) + + def test_single_with_gap(self): + """ check zero crossing rate on single crossing with a gap""" + fvec_write_sample(self.vector, +1., 0, buf_size / 2 - 2) + fvec_write_sample(self.vector, -1., 0, buf_size / 2) + self.assertEqual(2./buf_size, aubio_zero_crossing_rate(self.vector)) + +if __name__ == '__main__': + unittest.main() -- 2.26.2