1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy.testing import TestCase, run_module_suite |
---|
4 | from numpy.testing import assert_equal, assert_almost_equal |
---|
5 | from numpy import random |
---|
6 | from math import pi |
---|
7 | from numpy import array |
---|
8 | from aubio import cvec, filterbank |
---|
9 | |
---|
10 | def array_from_text_file(filename, dtype = 'float'): |
---|
11 | return array([line.split() for line in open(filename).readlines()], |
---|
12 | dtype = dtype) |
---|
13 | |
---|
14 | class aubio_filterbank_test_case(TestCase): |
---|
15 | |
---|
16 | def test_members(self): |
---|
17 | f = filterbank(40, 512) |
---|
18 | assert_equal ([f.n_filters, f.win_s], [40, 512]) |
---|
19 | |
---|
20 | def test_set_coeffs(self): |
---|
21 | f = filterbank(40, 512) |
---|
22 | r = random.random([40, 512 / 2 + 1]).astype('float32') |
---|
23 | f.set_coeffs(r) |
---|
24 | assert_equal (r, f.get_coeffs()) |
---|
25 | |
---|
26 | def test_phase(self): |
---|
27 | f = filterbank(40, 512) |
---|
28 | c = cvec(512) |
---|
29 | c.phas[:] = pi |
---|
30 | assert_equal( f(c), 0); |
---|
31 | |
---|
32 | def test_norm(self): |
---|
33 | f = filterbank(40, 512) |
---|
34 | c = cvec(512) |
---|
35 | c.norm[:] = 1 |
---|
36 | assert_equal( f(c), 0); |
---|
37 | |
---|
38 | def test_random_norm(self): |
---|
39 | f = filterbank(40, 512) |
---|
40 | c = cvec(512) |
---|
41 | c.norm[:] = random.random((512 / 2 + 1,)).astype('float32') |
---|
42 | assert_equal( f(c), 0) |
---|
43 | |
---|
44 | def test_random_coeffs(self): |
---|
45 | f = filterbank(40, 512) |
---|
46 | c = cvec(512) |
---|
47 | r = random.random([40, 512 / 2 + 1]).astype('float32') |
---|
48 | r /= r.sum() |
---|
49 | f.set_coeffs(r) |
---|
50 | c.norm[:] = random.random((512 / 2 + 1,)).astype('float32') |
---|
51 | assert_equal ( f(c) < 1., True ) |
---|
52 | assert_equal ( f(c) > 0., True ) |
---|
53 | |
---|
54 | def test_mfcc_coeffs(self): |
---|
55 | f = filterbank(40, 512) |
---|
56 | c = cvec(512) |
---|
57 | f.set_mel_coeffs_slaney(44100) |
---|
58 | c.norm[:] = random.random((512 / 2 + 1,)).astype('float32') |
---|
59 | assert_equal ( f(c) < 1., True ) |
---|
60 | assert_equal ( f(c) > 0., True ) |
---|
61 | |
---|
62 | def test_mfcc_coeffs_16000(self): |
---|
63 | expected = array_from_text_file('filterbank_mfcc_16000_512.expected') |
---|
64 | f = filterbank(40, 512) |
---|
65 | f.set_mel_coeffs_slaney(16000) |
---|
66 | assert_almost_equal ( expected, f.get_coeffs() ) |
---|
67 | |
---|
68 | if __name__ == '__main__': |
---|
69 | from unittest import main |
---|
70 | main() |
---|
71 | |
---|