wscript :
[aubio.git] / python / tests / test_filterbank.py
1 #! /usr/bin/env python
2
3 from unittest import main
4 from numpy.testing import TestCase
5 from numpy.testing import assert_equal, assert_almost_equal
6 import numpy as np
7 from aubio import cvec, filterbank, float_type
8 from .utils import array_from_text_file
9
10 class aubio_filterbank_test_case(TestCase):
11
12     def test_members(self):
13         f = filterbank(40, 512)
14         assert_equal ([f.n_filters, f.win_s], [40, 512])
15
16     def test_set_coeffs(self):
17         f = filterbank(40, 512)
18         r = np.random.random([40, int(512 / 2) + 1]).astype(float_type)
19         f.set_coeffs(r)
20         assert_equal (r, f.get_coeffs())
21
22     def test_phase(self):
23         f = filterbank(40, 512)
24         c = cvec(512)
25         c.phas[:] = np.pi
26         assert_equal( f(c), 0);
27
28     def test_norm(self):
29         f = filterbank(40, 512)
30         c = cvec(512)
31         c.norm[:] = 1
32         assert_equal( f(c), 0);
33
34     def test_random_norm(self):
35         f = filterbank(40, 512)
36         c = cvec(512)
37         c.norm[:] = np.random.random((int(512 / 2) + 1,)).astype(float_type)
38         assert_equal( f(c), 0)
39
40     def test_random_coeffs(self):
41         win_s = 128
42         f = filterbank(40, win_s)
43         c = cvec(win_s)
44         r = np.random.random([40, int(win_s / 2) + 1]).astype(float_type)
45         r /= r.sum()
46         f.set_coeffs(r)
47         c.norm[:] = np.random.random((int(win_s / 2) + 1,)).astype(float_type)
48         assert_equal ( f(c) < 1., True )
49         assert_equal ( f(c) > 0., True )
50
51     def test_mfcc_coeffs(self):
52         f = filterbank(40, 512)
53         c = cvec(512)
54         f.set_mel_coeffs_slaney(44100)
55         c.norm[:] = np.random.random((int(512 / 2) + 1,)).astype(float_type)
56         assert_equal ( f(c) < 1., True )
57         assert_equal ( f(c) > 0., True )
58
59     def test_mfcc_coeffs_16000(self):
60         expected = array_from_text_file('filterbank_mfcc_16000_512.expected')
61         f = filterbank(40, 512)
62         f.set_mel_coeffs_slaney(16000)
63         assert_almost_equal ( expected, f.get_coeffs() )
64
65 class aubio_filterbank_wrong_values(TestCase):
66
67     def test_negative_window(self):
68         self.assertRaises(ValueError, filterbank, 40, -20)
69
70     def test_negative_filters(self):
71         self.assertRaises(ValueError, filterbank, -40, 1024)
72
73     def test_filterbank_long_cvec(self):
74         f = filterbank(40, 512)
75         with self.assertRaises(ValueError):
76             f(cvec(1024))
77
78     def test_filterbank_short_cvec(self):
79         f = filterbank(40, 512)
80         with self.assertRaises(ValueError):
81             f(cvec(256))
82
83 if __name__ == '__main__':
84     main()