[75e715f] | 1 | #! /usr/bin/env python |
---|
[5e3ed60] | 2 | |
---|
[e116e19] | 3 | import numpy as np |
---|
| 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
---|
[67024ff] | 5 | from _tools import assert_warns |
---|
[e116e19] | 6 | |
---|
[e33d960] | 7 | from aubio import fvec, cvec, filterbank, float_type |
---|
[5e3ed60] | 8 | |
---|
| 9 | class aubio_filterbank_mel_test_case(TestCase): |
---|
| 10 | |
---|
[0b6d23d] | 11 | def test_slaney(self): |
---|
| 12 | f = filterbank(40, 512) |
---|
| 13 | f.set_mel_coeffs_slaney(16000) |
---|
| 14 | a = f.get_coeffs() |
---|
[e116e19] | 15 | assert_equal(np.shape (a), (40, 512/2 + 1) ) |
---|
[0b6d23d] | 16 | |
---|
| 17 | def test_other_slaney(self): |
---|
| 18 | f = filterbank(40, 512*2) |
---|
| 19 | f.set_mel_coeffs_slaney(44100) |
---|
[e116e19] | 20 | self.assertIsInstance(f.get_coeffs(), np.ndarray) |
---|
[0b6d23d] | 21 | #print "sum is", sum(sum(a)) |
---|
| 22 | for win_s in [256, 512, 1024, 2048, 4096]: |
---|
| 23 | f = filterbank(40, win_s) |
---|
| 24 | f.set_mel_coeffs_slaney(32000) |
---|
| 25 | #print "sum is", sum(sum(a)) |
---|
[e116e19] | 26 | self.assertIsInstance(f.get_coeffs(), np.ndarray) |
---|
[0b6d23d] | 27 | |
---|
| 28 | def test_triangle_freqs_zeros(self): |
---|
| 29 | f = filterbank(9, 1024) |
---|
| 30 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] |
---|
[e116e19] | 31 | freqs = np.array(freq_list, dtype = float_type) |
---|
[67024ff] | 32 | with assert_warns(UserWarning): |
---|
| 33 | f.set_triangle_bands(freqs, 48000) |
---|
[0b6d23d] | 34 | assert_equal ( f(cvec(1024)), 0) |
---|
[e116e19] | 35 | self.assertIsInstance(f.get_coeffs(), np.ndarray) |
---|
[0b6d23d] | 36 | |
---|
| 37 | def test_triangle_freqs_ones(self): |
---|
| 38 | f = filterbank(9, 1024) |
---|
| 39 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] |
---|
[e116e19] | 40 | freqs = np.array(freq_list, dtype = float_type) |
---|
[67024ff] | 41 | with assert_warns(UserWarning): |
---|
| 42 | f.set_triangle_bands(freqs, 48000) |
---|
[e116e19] | 43 | self.assertIsInstance(f.get_coeffs(), np.ndarray) |
---|
[0b6d23d] | 44 | spec = cvec(1024) |
---|
| 45 | spec.norm[:] = 1 |
---|
| 46 | assert_almost_equal ( f(spec), |
---|
| 47 | [ 0.02070313, 0.02138672, 0.02127604, 0.02135417, |
---|
| 48 | 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) |
---|
[5e3ed60] | 49 | |
---|
[67024ff] | 50 | def test_triangle_freqs_normal(self): |
---|
| 51 | """ create a filter with set_triangle_bands, normal mode """ |
---|
| 52 | f = filterbank(3, 1024) |
---|
| 53 | freq_list = [100, 1000, 10000, 15000, 20000] |
---|
| 54 | freqs = np.array(freq_list, dtype = float_type) |
---|
| 55 | f.set_triangle_bands(freqs, 44100) |
---|
| 56 | |
---|
| 57 | def test_triangle_freqs_too_long(self): |
---|
| 58 | """ create a filter with freq_list too long """ |
---|
| 59 | f = filterbank(2, 1024) |
---|
| 60 | freq_list = [100, 1000, 10000, 15000, 20000] |
---|
| 61 | freqs = np.array(freq_list, dtype = float_type) |
---|
| 62 | with assert_warns(UserWarning): |
---|
| 63 | f.set_triangle_bands(freqs, 44100) |
---|
| 64 | |
---|
| 65 | def test_triangle_freqs_too_short(self): |
---|
| 66 | """ create a filter with freq_list too short """ |
---|
| 67 | f = filterbank(4, 1024) |
---|
| 68 | freq_list = [100, 1000, 10000, 15000, 20000] |
---|
| 69 | freqs = np.array(freq_list, dtype = float_type) |
---|
| 70 | with assert_warns(UserWarning): |
---|
| 71 | f.set_triangle_bands(freqs, 44100) |
---|
| 72 | |
---|
| 73 | def test_above_nyquist(self): |
---|
| 74 | """ create a filter with freq_list too short """ |
---|
| 75 | f = filterbank(3, 1024) |
---|
| 76 | freq_list = [100, 1000, 10000, 15000, 23000] |
---|
| 77 | freqs = np.array(freq_list, dtype = float_type) |
---|
| 78 | with assert_warns(UserWarning): |
---|
| 79 | f.set_triangle_bands(freqs, 44100) |
---|
| 80 | |
---|
[98c712e] | 81 | def test_triangle_freqs_with_zeros(self): |
---|
| 82 | """make sure set_triangle_bands works when list starts with 0""" |
---|
| 83 | freq_list = [0, 40, 80] |
---|
| 84 | freqs = np.array(freq_list, dtype = float_type) |
---|
| 85 | f = filterbank(len(freqs)-2, 1024) |
---|
| 86 | f.set_triangle_bands(freqs, 48000) |
---|
| 87 | assert_equal ( f(cvec(1024)), 0) |
---|
| 88 | self.assertIsInstance(f.get_coeffs(), np.ndarray) |
---|
| 89 | |
---|
[e33d960] | 90 | def test_triangle_freqs_with_wrong_negative(self): |
---|
| 91 | """make sure set_triangle_bands fails when list contains a negative""" |
---|
| 92 | freq_list = [-10, 0, 80] |
---|
| 93 | f = filterbank(len(freq_list)-2, 1024) |
---|
| 94 | with self.assertRaises(ValueError): |
---|
| 95 | f.set_triangle_bands(fvec(freq_list), 48000) |
---|
| 96 | |
---|
| 97 | def test_triangle_freqs_with_wrong_ordering(self): |
---|
| 98 | """make sure set_triangle_bands fails when list not ordered""" |
---|
| 99 | freq_list = [0, 80, 40] |
---|
| 100 | f = filterbank(len(freq_list)-2, 1024) |
---|
| 101 | with self.assertRaises(ValueError): |
---|
| 102 | f.set_triangle_bands(fvec(freq_list), 48000) |
---|
| 103 | |
---|
| 104 | def test_triangle_freqs_with_large_freq(self): |
---|
| 105 | """make sure set_triangle_bands warns when freq > nyquist""" |
---|
| 106 | samplerate = 22050 |
---|
| 107 | freq_list = [0, samplerate//4, samplerate // 2 + 1] |
---|
| 108 | f = filterbank(len(freq_list)-2, 1024) |
---|
| 109 | # TODO add assert_warns |
---|
| 110 | f.set_triangle_bands(fvec(freq_list), samplerate) |
---|
| 111 | |
---|
| 112 | def test_triangle_freqs_with_not_enough_filters(self): |
---|
| 113 | """make sure set_triangle_bands warns when not enough filters""" |
---|
| 114 | samplerate = 22050 |
---|
| 115 | freq_list = [0, 100, 1000, 4000, 8000, 10000] |
---|
| 116 | f = filterbank(len(freq_list)-3, 1024) |
---|
| 117 | # TODO add assert_warns |
---|
| 118 | f.set_triangle_bands(fvec(freq_list), samplerate) |
---|
| 119 | |
---|
| 120 | def test_triangle_freqs_with_too_many_filters(self): |
---|
| 121 | """make sure set_triangle_bands warns when too many filters""" |
---|
| 122 | samplerate = 22050 |
---|
| 123 | freq_list = [0, 100, 1000, 4000, 8000, 10000] |
---|
| 124 | f = filterbank(len(freq_list)-1, 1024) |
---|
| 125 | # TODO add assert_warns |
---|
| 126 | f.set_triangle_bands(fvec(freq_list), samplerate) |
---|
| 127 | |
---|
[693de50] | 128 | def test_triangle_freqs_with_double_value(self): |
---|
| 129 | """make sure set_triangle_bands works with 2 duplicate freqs""" |
---|
| 130 | samplerate = 22050 |
---|
| 131 | freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000] |
---|
| 132 | f = filterbank(len(freq_list)-2, 1024) |
---|
| 133 | # TODO add assert_warns |
---|
| 134 | f.set_triangle_bands(fvec(freq_list), samplerate) |
---|
| 135 | |
---|
| 136 | def test_triangle_freqs_with_triple(self): |
---|
| 137 | """make sure set_triangle_bands works with 3 duplicate freqs""" |
---|
| 138 | samplerate = 22050 |
---|
| 139 | freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000] |
---|
| 140 | f = filterbank(len(freq_list)-2, 1024) |
---|
| 141 | # TODO add assert_warns |
---|
| 142 | f.set_triangle_bands(fvec(freq_list), samplerate) |
---|
| 143 | |
---|
[893e699] | 144 | |
---|
[5e3ed60] | 145 | if __name__ == '__main__': |
---|
[319edae] | 146 | from unittest import main |
---|
| 147 | main() |
---|