source: python/tests/test_mfcc.py @ 00fcc5d

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

[tests] default to pytest in mfcc

  • Property mode set to 100755
File size: 3.2 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:
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:
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
108if __name__ == '__main__':
109    import sys, pytest
110    pytest.main(sys.argv)
Note: See TracBrowser for help on using the repository browser.