6e63f3328df99fe69c3f3e0f2b65fbc5096083cb
[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 class aubio_mfcc_compute(TestCase):
60
61     def test_members(self):
62
63         o = mfcc(buf_size, n_filters, n_coeffs, samplerate)
64         #assert_equal ([o.buf_size, o.method], [buf_size, method])
65
66         spec = cvec(buf_size)
67         #spec.norm[0] = 1
68         #spec.norm[1] = 1./2.
69         #print "%20s" % method, str(o(spec))
70         coeffs = o(spec)
71         self.assertEqual(coeffs.size, n_coeffs)
72         #print coeffs
73         spec.norm = random.random_sample((len(spec.norm),)).astype(float_type)
74         spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
75         #print "%20s" % method, str(o(spec))
76         self.assertEqual(count_nonzero(o(spec) != 0.), n_coeffs)
77         #print coeffs
78
79
80 class aubio_mfcc_all_parameters(TestCase):
81
82     @params(
83             (2048, 40, 13, 44100),
84             (1024, 40, 13, 44100),
85             (512, 40, 13, 44100),
86             (512, 40, 13, 16000),
87             (256, 40, 13, 16000),
88             (128, 40, 13, 16000),
89             (128, 40, 12, 16000),
90             (128, 40, 13, 15000),
91             (512, 40, 20, 44100),
92             (512, 40, 40, 44100),
93             (512, 40, 3, 44100),
94             (1024, 40, 20, 44100),
95             #(1024, 30, 20, 44100),
96             (1024, 40, 40, 44100),
97             (1024, 40, 3, 44100),
98             )
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 if __name__ == '__main__':
109     main()