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