source: python/tests/test_mfcc.py @ 51b5f9c

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 51b5f9c was c1ba75b, checked in by Paul Brossier <piem@piem.org>, 5 years ago

Merge branch 'master' into feature/pytest

  • Property mode set to 100755
File size: 4.5 KB
Line 
1#! /usr/bin/env python
2
3from _tools import parametrize, assert_raises
4from numpy import random, count_nonzero
5from numpy.testing import TestCase
6from aubio import mfcc, cvec, float_type
7
8buf_size = 2048
9n_filters = 40
10n_coeffs = 13
11samplerate = 44100
12
13
14new_params = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
15new_deflts = [1024, 40, 13, 44100]
16
17class 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
33class 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
56class 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
77class 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
109class 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
145if __name__ == '__main__':
146    from _tools import run_module_suite
147    run_module_suite()
Note: See TracBrowser for help on using the repository browser.