[75e715f] | 1 | #! /usr/bin/env python |
---|
[fc197b0] | 2 | |
---|
| 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
---|
| 4 | from aubio import peakpicker, fvec |
---|
| 5 | |
---|
| 6 | class aubio_peakpicker(TestCase): |
---|
| 7 | |
---|
| 8 | def test_members(self): |
---|
| 9 | o = peakpicker() |
---|
| 10 | |
---|
| 11 | def test_peakpicker_zeroes(self): |
---|
| 12 | o = peakpicker() |
---|
| 13 | assert_equal(o.get_thresholded_input(), 0.) |
---|
| 14 | |
---|
| 15 | def test_peakpick_set_threshold(self): |
---|
| 16 | o = peakpicker() |
---|
| 17 | new_threshold = threshold |
---|
| 18 | o.set_threshold(new_threshold) |
---|
| 19 | assert_almost_equal(new_threshold, o.get_threshold()) |
---|
| 20 | |
---|
| 21 | def test_peakpicker_get_threshold(self): |
---|
| 22 | o = peakpicker() |
---|
| 23 | new_threshold = o.get_threshold() |
---|
| 24 | o.set_threshold(new_threshold) |
---|
| 25 | assert_equal(new_threshold, o.get_threshold()) |
---|
| 26 | |
---|
| 27 | buf_size = 1024 |
---|
| 28 | slice_size = 5 |
---|
| 29 | delay = 1 |
---|
| 30 | threshold = .9 |
---|
| 31 | |
---|
| 32 | class aubio_peakpicker_peaks(TestCase): |
---|
| 33 | |
---|
| 34 | def setUp(self): |
---|
| 35 | self.o = peakpicker() |
---|
| 36 | self.o.set_threshold (threshold) |
---|
| 37 | self.vec = fvec(buf_size) |
---|
| 38 | |
---|
| 39 | def test_peakpicker_impulse(self): |
---|
| 40 | vec = self.vec; o = self.o |
---|
| 41 | a = 345 |
---|
| 42 | vec[a] = 1000. |
---|
| 43 | self.peaks = [a] |
---|
| 44 | |
---|
| 45 | def test_peakpicker_ramp_up(self): |
---|
| 46 | vec = self.vec; o = self.o |
---|
| 47 | a = 345 |
---|
| 48 | vec[a] = 1000. / 4. * 1. |
---|
| 49 | vec[a+1] = 1000. / 4. * 2. |
---|
| 50 | vec[a+2] = 1000. / 4. * 3. |
---|
| 51 | vec[a+3] = 1000. |
---|
| 52 | self.peaks = [a+1] |
---|
| 53 | |
---|
| 54 | def test_peakpicker_ramp_down(self): |
---|
| 55 | vec = self.vec; o = self.o |
---|
| 56 | a = 345 |
---|
| 57 | vec[a] = 1000. |
---|
| 58 | vec[a+1] = 1000. / 4. * 3. |
---|
| 59 | vec[a+2] = 1000. / 4. * 2. |
---|
| 60 | vec[a+3] = 1000. / 4. * 1. |
---|
| 61 | self.peaks = [a] |
---|
| 62 | |
---|
| 63 | def test_peakpicker_plateau(self): |
---|
| 64 | vec = self.vec; o = self.o |
---|
| 65 | a = 345 |
---|
| 66 | vec[a] = 1000. / 2 |
---|
| 67 | vec[a+1] = 1000. |
---|
| 68 | vec[a+2] = 1000. |
---|
| 69 | vec[a+3] = 1000. |
---|
| 70 | vec[a+4] = 1000. / 2 |
---|
| 71 | self.peaks = [a+1] |
---|
| 72 | |
---|
| 73 | def test_peakpicker_consecutive_peaks(self): |
---|
| 74 | vec = self.vec; o = self.o |
---|
| 75 | a = 345 |
---|
| 76 | vec[a] = 1000. / 2 |
---|
| 77 | vec[a+1] = 1000. |
---|
| 78 | vec[a+3] = 1000. |
---|
| 79 | vec[a+4] = 1000. / 2 |
---|
| 80 | self.peaks = [a] |
---|
| 81 | |
---|
| 82 | def test_peakpicker_distant_peaks(self): |
---|
| 83 | vec = self.vec; o = self.o |
---|
| 84 | a = 345 |
---|
| 85 | vec[a] = 1000. |
---|
| 86 | vec[a+7] = 1000. |
---|
| 87 | self.peaks = [a, a+7] |
---|
| 88 | |
---|
| 89 | def test_peakpicker_very_distant_peaks(self): |
---|
| 90 | vec = self.vec; o = self.o |
---|
| 91 | a = 345 |
---|
| 92 | vec[a] = 1000. |
---|
| 93 | vec[a+67] = 1000. |
---|
| 94 | self.peaks = [a, a+67] |
---|
| 95 | |
---|
| 96 | def tearDown(self): |
---|
| 97 | fpeaks = [] |
---|
| 98 | for index in range(0,buf_size-slice_size): |
---|
| 99 | sliced = self.vec[index:index+slice_size] |
---|
| 100 | findex = self.o(sliced) |
---|
| 101 | if findex: |
---|
| 102 | # we found a peak |
---|
| 103 | fpeak = index - findex - delay |
---|
| 104 | #print self.peaks, index, '-', findex, '-', delay, '=', fpeak |
---|
| 105 | if not round(index - findex - delay) in self.peaks: |
---|
| 106 | self.fail('missing peak ' + str(fpeak)) |
---|
| 107 | fpeaks.append(fpeak) |
---|
| 108 | if len(fpeaks) != len(self.peaks): |
---|
| 109 | self.fail('some peaks of ' + str(self.peaks) + 'were not found, got only ' + str(fpeaks)) |
---|
| 110 | #print |
---|
| 111 | #print fpeaks, self.peaks |
---|
| 112 | |
---|
| 113 | if __name__ == '__main__': |
---|
| 114 | from unittest import main |
---|
| 115 | main() |
---|