3 from numpy.testing import TestCase, assert_equal, assert_array_less
4 from aubio import fvec, cvec, pvoc, float_type
6 from nose2.tools import params
9 if float_type == 'float32':
14 def create_sine(hop_s, freq, samplerate):
15 t = np.arange(hop_s).astype(float_type)
16 return np.sin( 2. * np.pi * freq * t / float(samplerate))
18 def create_noise(hop_s):
19 return np.random.rand(hop_s).astype(float_type) * 2. - 1.
21 class aubio_pvoc_test_case(TestCase):
22 """ pvoc object test case """
24 def test_members_automatic_sizes_default(self):
25 """ check object creation with default parameters """
27 assert_equal ([f.win_s, f.hop_s], [1024, 512])
29 def test_members_unnamed_params(self):
30 """ check object creation with unnamed parameters """
32 assert_equal ([f.win_s, f.hop_s], [2048, 128])
34 def test_members_named_params(self):
35 """ check object creation with named parameters """
36 f = pvoc(hop_s = 128, win_s = 2048)
37 assert_equal ([f.win_s, f.hop_s], [2048, 128])
40 """ check the resynthesis of zeros gives zeros """
41 win_s, hop_s = 1024, 256
42 f = pvoc (win_s, hop_s)
44 for _ in range( int ( 4 * win_s / hop_s ) ):
48 assert_equal ( s.norm, 0.)
50 assert_equal ( s.phas, 0 )
51 except AssertionError:
52 assert_equal (s.phas[s.phas > 0], +np.pi)
53 assert_equal (s.phas[s.phas < 0], -np.pi)
54 assert_equal (np.abs(s.phas[np.abs(s.phas) != np.pi]), 0)
55 self.skipTest('pvoc(fvec(%d)).phas != +0, ' % win_s \
56 + 'This is expected when using fftw3 on powerpc.')
59 def test_no_overlap(self):
60 win_s, hop_s = 1024, 1024
61 f = pvoc (win_s, hop_s)
91 def test_resynth_steps_noise(self, hop_s, ratio):
92 """ check the resynthesis of a random signal is correct """
93 sigin = create_noise(hop_s)
94 self.reconstruction(sigin, hop_s, ratio)
98 (44100, 256, 4, 1203),
99 (44100, 256, 2, 3045),
100 (44100, 512, 8, 445),
101 (44100, 512, 4, 445),
102 (44100, 512, 2, 445),
103 (44100, 1024, 8, 445),
104 (44100, 1024, 4, 445),
105 (44100, 1024, 2, 445),
106 ( 8000, 1024, 2, 445),
107 (22050, 1024, 2, 445),
108 (22050, 256, 8, 445),
109 (96000, 1024, 8, 47000),
110 (96000, 1024, 8, 20),
112 def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq):
113 """ check the resynthesis of a sine is correct """
114 sigin = create_sine(hop_s, freq, samplerate)
115 self.reconstruction(sigin, hop_s, ratio)
117 def reconstruction(self, sigin, hop_s, ratio):
118 buf_s = hop_s * ratio
119 f = pvoc(buf_s, hop_s)
121 r2 = f.rdo( f(sigin) )
122 for _ in range(1, ratio):
123 r2 = f.rdo( f(zeros) )
124 # compute square errors
125 sq_error = (r2 - sigin)**2
126 # make sure all square errors are less than desired precision
127 assert_array_less(sq_error, max_sq_error)
129 class aubio_pvoc_strange_params(TestCase):
131 def test_win_size_short(self):
132 with self.assertRaises(RuntimeError):
135 def test_hop_size_long(self):
136 with self.assertRaises(RuntimeError):
139 def test_large_input_timegrain(self):
143 with self.assertRaises(ValueError):
146 def test_small_input_timegrain(self):
150 with self.assertRaises(ValueError):
153 def test_large_input_fftgrain(self):
157 with self.assertRaises(ValueError):
160 def test_small_input_fftgrain(self):
164 with self.assertRaises(ValueError):
167 class aubio_pvoc_wrong_params(TestCase):
169 def test_wrong_buf_size(self):
171 with self.assertRaises(ValueError):
174 def test_buf_size_too_small(self):
176 with self.assertRaises(RuntimeError):
179 def test_hop_size_negative(self):
182 with self.assertRaises(ValueError):
185 def test_hop_size_too_small(self):
188 with self.assertRaises(RuntimeError):
191 def test_buf_size_not_power_of_two(self):
195 with self.assertRaises(RuntimeError):
197 except AssertionError:
198 # when compiled with fftw3, aubio supports non power of two fft sizes
199 self.skipTest('creating aubio.pvoc with size %d did not fail' % win_s)
201 if __name__ == '__main__':