Merge branch 'master' into awhitening
[aubio.git] / python / tests / test_mfcc.py
1 #! /usr/bin/env python
2
3 from nose2 import main
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
8
9 buf_size = 2048
10 n_filters = 40
11 n_coeffs = 13
12 samplerate = 44100
13
14
15 new_params = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
16 new_deflts = [1024, 40, 13, 44100]
17
18 class aubio_mfcc(TestCase):
19
20     def setUp(self):
21         self.o = mfcc()
22
23     def test_default_creation(self):
24         pass
25
26     def test_delete(self):
27         del self.o
28
29     @params(*new_params)
30     def test_read_only_member(self, name):
31         o = self.o
32         with self.assertRaises((TypeError, AttributeError)):
33             setattr(o, name, 0)
34
35     @params(*zip(new_params, new_deflts))
36     def test_default_param(self, name, expected):
37         """ test mfcc.{:s} = {:d} """.format(name, expected)
38         o = self.o
39         self.assertEqual( getattr(o, name), expected)
40
41 class aubio_mfcc_wrong_params(TestCase):
42
43     def test_wrong_buf_size(self):
44         with self.assertRaises(ValueError):
45             mfcc(buf_size = -1)
46
47     def test_wrong_n_filters(self):
48         with self.assertRaises(ValueError):
49             mfcc(n_filters = -1)
50
51     def test_wrong_n_coeffs(self):
52         with self.assertRaises(ValueError):
53             mfcc(n_coeffs = -1)
54
55     def test_wrong_samplerate(self):
56         with self.assertRaises(ValueError):
57             mfcc(samplerate = -1)
58
59     def test_wrong_input_size(self):
60         m = mfcc(buf_size = 1024)
61         with self.assertRaises(ValueError):
62             m(cvec(512))
63
64 class aubio_mfcc_compute(TestCase):
65
66     def test_members(self):
67
68         o = mfcc(buf_size, n_filters, n_coeffs, samplerate)
69         #assert_equal ([o.buf_size, o.method], [buf_size, method])
70
71         spec = cvec(buf_size)
72         #spec.norm[0] = 1
73         #spec.norm[1] = 1./2.
74         #print "%20s" % method, str(o(spec))
75         coeffs = o(spec)
76         self.assertEqual(coeffs.size, n_coeffs)
77         #print 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)
82         #print coeffs
83
84
85 class aubio_mfcc_all_parameters(TestCase):
86
87     @params(
88             (2048, 40, 13, 44100),
89             (1024, 40, 13, 44100),
90             (512, 40, 13, 44100),
91             (512, 40, 13, 16000),
92             (256, 40, 13, 16000),
93             (128, 40, 13, 16000),
94             (128, 40, 12, 16000),
95             (128, 40, 13, 15000),
96             (512, 40, 20, 44100),
97             (512, 40, 40, 44100),
98             (512, 40, 3, 44100),
99             (1024, 40, 20, 44100),
100             #(1024, 30, 20, 44100),
101             (1024, 40, 40, 44100),
102             (1024, 40, 3, 44100),
103             )
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)
108         spec.phas[0] = 0.2
109         for _ in range(10):
110             o(spec)
111         #print coeffs
112
113 if __name__ == '__main__':
114     main()