File: test_mfcc.py

package info (click to toggle)
aubio 0.4.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: python: 20,447; ansic: 20,127; makefile: 348; sh: 232
file content (147 lines) | stat: -rwxr-xr-x 4,637 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/env python

from _tools import parametrize, assert_raises
from numpy import random, count_nonzero
from numpy.testing import TestCase
from aubio import mfcc, cvec, float_type

buf_size = 2048
n_filters = 40
n_coeffs = 13
samplerate = 44100


new_params = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
new_deflts = [1024, 40, 13, 44100]

class Test_aubio_mfcc(object):

    members_args = 'name'

    @parametrize(members_args, new_params)
    def test_read_only_member(self, name):
        o = mfcc()
        with assert_raises((TypeError, AttributeError)):
            setattr(o, name, 0)

    @parametrize('name, expected', zip(new_params, new_deflts))
    def test_default_param(self, name, expected):
        """ test mfcc.{:s} = {:d} """.format(name, expected)
        o = mfcc()
        assert getattr(o, name) == expected

class aubio_mfcc_wrong_params(TestCase):

    def test_wrong_buf_size(self):
        with self.assertRaises(ValueError):
            mfcc(buf_size = -1)

    def test_wrong_n_filters(self):
        with self.assertRaises(ValueError):
            mfcc(n_filters = -1)

    def test_wrong_n_coeffs(self):
        with self.assertRaises(ValueError):
            mfcc(n_coeffs = -1)

    def test_wrong_samplerate(self):
        with self.assertRaises(ValueError):
            mfcc(samplerate = -1)

    def test_wrong_input_size(self):
        m = mfcc(buf_size = 1024)
        with self.assertRaises(ValueError):
            m(cvec(512))

class aubio_mfcc_compute(TestCase):

    def test_members(self):

        o = mfcc(buf_size, n_filters, n_coeffs, samplerate)
        #assert_equal ([o.buf_size, o.method], [buf_size, method])

        spec = cvec(buf_size)
        #spec.norm[0] = 1
        #spec.norm[1] = 1./2.
        #print "%20s" % method, str(o(spec))
        coeffs = o(spec)
        self.assertEqual(coeffs.size, n_coeffs)
        #print coeffs
        spec.norm = random.random_sample((len(spec.norm),)).astype(float_type)
        spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
        #print "%20s" % method, str(o(spec))
        self.assertEqual(count_nonzero(o(spec) != 0.), n_coeffs)
        #print coeffs


class Test_aubio_mfcc_all_parameters(object):

    run_values = [
            (2048, 40, 13, 44100),
            (1024, 40, 13, 44100),
            (512, 40, 13, 44100),
            (512, 40, 13, 16000),
            (256, 40, 13, 16000),
            (128, 40, 13, 16000),
            (128, 40, 12, 16000),
            (128, 40, 13, 15000),
            (512, 40, 20, 44100),
            (512, 40, 40, 44100),
            (512, 40, 3, 44100),
            (1024, 40, 20, 44100),
            #(1024, 30, 20, 44100),
            (1024, 40, 40, 44100),
            (1024, 40, 3, 44100),
            ]
    run_args = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']

    @parametrize(run_args, run_values)
    def test_run_with_params(self, buf_size, n_filters, n_coeffs, samplerate):
        " check mfcc can run with reasonable parameters "
        o = mfcc(buf_size, n_filters, n_coeffs, samplerate)
        spec = cvec(buf_size)
        spec.phas[0] = 0.2
        for _ in range(10):
            o(spec)
        #print coeffs


class aubio_mfcc_fb_params(TestCase):

    def test_set_scale(self):
        buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
        m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
        m.set_scale(10.5)
        assert m.get_scale() == 10.5
        m(cvec(buf_size))

    def test_set_power(self):
        buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
        m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
        m.set_power(2.5)
        assert m.get_power() == 2.5
        m(cvec(buf_size))

    def test_set_mel_coeffs(self):
        buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
        m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
        m.set_mel_coeffs(0., samplerate/2.)
        m(cvec(buf_size))

    def test_set_mel_coeffs_htk(self):
        buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000
        m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
        m.set_mel_coeffs_htk(0., samplerate/2.)
        m(cvec(buf_size))

    def test_set_mel_coeffs_slaney(self):
        buf_size, n_filters, n_coeffs, samplerate = 512, 40, 10, 16000
        m = mfcc(buf_size, n_filters, n_coeffs, samplerate)
        m.set_mel_coeffs_slaney()
        m(cvec(buf_size))
        assert m.get_power() == 1
        assert m.get_scale() == 1

if __name__ == '__main__':
    from _tools import run_module_suite
    run_module_suite()