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
14 new_params = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
15 new_deflts = [1024, 40, 13, 44100]
17 class Test_aubio_mfcc(object):
21 @parametrize(members_args, new_params)
22 def test_read_only_member(self, name):
24 with assert_raises((TypeError, AttributeError)):
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)
31 assert getattr(o, name) == expected
33 class aubio_mfcc_wrong_params(TestCase):
35 def test_wrong_buf_size(self):
36 with self.assertRaises(ValueError):
39 def test_wrong_n_filters(self):
40 with self.assertRaises(ValueError):
43 def test_wrong_n_coeffs(self):
44 with self.assertRaises(ValueError):
47 def test_wrong_samplerate(self):
48 with self.assertRaises(ValueError):
51 def test_wrong_input_size(self):
52 m = mfcc(buf_size = 1024)
53 with self.assertRaises(ValueError):
56 class aubio_mfcc_compute(TestCase):
58 def test_members(self):
60 o = mfcc(buf_size, n_filters, n_coeffs, samplerate)
61 #assert_equal ([o.buf_size, o.method], [buf_size, method])
66 #print "%20s" % method, str(o(spec))
68 self.assertEqual(coeffs.size, n_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)
77 class Test_aubio_mfcc_all_parameters(object):
80 (2048, 40, 13, 44100),
81 (1024, 40, 13, 44100),
91 (1024, 40, 20, 44100),
92 #(1024, 30, 20, 44100),
93 (1024, 40, 40, 44100),
96 run_args = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
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)
109 class aubio_mfcc_fb_params(TestCase):
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)
117 def test_set_power(self):
118 buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
119 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
123 def test_set_mel_coeffs(self):
124 buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
125 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
126 m.set_mel_coeffs(0., samplerate/2.)
129 def test_set_mel_coeffs_htk(self):
130 buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
131 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
132 m.set_mel_coeffs_htk(0., samplerate/2.)
135 def test_set_mel_coeffs_slaney(self):
136 buf_size, n_filters, n_coeffs, samplerate = 512, 40, 10, 16000
137 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
138 m.set_mel_coeffs_slaney()
140 assert m.get_power() == 1
141 assert m.get_scale() == 1
143 if __name__ == '__main__':
144 from _tools import run_module_suite