1 | |
---|
2 | import unittest |
---|
3 | from aubio.aubiowrapper import * |
---|
4 | |
---|
5 | win_size = 2048 |
---|
6 | channels = 1 |
---|
7 | n_filters = 40 |
---|
8 | samplerate = 44100 |
---|
9 | zerodb = -96.015602111816406 |
---|
10 | |
---|
11 | class filterbank_test_case(unittest.TestCase): |
---|
12 | |
---|
13 | def setUp(self): |
---|
14 | self.input_spectrum = new_cvec(win_size,channels) |
---|
15 | self.output_banks = new_fvec(n_filters,channels) |
---|
16 | self.filterbank = new_aubio_filterbank(n_filters,win_size) |
---|
17 | |
---|
18 | def tearDown(self): |
---|
19 | del_aubio_filterbank(self.filterbank) |
---|
20 | del_cvec(self.input_spectrum) |
---|
21 | del_fvec(self.output_banks) |
---|
22 | |
---|
23 | def testzeroes(self): |
---|
24 | """ check the output of the filterbank is -96 when input spectrum is 0 """ |
---|
25 | aubio_filterbank_do(self.filterbank,self.input_spectrum, |
---|
26 | self.output_banks) |
---|
27 | for channel in range(channels): |
---|
28 | for index in range(n_filters): |
---|
29 | self.assertEqual(fvec_read_sample(self.output_banks,channel,index), zerodb) |
---|
30 | |
---|
31 | def testphase(self): |
---|
32 | """ check the output of the filterbank is -96 when input phase is pi """ |
---|
33 | from math import pi |
---|
34 | for channel in range(channels): |
---|
35 | for index in range(win_size/2+1): |
---|
36 | cvec_write_phas(self.input_spectrum,pi,channel,index) |
---|
37 | aubio_filterbank_do(self.filterbank,self.input_spectrum, |
---|
38 | self.output_banks) |
---|
39 | for channel in range(channels): |
---|
40 | for index in range(n_filters): |
---|
41 | self.assertEqual(fvec_read_sample(self.output_banks,channel,index), zerodb) |
---|
42 | |
---|
43 | def testones(self): |
---|
44 | """ check the output of the filterbank is -96 when input norm is 1 |
---|
45 | (the filterbank is currently set to 0). |
---|
46 | """ |
---|
47 | for channel in range(channels): |
---|
48 | for index in range(win_size/2+1): |
---|
49 | cvec_write_norm(self.input_spectrum,1.,channel,index) |
---|
50 | aubio_filterbank_do(self.filterbank,self.input_spectrum, |
---|
51 | self.output_banks) |
---|
52 | for channel in range(channels): |
---|
53 | for index in range(n_filters): |
---|
54 | self.assertEqual(fvec_read_sample(self.output_banks,channel,index), zerodb) |
---|
55 | |
---|
56 | def testmfcc_zeroes(self): |
---|
57 | """ check the mfcc filterbank output is -96 when input is 0 """ |
---|
58 | self.filterbank = new_aubio_filterbank_mfcc(n_filters, win_size, samplerate, 0., samplerate) |
---|
59 | aubio_filterbank_do(self.filterbank,self.input_spectrum, |
---|
60 | self.output_banks) |
---|
61 | for channel in range(channels): |
---|
62 | for index in range(n_filters): |
---|
63 | self.assertEqual(fvec_read_sample(self.output_banks,channel,index), zerodb) |
---|
64 | |
---|
65 | def testmfcc_phasepi(self): |
---|
66 | """ check the mfcc filterbank output is -96 when input phase is pi """ |
---|
67 | self.filterbank = new_aubio_filterbank_mfcc(n_filters, win_size, samplerate, 0., samplerate) |
---|
68 | from math import pi |
---|
69 | for channel in range(channels): |
---|
70 | for index in range(win_size/2+1): |
---|
71 | cvec_write_phas(self.input_spectrum,pi,channel,index) |
---|
72 | aubio_filterbank_do(self.filterbank,self.input_spectrum, |
---|
73 | self.output_banks) |
---|
74 | for channel in range(channels): |
---|
75 | for index in range(n_filters): |
---|
76 | self.assertEqual(fvec_read_sample(self.output_banks,channel,index), zerodb) |
---|
77 | |
---|
78 | def testmfcc_ones(self): |
---|
79 | """ check setting the input spectrum to 1 gives something between -3. and -4. """ |
---|
80 | self.filterbank = new_aubio_filterbank_mfcc(n_filters, win_size, samplerate, 0., samplerate) |
---|
81 | for channel in range(channels): |
---|
82 | for index in range(win_size/2+1): |
---|
83 | cvec_write_norm(self.input_spectrum,1.,channel,index) |
---|
84 | aubio_filterbank_do(self.filterbank,self.input_spectrum, |
---|
85 | self.output_banks) |
---|
86 | for channel in range(channels): |
---|
87 | for index in range(n_filters): |
---|
88 | val = fvec_read_sample(self.output_banks,channel,index) |
---|
89 | self.failIf(val > -2.5 , val ) |
---|
90 | self.failIf(val < -4. , val ) |
---|
91 | |
---|
92 | def testmfcc_channels(self): |
---|
93 | """ check the values of each filters in the mfcc filterbank """ |
---|
94 | self.filterbank = new_aubio_filterbank_mfcc(n_filters, win_size, samplerate, |
---|
95 | 0., samplerate) |
---|
96 | filterbank_mfcc = [ [float(f) for f in line.strip().split()] |
---|
97 | for line in open('filterbank_mfcc.txt').readlines()] |
---|
98 | for channel in range(n_filters): |
---|
99 | vec = aubio_filterbank_getchannel(self.filterbank,channel) |
---|
100 | for index in range(win_size): |
---|
101 | self.assertAlmostEqual(fvec_read_sample(vec,0,index), |
---|
102 | filterbank_mfcc[channel][index]) |
---|
103 | aubio_filterbank_do(self.filterbank,self.input_spectrum, |
---|
104 | self.output_banks) |
---|
105 | for channel in range(channels): |
---|
106 | for index in range(n_filters): |
---|
107 | self.assertEqual(fvec_read_sample(self.output_banks,channel,index), zerodb) |
---|
108 | |
---|
109 | if __name__ == '__main__': |
---|
110 | unittest.main() |
---|