[969bbb2] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | from numpy.testing import TestCase |
---|
[89e9e71] | 4 | from aubio import fvec, zero_crossing_rate |
---|
| 5 | |
---|
[969bbb2] | 6 | buf_size = 2048 |
---|
| 7 | |
---|
| 8 | class zero_crossing_rate_test_case(TestCase): |
---|
| 9 | |
---|
| 10 | def setUp(self): |
---|
| 11 | self.vector = fvec(buf_size) |
---|
| 12 | |
---|
| 13 | def test_zeroes(self): |
---|
| 14 | """ check zero crossing rate on a buffer of 0. """ |
---|
| 15 | self.assertEqual(0., zero_crossing_rate(self.vector)) |
---|
| 16 | |
---|
| 17 | def test_ones(self): |
---|
| 18 | """ check zero crossing rate on a buffer of 1. """ |
---|
| 19 | self.vector[:] = 1. |
---|
| 20 | self.assertEqual(0., zero_crossing_rate(self.vector)) |
---|
| 21 | |
---|
| 22 | def test_impulse(self): |
---|
| 23 | """ check zero crossing rate on a buffer with an impulse """ |
---|
[376d5e9] | 24 | self.vector[int(buf_size / 2)] = 1. |
---|
[969bbb2] | 25 | self.assertEqual(0., zero_crossing_rate(self.vector)) |
---|
| 26 | |
---|
| 27 | def test_negative_impulse(self): |
---|
| 28 | """ check zero crossing rate on a buffer with a negative impulse """ |
---|
[376d5e9] | 29 | self.vector[int(buf_size / 2)] = -1. |
---|
[969bbb2] | 30 | self.assertEqual(2./buf_size, zero_crossing_rate(self.vector)) |
---|
| 31 | |
---|
| 32 | def test_single(self): |
---|
| 33 | """ check zero crossing rate on single crossing """ |
---|
[376d5e9] | 34 | self.vector[int(buf_size / 2) - 1] = 1. |
---|
| 35 | self.vector[int(buf_size / 2)] = -1. |
---|
[969bbb2] | 36 | self.assertEqual(2./buf_size, zero_crossing_rate(self.vector)) |
---|
| 37 | |
---|
| 38 | def test_single_with_gap(self): |
---|
| 39 | """ check zero crossing rate on single crossing with a gap""" |
---|
[376d5e9] | 40 | self.vector[int(buf_size / 2) - 2] = 1. |
---|
| 41 | self.vector[int(buf_size / 2)] = -1. |
---|
[969bbb2] | 42 | self.assertEqual(2./buf_size, zero_crossing_rate(self.vector)) |
---|
| 43 | |
---|
| 44 | if __name__ == '__main__': |
---|
[bb63c19] | 45 | from unittest import main |
---|
[969bbb2] | 46 | main() |
---|