1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy.testing import TestCase, assert_equal, assert_array_less |
---|
4 | from _tools import parametrize |
---|
5 | from aubio import fvec, cvec, pvoc, float_type |
---|
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 Test_aubio_pvoc_test_case: |
---|
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 _ 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 | try: |
---|
49 | assert_equal ( s.phas, 0 ) |
---|
50 | except AssertionError: |
---|
51 | assert_equal (s.phas[s.phas > 0], +np.pi) |
---|
52 | assert_equal (s.phas[s.phas < 0], -np.pi) |
---|
53 | assert_equal (np.abs(s.phas[np.abs(s.phas) != np.pi]), 0) |
---|
54 | self.skipTest('pvoc(fvec(%d)).phas != +0, ' % win_s \ |
---|
55 | + 'This is expected when using fftw3 on powerpc.') |
---|
56 | assert_equal ( r, 0.) |
---|
57 | |
---|
58 | resynth_noise_args = "hop_s, ratio" |
---|
59 | resynth_noise_values = [ |
---|
60 | ( 256, 8), |
---|
61 | ( 256, 4), |
---|
62 | ( 256, 2), |
---|
63 | ( 512, 8), |
---|
64 | ( 512, 4), |
---|
65 | ( 512, 2), |
---|
66 | #( 129, 2), |
---|
67 | #( 320, 4), |
---|
68 | #( 13, 8), |
---|
69 | (1024, 8), |
---|
70 | (1024, 4), |
---|
71 | (1024, 2), |
---|
72 | (2048, 8), |
---|
73 | (2048, 4), |
---|
74 | (2048, 2), |
---|
75 | (4096, 8), |
---|
76 | (4096, 4), |
---|
77 | (4096, 2), |
---|
78 | (8192, 8), |
---|
79 | (8192, 4), |
---|
80 | (8192, 2), |
---|
81 | ] |
---|
82 | |
---|
83 | @parametrize(resynth_noise_args, resynth_noise_values) |
---|
84 | def test_resynth_steps_noise(self, hop_s, ratio): |
---|
85 | """ check the resynthesis of a random signal is correct """ |
---|
86 | sigin = create_noise(hop_s) |
---|
87 | self.reconstruction(sigin, hop_s, ratio) |
---|
88 | |
---|
89 | resynth_sine_args = "samplerate, hop_s, ratio, freq" |
---|
90 | resynth_sine_values = [ |
---|
91 | (44100, 256, 8, 441), |
---|
92 | (44100, 256, 4, 1203), |
---|
93 | (44100, 256, 2, 3045), |
---|
94 | (44100, 512, 8, 445), |
---|
95 | (44100, 512, 4, 445), |
---|
96 | (44100, 512, 2, 445), |
---|
97 | (44100, 1024, 8, 445), |
---|
98 | (44100, 1024, 4, 445), |
---|
99 | (44100, 1024, 2, 445), |
---|
100 | ( 8000, 1024, 2, 445), |
---|
101 | (22050, 1024, 2, 445), |
---|
102 | (22050, 256, 8, 445), |
---|
103 | (96000, 1024, 8, 47000), |
---|
104 | (96000, 1024, 8, 20), |
---|
105 | ] |
---|
106 | |
---|
107 | @parametrize(resynth_sine_args, resynth_sine_values) |
---|
108 | def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq): |
---|
109 | """ check the resynthesis of a sine is correct """ |
---|
110 | sigin = create_sine(hop_s, freq, samplerate) |
---|
111 | self.reconstruction(sigin, hop_s, ratio) |
---|
112 | |
---|
113 | def reconstruction(self, sigin, hop_s, ratio): |
---|
114 | buf_s = hop_s * ratio |
---|
115 | f = pvoc(buf_s, hop_s) |
---|
116 | zeros = fvec(hop_s) |
---|
117 | r2 = f.rdo( f(sigin) ) |
---|
118 | for _ in range(1, ratio): |
---|
119 | r2 = f.rdo( f(zeros) ) |
---|
120 | # compute square errors |
---|
121 | sq_error = (r2 - sigin)**2 |
---|
122 | # make sure all square errors are less than desired precision |
---|
123 | assert_array_less(sq_error, max_sq_error) |
---|
124 | |
---|
125 | class aubio_pvoc_strange_params(TestCase): |
---|
126 | |
---|
127 | def test_win_size_short(self): |
---|
128 | with self.assertRaises(RuntimeError): |
---|
129 | pvoc(1, 1) |
---|
130 | |
---|
131 | def test_hop_size_long(self): |
---|
132 | with self.assertRaises(RuntimeError): |
---|
133 | pvoc(1024, 1025) |
---|
134 | |
---|
135 | def test_large_input_timegrain(self): |
---|
136 | win_s = 1024 |
---|
137 | f = pvoc(win_s) |
---|
138 | t = fvec(win_s + 1) |
---|
139 | with self.assertRaises(ValueError): |
---|
140 | f(t) |
---|
141 | |
---|
142 | def test_small_input_timegrain(self): |
---|
143 | win_s = 1024 |
---|
144 | f = pvoc(win_s) |
---|
145 | t = fvec(1) |
---|
146 | with self.assertRaises(ValueError): |
---|
147 | f(t) |
---|
148 | |
---|
149 | def test_large_input_fftgrain(self): |
---|
150 | win_s = 1024 |
---|
151 | f = pvoc(win_s) |
---|
152 | s = cvec(win_s + 5) |
---|
153 | with self.assertRaises(ValueError): |
---|
154 | f.rdo(s) |
---|
155 | |
---|
156 | def test_small_input_fftgrain(self): |
---|
157 | win_s = 1024 |
---|
158 | f = pvoc(win_s) |
---|
159 | s = cvec(16) |
---|
160 | with self.assertRaises(ValueError): |
---|
161 | f.rdo(s) |
---|
162 | |
---|
163 | class aubio_pvoc_wrong_params(TestCase): |
---|
164 | |
---|
165 | def test_wrong_buf_size(self): |
---|
166 | win_s = -1 |
---|
167 | with self.assertRaises(ValueError): |
---|
168 | pvoc(win_s) |
---|
169 | |
---|
170 | def test_buf_size_too_small(self): |
---|
171 | win_s = 1 |
---|
172 | with self.assertRaises(RuntimeError): |
---|
173 | pvoc(win_s) |
---|
174 | |
---|
175 | def test_hop_size_negative(self): |
---|
176 | win_s = 512 |
---|
177 | hop_s = -2 |
---|
178 | with self.assertRaises(ValueError): |
---|
179 | pvoc(win_s, hop_s) |
---|
180 | |
---|
181 | def test_hop_size_too_small(self): |
---|
182 | win_s = 1 |
---|
183 | hop_s = 1 |
---|
184 | with self.assertRaises(RuntimeError): |
---|
185 | pvoc(win_s, hop_s) |
---|
186 | |
---|
187 | def test_buf_size_not_power_of_two(self): |
---|
188 | win_s = 320 |
---|
189 | hop_s = win_s // 2 |
---|
190 | try: |
---|
191 | with self.assertRaises(RuntimeError): |
---|
192 | pvoc(win_s, hop_s) |
---|
193 | except AssertionError: |
---|
194 | # when compiled with fftw3, aubio supports non power of two fft sizes |
---|
195 | self.skipTest('creating aubio.pvoc with size %d did not fail' % win_s) |
---|
196 | |
---|
197 | if __name__ == '__main__': |
---|
198 | from unittest import main |
---|
199 | main() |
---|