1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy.testing import TestCase, assert_equal, assert_array_less |
---|
4 | from aubio import fvec, cvec, pvoc, float_type |
---|
5 | from nose2.tools import params |
---|
6 | import numpy as np |
---|
7 | |
---|
8 | if float_type == 'float32': |
---|
9 | max_sq_error = 1.e-12 |
---|
10 | else: |
---|
11 | max_sq_error = 1.e-29 |
---|
12 | |
---|
13 | def create_sine(hop_s, freq, samplerate): |
---|
14 | t = np.arange(hop_s).astype(float_type) |
---|
15 | return np.sin( 2. * np.pi * freq * t / float(samplerate)) |
---|
16 | |
---|
17 | def create_noise(hop_s): |
---|
18 | return np.random.rand(hop_s).astype(float_type) * 2. - 1. |
---|
19 | |
---|
20 | class aubio_pvoc_test_case(TestCase): |
---|
21 | """ pvoc object test case """ |
---|
22 | |
---|
23 | def test_members_automatic_sizes_default(self): |
---|
24 | """ check object creation with default parameters """ |
---|
25 | f = pvoc() |
---|
26 | assert_equal ([f.win_s, f.hop_s], [1024, 512]) |
---|
27 | |
---|
28 | def test_members_unnamed_params(self): |
---|
29 | """ check object creation with unnamed parameters """ |
---|
30 | f = pvoc(2048, 128) |
---|
31 | assert_equal ([f.win_s, f.hop_s], [2048, 128]) |
---|
32 | |
---|
33 | def test_members_named_params(self): |
---|
34 | """ check object creation with named parameters """ |
---|
35 | f = pvoc(hop_s = 128, win_s = 2048) |
---|
36 | assert_equal ([f.win_s, f.hop_s], [2048, 128]) |
---|
37 | |
---|
38 | def test_zeros(self): |
---|
39 | """ check the resynthesis of zeros gives zeros """ |
---|
40 | win_s, hop_s = 1024, 256 |
---|
41 | f = pvoc (win_s, hop_s) |
---|
42 | t = fvec (hop_s) |
---|
43 | for time in range( int ( 4 * win_s / hop_s ) ): |
---|
44 | s = f(t) |
---|
45 | r = f.rdo(s) |
---|
46 | assert_equal ( t, 0.) |
---|
47 | assert_equal ( s.norm, 0.) |
---|
48 | assert_equal ( s.phas, 0.) |
---|
49 | assert_equal ( r, 0.) |
---|
50 | |
---|
51 | @params( |
---|
52 | ( 256, 8), |
---|
53 | ( 256, 4), |
---|
54 | ( 256, 2), |
---|
55 | ( 512, 8), |
---|
56 | ( 512, 4), |
---|
57 | ( 512, 2), |
---|
58 | #( 129, 2), |
---|
59 | #( 320, 4), |
---|
60 | #( 13, 8), |
---|
61 | (1024, 8), |
---|
62 | (1024, 4), |
---|
63 | (1024, 2), |
---|
64 | (2048, 8), |
---|
65 | (2048, 4), |
---|
66 | (2048, 2), |
---|
67 | (4096, 8), |
---|
68 | (4096, 4), |
---|
69 | (4096, 2), |
---|
70 | (8192, 8), |
---|
71 | (8192, 4), |
---|
72 | (8192, 2), |
---|
73 | ) |
---|
74 | def test_resynth_steps_noise(self, hop_s, ratio): |
---|
75 | """ check the resynthesis of a random signal is correct """ |
---|
76 | sigin = create_noise(hop_s) |
---|
77 | self.reconstruction(sigin, hop_s, ratio) |
---|
78 | |
---|
79 | @params( |
---|
80 | (44100, 256, 8, 441), |
---|
81 | (44100, 256, 4, 1203), |
---|
82 | (44100, 256, 2, 3045), |
---|
83 | (44100, 512, 8, 445), |
---|
84 | (44100, 512, 4, 445), |
---|
85 | (44100, 512, 2, 445), |
---|
86 | (44100, 1024, 8, 445), |
---|
87 | (44100, 1024, 4, 445), |
---|
88 | (44100, 1024, 2, 445), |
---|
89 | ( 8000, 1024, 2, 445), |
---|
90 | (22050, 1024, 2, 445), |
---|
91 | (22050, 256, 8, 445), |
---|
92 | (96000, 1024, 8, 47000), |
---|
93 | (96000, 1024, 8, 20), |
---|
94 | ) |
---|
95 | def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq): |
---|
96 | """ check the resynthesis of a sine is correct """ |
---|
97 | sigin = create_sine(hop_s, freq, samplerate) |
---|
98 | self.reconstruction(sigin, hop_s, ratio) |
---|
99 | |
---|
100 | def reconstruction(self, sigin, hop_s, ratio): |
---|
101 | buf_s = hop_s * ratio |
---|
102 | f = pvoc(buf_s, hop_s) |
---|
103 | zeros = fvec(hop_s) |
---|
104 | r2 = f.rdo( f(sigin) ) |
---|
105 | for i in range(1, ratio): |
---|
106 | r2 = f.rdo( f(zeros) ) |
---|
107 | # compute square errors |
---|
108 | sq_error = (r2 - sigin)**2 |
---|
109 | # make sure all square errors are less than desired precision |
---|
110 | assert_array_less(sq_error, max_sq_error) |
---|
111 | |
---|
112 | class aubio_pvoc_strange_params(TestCase): |
---|
113 | |
---|
114 | def test_win_size_short(self): |
---|
115 | with self.assertRaises(RuntimeError): |
---|
116 | pvoc(1, 1) |
---|
117 | |
---|
118 | def test_hop_size_long(self): |
---|
119 | with self.assertRaises(RuntimeError): |
---|
120 | pvoc(1024, 1025) |
---|
121 | |
---|
122 | def test_large_input_timegrain(self): |
---|
123 | win_s = 1024 |
---|
124 | f = pvoc(win_s) |
---|
125 | t = fvec(win_s + 1) |
---|
126 | with self.assertRaises(ValueError): |
---|
127 | f(t) |
---|
128 | |
---|
129 | def test_small_input_timegrain(self): |
---|
130 | win_s = 1024 |
---|
131 | f = pvoc(win_s) |
---|
132 | t = fvec(1) |
---|
133 | with self.assertRaises(ValueError): |
---|
134 | f(t) |
---|
135 | |
---|
136 | def test_large_input_fftgrain(self): |
---|
137 | win_s = 1024 |
---|
138 | f = pvoc(win_s) |
---|
139 | s = cvec(win_s + 5) |
---|
140 | with self.assertRaises(ValueError): |
---|
141 | f.rdo(s) |
---|
142 | |
---|
143 | def test_small_input_fftgrain(self): |
---|
144 | win_s = 1024 |
---|
145 | f = pvoc(win_s) |
---|
146 | s = cvec(16) |
---|
147 | with self.assertRaises(ValueError): |
---|
148 | f.rdo(s) |
---|
149 | |
---|
150 | class aubio_pvoc_wrong_params(TestCase): |
---|
151 | |
---|
152 | def test_wrong_buf_size(self): |
---|
153 | win_s = -1 |
---|
154 | with self.assertRaises(ValueError): |
---|
155 | pvoc(win_s) |
---|
156 | |
---|
157 | def test_buf_size_too_small(self): |
---|
158 | win_s = 1 |
---|
159 | with self.assertRaises(RuntimeError): |
---|
160 | pvoc(win_s) |
---|
161 | |
---|
162 | def test_hop_size_negative(self): |
---|
163 | win_s = 512 |
---|
164 | hop_s = -2 |
---|
165 | with self.assertRaises(ValueError): |
---|
166 | pvoc(win_s, hop_s) |
---|
167 | |
---|
168 | def test_hop_size_too_small(self): |
---|
169 | win_s = 1 |
---|
170 | hop_s = 1 |
---|
171 | with self.assertRaises(RuntimeError): |
---|
172 | pvoc(win_s, hop_s) |
---|
173 | |
---|
174 | def test_buf_size_not_power_of_two(self): |
---|
175 | win_s = 320 |
---|
176 | hop_s = win_s // 2 |
---|
177 | try: |
---|
178 | with self.assertRaises(RuntimeError): |
---|
179 | pvoc(win_s, hop_s) |
---|
180 | except AssertionError as e: |
---|
181 | # when compiled with fftw3, aubio supports non power of two fft sizes |
---|
182 | self.skipTest('creating aubio.pvoc with size %d did not fail' % win_s) |
---|
183 | |
---|
184 | if __name__ == '__main__': |
---|
185 | from nose2 import main |
---|
186 | main() |
---|
187 | |
---|