1 | #! /usr/bin/env python |
---|

2 | |
---|

3 | from numpy.testing import TestCase |
---|

4 | from aubio import fvec, zero_crossing_rate |
---|

5 | |
---|

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 """ |
---|

24 | self.vector[int(buf_size / 2)] = 1. |
---|

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 """ |
---|

29 | self.vector[int(buf_size / 2)] = -1. |
---|

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 """ |
---|

34 | self.vector[int(buf_size / 2) - 1] = 1. |
---|

35 | self.vector[int(buf_size / 2)] = -1. |
---|

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""" |
---|

40 | self.vector[int(buf_size / 2) - 2] = 1. |
---|

41 | self.vector[int(buf_size / 2)] = -1. |
---|

42 | self.assertEqual(2./buf_size, zero_crossing_rate(self.vector)) |
---|

43 | |
---|

44 | if __name__ == '__main__': |
---|

45 | from unittest import main |
---|

46 | main() |
---|