1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from _tools import parametrize, assert_raises |
---|
4 | from numpy import random, count_nonzero |
---|
5 | from numpy.testing import TestCase |
---|
6 | from aubio import mfcc, cvec, float_type |
---|
7 | |
---|
8 | buf_size = 2048 |
---|
9 | n_filters = 40 |
---|
10 | n_coeffs = 13 |
---|
11 | samplerate = 44100 |
---|
12 | |
---|
13 | |
---|
14 | new_params = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate'] |
---|
15 | new_deflts = [1024, 40, 13, 44100] |
---|
16 | |
---|
17 | class Test_aubio_mfcc(object): |
---|
18 | |
---|
19 | members_args = 'name' |
---|
20 | |
---|
21 | @parametrize(members_args, new_params) |
---|
22 | def test_read_only_member(self, name): |
---|
23 | o = mfcc() |
---|
24 | with assert_raises((TypeError, AttributeError)): |
---|
25 | setattr(o, name, 0) |
---|
26 | |
---|
27 | @parametrize('name, expected', zip(new_params, new_deflts)) |
---|
28 | def test_default_param(self, name, expected): |
---|
29 | """ test mfcc.{:s} = {:d} """.format(name, expected) |
---|
30 | o = mfcc() |
---|
31 | assert getattr(o, name) == expected |
---|
32 | |
---|
33 | class aubio_mfcc_wrong_params(TestCase): |
---|
34 | |
---|
35 | def test_wrong_buf_size(self): |
---|
36 | with self.assertRaises(ValueError): |
---|
37 | mfcc(buf_size = -1) |
---|
38 | |
---|
39 | def test_wrong_n_filters(self): |
---|
40 | with self.assertRaises(ValueError): |
---|
41 | mfcc(n_filters = -1) |
---|
42 | |
---|
43 | def test_wrong_n_coeffs(self): |
---|
44 | with self.assertRaises(ValueError): |
---|
45 | mfcc(n_coeffs = -1) |
---|
46 | |
---|
47 | def test_wrong_samplerate(self): |
---|
48 | with self.assertRaises(ValueError): |
---|
49 | mfcc(samplerate = -1) |
---|
50 | |
---|
51 | def test_wrong_input_size(self): |
---|
52 | m = mfcc(buf_size = 1024) |
---|
53 | with self.assertRaises(ValueError): |
---|
54 | m(cvec(512)) |
---|
55 | |
---|
56 | class aubio_mfcc_compute(TestCase): |
---|
57 | |
---|
58 | def test_members(self): |
---|
59 | |
---|
60 | o = mfcc(buf_size, n_filters, n_coeffs, samplerate) |
---|
61 | #assert_equal ([o.buf_size, o.method], [buf_size, method]) |
---|
62 | |
---|
63 | spec = cvec(buf_size) |
---|
64 | #spec.norm[0] = 1 |
---|
65 | #spec.norm[1] = 1./2. |
---|
66 | #print "%20s" % method, str(o(spec)) |
---|
67 | coeffs = o(spec) |
---|
68 | self.assertEqual(coeffs.size, n_coeffs) |
---|
69 | #print coeffs |
---|
70 | spec.norm = random.random_sample((len(spec.norm),)).astype(float_type) |
---|
71 | spec.phas = random.random_sample((len(spec.phas),)).astype(float_type) |
---|
72 | #print "%20s" % method, str(o(spec)) |
---|
73 | self.assertEqual(count_nonzero(o(spec) != 0.), n_coeffs) |
---|
74 | #print coeffs |
---|
75 | |
---|
76 | |
---|
77 | class Test_aubio_mfcc_all_parameters(object): |
---|
78 | |
---|
79 | run_values = [ |
---|
80 | (2048, 40, 13, 44100), |
---|
81 | (1024, 40, 13, 44100), |
---|
82 | (512, 40, 13, 44100), |
---|
83 | (512, 40, 13, 16000), |
---|
84 | (256, 40, 13, 16000), |
---|
85 | (128, 40, 13, 16000), |
---|
86 | (128, 40, 12, 16000), |
---|
87 | (128, 40, 13, 15000), |
---|
88 | (512, 40, 20, 44100), |
---|
89 | (512, 40, 40, 44100), |
---|
90 | (512, 40, 3, 44100), |
---|
91 | (1024, 40, 20, 44100), |
---|
92 | #(1024, 30, 20, 44100), |
---|
93 | (1024, 40, 40, 44100), |
---|
94 | (1024, 40, 3, 44100), |
---|
95 | ] |
---|
96 | run_args = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate'] |
---|
97 | |
---|
98 | @parametrize(run_args, run_values) |
---|
99 | def test_run_with_params(self, buf_size, n_filters, n_coeffs, samplerate): |
---|
100 | " check mfcc can run with reasonable parameters " |
---|
101 | o = mfcc(buf_size, n_filters, n_coeffs, samplerate) |
---|
102 | spec = cvec(buf_size) |
---|
103 | spec.phas[0] = 0.2 |
---|
104 | for _ in range(10): |
---|
105 | o(spec) |
---|
106 | #print coeffs |
---|
107 | |
---|
108 | |
---|
109 | class aubio_mfcc_fb_params(TestCase): |
---|
110 | |
---|
111 | def test_set_scale(self): |
---|
112 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 |
---|
113 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) |
---|
114 | m.set_scale(10.5) |
---|
115 | assert m.get_scale() == 10.5 |
---|
116 | m(cvec(buf_size)) |
---|
117 | |
---|
118 | def test_set_power(self): |
---|
119 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 |
---|
120 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) |
---|
121 | m.set_power(2.5) |
---|
122 | assert m.get_power() == 2.5 |
---|
123 | m(cvec(buf_size)) |
---|
124 | |
---|
125 | def test_set_mel_coeffs(self): |
---|
126 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 |
---|
127 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) |
---|
128 | m.set_mel_coeffs(0., samplerate/2.) |
---|
129 | m(cvec(buf_size)) |
---|
130 | |
---|
131 | def test_set_mel_coeffs_htk(self): |
---|
132 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 |
---|
133 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) |
---|
134 | m.set_mel_coeffs_htk(0., samplerate/2.) |
---|
135 | m(cvec(buf_size)) |
---|
136 | |
---|
137 | def test_set_mel_coeffs_slaney(self): |
---|
138 | buf_size, n_filters, n_coeffs, samplerate = 512, 40, 10, 16000 |
---|
139 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) |
---|
140 | m.set_mel_coeffs_slaney() |
---|
141 | m(cvec(buf_size)) |
---|
142 | assert m.get_power() == 1 |
---|
143 | assert m.get_scale() == 1 |
---|
144 | |
---|
145 | if __name__ == '__main__': |
---|
146 | from _tools import run_module_suite |
---|
147 | run_module_suite() |
---|