4 from nose2.tools import params
5 from numpy import random, count_nonzero
6 from numpy.testing import TestCase
7 from aubio import mfcc, cvec, float_type
15 new_params = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
16 new_deflts = [1024, 40, 13, 44100]
18 class aubio_mfcc(TestCase):
23 def test_default_creation(self):
26 def test_delete(self):
30 def test_read_only_member(self, name):
32 with self.assertRaises((TypeError, AttributeError)):
35 @params(*zip(new_params, new_deflts))
36 def test_default_param(self, name, expected):
37 """ test mfcc.{:s} = {:d} """.format(name, expected)
39 self.assertEqual( getattr(o, name), expected)
41 class aubio_mfcc_wrong_params(TestCase):
43 def test_wrong_buf_size(self):
44 with self.assertRaises(ValueError):
47 def test_wrong_n_filters(self):
48 with self.assertRaises(ValueError):
51 def test_wrong_n_coeffs(self):
52 with self.assertRaises(ValueError):
55 def test_wrong_samplerate(self):
56 with self.assertRaises(ValueError):
59 def test_wrong_input_size(self):
60 m = mfcc(buf_size = 1024)
61 with self.assertRaises(ValueError):
64 class aubio_mfcc_compute(TestCase):
66 def test_members(self):
68 o = mfcc(buf_size, n_filters, n_coeffs, samplerate)
69 #assert_equal ([o.buf_size, o.method], [buf_size, method])
74 #print "%20s" % method, str(o(spec))
76 self.assertEqual(coeffs.size, n_coeffs)
78 spec.norm = random.random_sample((len(spec.norm),)).astype(float_type)
79 spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
80 #print "%20s" % method, str(o(spec))
81 self.assertEqual(count_nonzero(o(spec) != 0.), n_coeffs)
85 class aubio_mfcc_all_parameters(TestCase):
88 (2048, 40, 13, 44100),
89 (1024, 40, 13, 44100),
99 (1024, 40, 20, 44100),
100 #(1024, 30, 20, 44100),
101 (1024, 40, 40, 44100),
102 (1024, 40, 3, 44100),
104 def test_run_with_params(self, buf_size, n_filters, n_coeffs, samplerate):
105 " check mfcc can run with reasonable parameters "
106 o = mfcc(buf_size, n_filters, n_coeffs, samplerate)
107 spec = cvec(buf_size)
114 class aubio_mfcc_fb_params(TestCase):
116 def test_set_scale(self):
117 buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
118 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
122 def test_set_power(self):
123 buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
124 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
128 def test_set_mel_coeffs(self):
129 buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
130 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
131 m.set_mel_coeffs(0., samplerate/2.)
134 def test_set_mel_coeffs_htk(self):
135 buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
136 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
137 m.set_mel_coeffs_htk(0., samplerate/2.)
140 def test_set_mel_coeffs_slaney(self):
141 buf_size, n_filters, n_coeffs, samplerate = 512, 40, 10, 16000
142 m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
143 m.set_mel_coeffs_slaney(samplerate)
145 assert m.get_power() == 1
146 assert m.get_scale() == 1
148 if __name__ == '__main__':