4df734278a7402ec8853fec26789ca1b26e18fa9
[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 cvec, filterbank, float_type
7
8 class aubio_filterbank_mel_test_case(TestCase):
9
10     def test_slaney(self):
11         f = filterbank(40, 512)
12         f.set_mel_coeffs_slaney(16000)
13         a = f.get_coeffs()
14         assert_equal(np.shape (a), (40, 512/2 + 1) )
15
16     def test_other_slaney(self):
17         f = filterbank(40, 512*2)
18         f.set_mel_coeffs_slaney(44100)
19         self.assertIsInstance(f.get_coeffs(), np.ndarray)
20         #print "sum is", sum(sum(a))
21         for win_s in [256, 512, 1024, 2048, 4096]:
22             f = filterbank(40, win_s)
23             f.set_mel_coeffs_slaney(32000)
24             #print "sum is", sum(sum(a))
25             self.assertIsInstance(f.get_coeffs(), np.ndarray)
26
27     def test_triangle_freqs_zeros(self):
28         f = filterbank(9, 1024)
29         freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
30         freqs = np.array(freq_list, dtype = float_type)
31         f.set_triangle_bands(freqs, 48000)
32         assert_equal ( f(cvec(1024)), 0)
33         self.assertIsInstance(f.get_coeffs(), np.ndarray)
34
35     def test_triangle_freqs_ones(self):
36         f = filterbank(9, 1024)
37         freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
38         freqs = np.array(freq_list, dtype = float_type)
39         f.set_triangle_bands(freqs, 48000)
40         self.assertIsInstance(f.get_coeffs(), np.ndarray)
41         spec = cvec(1024)
42         spec.norm[:] = 1
43         assert_almost_equal ( f(spec),
44                 [ 0.02070313, 0.02138672, 0.02127604, 0.02135417,
45                     0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345])
46
47 if __name__ == '__main__':
48     import nose2
49     nose2.main()