[75e715f] | 1 | #! /usr/bin/env python |
---|
[5e3ed60] | 2 | |
---|
[2a14820] | 3 | import numpy as np |
---|
[e116e19] | 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
---|
| 5 | |
---|
[c4b2183] | 6 | from aubio import cvec, filterbank, float_type |
---|
[a1bf01d] | 7 | from .utils import array_from_text_file |
---|
[71ef9c2] | 8 | |
---|
[5e3ed60] | 9 | class aubio_filterbank_test_case(TestCase): |
---|
[0536612] | 10 | |
---|
[cdfad0c6] | 11 | def test_members(self): |
---|
| 12 | f = filterbank(40, 512) |
---|
| 13 | assert_equal ([f.n_filters, f.win_s], [40, 512]) |
---|
[0536612] | 14 | |
---|
[cdfad0c6] | 15 | def test_set_coeffs(self): |
---|
| 16 | f = filterbank(40, 512) |
---|
| 17 | r = np.random.random([40, int(512 / 2) + 1]).astype(float_type) |
---|
| 18 | f.set_coeffs(r) |
---|
| 19 | assert_equal (r, f.get_coeffs()) |
---|
[0536612] | 20 | |
---|
[cdfad0c6] | 21 | def test_phase(self): |
---|
| 22 | f = filterbank(40, 512) |
---|
| 23 | c = cvec(512) |
---|
| 24 | c.phas[:] = np.pi |
---|
| 25 | assert_equal( f(c), 0); |
---|
[ac9eea7] | 26 | |
---|
[cdfad0c6] | 27 | def test_norm(self): |
---|
| 28 | f = filterbank(40, 512) |
---|
| 29 | c = cvec(512) |
---|
| 30 | c.norm[:] = 1 |
---|
| 31 | assert_equal( f(c), 0); |
---|
[ac9eea7] | 32 | |
---|
[cdfad0c6] | 33 | def test_random_norm(self): |
---|
| 34 | f = filterbank(40, 512) |
---|
| 35 | c = cvec(512) |
---|
| 36 | c.norm[:] = np.random.random((int(512 / 2) + 1,)).astype(float_type) |
---|
| 37 | assert_equal( f(c), 0) |
---|
[ac9eea7] | 38 | |
---|
[cdfad0c6] | 39 | def test_random_coeffs(self): |
---|
| 40 | win_s = 128 |
---|
| 41 | f = filterbank(40, win_s) |
---|
| 42 | c = cvec(win_s) |
---|
| 43 | r = np.random.random([40, int(win_s / 2) + 1]).astype(float_type) |
---|
| 44 | r /= r.sum() |
---|
| 45 | f.set_coeffs(r) |
---|
| 46 | c.norm[:] = np.random.random((int(win_s / 2) + 1,)).astype(float_type) |
---|
| 47 | assert_equal ( f(c) < 1., True ) |
---|
| 48 | assert_equal ( f(c) > 0., True ) |
---|
[ac9eea7] | 49 | |
---|
[cdfad0c6] | 50 | def test_mfcc_coeffs(self): |
---|
| 51 | f = filterbank(40, 512) |
---|
| 52 | c = cvec(512) |
---|
| 53 | f.set_mel_coeffs_slaney(44100) |
---|
| 54 | c.norm[:] = np.random.random((int(512 / 2) + 1,)).astype(float_type) |
---|
| 55 | assert_equal ( f(c) < 1., True ) |
---|
| 56 | assert_equal ( f(c) > 0., True ) |
---|
[ac9eea7] | 57 | |
---|
[cdfad0c6] | 58 | def test_mfcc_coeffs_16000(self): |
---|
| 59 | expected = array_from_text_file('filterbank_mfcc_16000_512.expected') |
---|
| 60 | f = filterbank(40, 512) |
---|
| 61 | f.set_mel_coeffs_slaney(16000) |
---|
| 62 | assert_almost_equal ( expected, f.get_coeffs() ) |
---|
[71ef9c2] | 63 | |
---|
[e116e19] | 64 | def test_mfcc_coeffs_get_coeffs(self): |
---|
| 65 | f = filterbank(40, 512) |
---|
| 66 | coeffs = f.get_coeffs() |
---|
| 67 | self.assertIsInstance(coeffs, np.ndarray) |
---|
| 68 | assert_equal (coeffs, 0) |
---|
| 69 | assert_equal (np.shape(coeffs), (40, 512 / 2 + 1)) |
---|
| 70 | |
---|
[ed938a2] | 71 | class aubio_filterbank_wrong_values(TestCase): |
---|
| 72 | |
---|
| 73 | def test_negative_window(self): |
---|
| 74 | self.assertRaises(ValueError, filterbank, 40, -20) |
---|
| 75 | |
---|
| 76 | def test_negative_filters(self): |
---|
[3865924] | 77 | self.assertRaises(ValueError, filterbank, -40, 1024) |
---|
[ed938a2] | 78 | |
---|
[de0a492] | 79 | def test_filterbank_long_cvec(self): |
---|
| 80 | f = filterbank(40, 512) |
---|
| 81 | with self.assertRaises(ValueError): |
---|
| 82 | f(cvec(1024)) |
---|
| 83 | |
---|
| 84 | def test_filterbank_short_cvec(self): |
---|
| 85 | f = filterbank(40, 512) |
---|
| 86 | with self.assertRaises(ValueError): |
---|
| 87 | f(cvec(256)) |
---|
| 88 | |
---|
[0536612] | 89 | if __name__ == '__main__': |
---|
[319edae] | 90 | from unittest import main |
---|
| 91 | main() |
---|