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