[py] add tests for new filterbank methods
[aubio.git] / python / tests / test_filterbank_mel.py
1 #! /usr/bin/env python
2
3 import numpy as np
4 from numpy.testing import TestCase, assert_equal, assert_almost_equal
5
6 from aubio import fvec, cvec, filterbank, float_type
7
8 import warnings
9 warnings.filterwarnings('ignore', category=UserWarning, append=True)
10
11 class aubio_filterbank_mel_test_case(TestCase):
12
13     def test_slaney(self):
14         f = filterbank(40, 512)
15         f.set_mel_coeffs_slaney(16000)
16         a = f.get_coeffs()
17         assert_equal(np.shape (a), (40, 512/2 + 1) )
18
19     def test_other_slaney(self):
20         f = filterbank(40, 512*2)
21         f.set_mel_coeffs_slaney(44100)
22         self.assertIsInstance(f.get_coeffs(), np.ndarray)
23         #print "sum is", sum(sum(a))
24         for win_s in [256, 512, 1024, 2048, 4096]:
25             f = filterbank(40, win_s)
26             f.set_mel_coeffs_slaney(32000)
27             #print "sum is", sum(sum(a))
28             self.assertIsInstance(f.get_coeffs(), np.ndarray)
29
30     def test_triangle_freqs_zeros(self):
31         f = filterbank(9, 1024)
32         freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
33         freqs = np.array(freq_list, dtype = float_type)
34         f.set_triangle_bands(freqs, 48000)
35         assert_equal ( f(cvec(1024)), 0)
36         self.assertIsInstance(f.get_coeffs(), np.ndarray)
37
38     def test_triangle_freqs_ones(self):
39         f = filterbank(9, 1024)
40         freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
41         freqs = np.array(freq_list, dtype = float_type)
42         f.set_triangle_bands(freqs, 48000)
43         self.assertIsInstance(f.get_coeffs(), np.ndarray)
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])
49
50     def test_triangle_freqs_with_zeros(self):
51         """make sure set_triangle_bands works when list starts with 0"""
52         freq_list = [0, 40, 80]
53         freqs = np.array(freq_list, dtype = float_type)
54         f = filterbank(len(freqs)-2, 1024)
55         f.set_triangle_bands(freqs, 48000)
56         assert_equal ( f(cvec(1024)), 0)
57         self.assertIsInstance(f.get_coeffs(), np.ndarray)
58
59     def test_triangle_freqs_with_wrong_negative(self):
60         """make sure set_triangle_bands fails when list contains a negative"""
61         freq_list = [-10, 0, 80]
62         f = filterbank(len(freq_list)-2, 1024)
63         with self.assertRaises(ValueError):
64             f.set_triangle_bands(fvec(freq_list), 48000)
65
66     def test_triangle_freqs_with_wrong_ordering(self):
67         """make sure set_triangle_bands fails when list not ordered"""
68         freq_list = [0, 80, 40]
69         f = filterbank(len(freq_list)-2, 1024)
70         with self.assertRaises(ValueError):
71             f.set_triangle_bands(fvec(freq_list), 48000)
72
73     def test_triangle_freqs_with_large_freq(self):
74         """make sure set_triangle_bands warns when freq > nyquist"""
75         samplerate = 22050
76         freq_list = [0, samplerate//4, samplerate // 2 + 1]
77         f = filterbank(len(freq_list)-2, 1024)
78         # TODO add assert_warns
79         f.set_triangle_bands(fvec(freq_list), samplerate)
80
81     def test_triangle_freqs_with_not_enough_filters(self):
82         """make sure set_triangle_bands warns when not enough filters"""
83         samplerate = 22050
84         freq_list = [0, 100, 1000, 4000, 8000, 10000]
85         f = filterbank(len(freq_list)-3, 1024)
86         # TODO add assert_warns
87         f.set_triangle_bands(fvec(freq_list), samplerate)
88
89     def test_triangle_freqs_with_too_many_filters(self):
90         """make sure set_triangle_bands warns when too many filters"""
91         samplerate = 22050
92         freq_list = [0, 100, 1000, 4000, 8000, 10000]
93         f = filterbank(len(freq_list)-1, 1024)
94         # TODO add assert_warns
95         f.set_triangle_bands(fvec(freq_list), samplerate)
96
97     def test_triangle_freqs_with_double_value(self):
98         """make sure set_triangle_bands works with 2 duplicate freqs"""
99         samplerate = 22050
100         freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000]
101         f = filterbank(len(freq_list)-2, 1024)
102         # TODO add assert_warns
103         f.set_triangle_bands(fvec(freq_list), samplerate)
104
105     def test_triangle_freqs_with_triple(self):
106         """make sure set_triangle_bands works with 3 duplicate freqs"""
107         samplerate = 22050
108         freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000]
109         f = filterbank(len(freq_list)-2, 1024)
110         # TODO add assert_warns
111         f.set_triangle_bands(fvec(freq_list), samplerate)
112
113     def test_triangle_freqs_without_norm(self):
114         """make sure set_triangle_bands works without """
115         samplerate = 22050
116         freq_list = fvec([0, 100, 1000, 10000])
117         f = filterbank(len(freq_list) - 2, 1024)
118         f.set_norm(0)
119         f.set_triangle_bands(freq_list, samplerate)
120         expected = f.get_coeffs()
121         f.set_norm(1)
122         f.set_triangle_bands(fvec(freq_list), samplerate)
123         assert_almost_equal(f.get_coeffs().T,
124                 expected.T * 2. / (freq_list[2:] - freq_list[:-2]))
125
126     def test_triangle_freqs_wrong_norm(self):
127         f = filterbank(10, 1024)
128         with self.assertRaises(ValueError):
129             f.set_norm(-1)
130
131     def test_triangle_freqs_with_power(self):
132         f = filterbank(9, 1024)
133         freqs = fvec([40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000,
134             24000])
135         f.set_power(2)
136         f.set_triangle_bands(freqs, 48000)
137         spec = cvec(1024)
138         spec.norm[:] = .1
139         expected = fvec([0.02070313, 0.02138672, 0.02127604, 0.02135417,
140             0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345])
141         expected /= 100.
142         assert_almost_equal(f(spec), expected)
143
144     def test_mel_coeffs(self):
145         f = filterbank(40, 1024)
146         f.set_mel_coeffs(44100, 0, 44100 / 2)
147
148     def test_zero_fmax(self):
149         f = filterbank(40, 1024)
150         f.set_mel_coeffs(44100, 0, 0)
151
152     def test_wrong_mel_coeffs(self):
153         f = filterbank(40, 1024)
154         with self.assertRaises(ValueError):
155             f.set_mel_coeffs_slaney(0)
156         with self.assertRaises(ValueError):
157             f.set_mel_coeffs(44100, 0, -44100 / 2)
158         with self.assertRaises(ValueError):
159             f.set_mel_coeffs(44100, -0.1, 44100 / 2)
160         with self.assertRaises(ValueError):
161             f.set_mel_coeffs(-44100, 0.1, 44100 / 2)
162         with self.assertRaises(ValueError):
163             f.set_mel_coeffs_htk(-1, 0, 0)
164
165     def test_mel_coeffs_htk(self):
166         f = filterbank(40, 1024)
167         f.set_mel_coeffs_htk(44100, 0, 44100 / 2)
168
169
170 if __name__ == '__main__':
171     import nose2
172     nose2.main()