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.')
82 def test_resynth_steps_noise(self, hop_s, ratio):
83 """ check the resynthesis of a random signal is correct """
84 sigin = create_noise(hop_s)
85 self.reconstruction(sigin, hop_s, ratio)
89 (44100, 256, 4, 1203),
90 (44100, 256, 2, 3045),
94 (44100, 1024, 8, 445),
95 (44100, 1024, 4, 445),
96 (44100, 1024, 2, 445),
97 ( 8000, 1024, 2, 445),
98 (22050, 1024, 2, 445),
100 (96000, 1024, 8, 47000),
101 (96000, 1024, 8, 20),
103 def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq):
104 """ check the resynthesis of a sine is correct """
105 sigin = create_sine(hop_s, freq, samplerate)
106 self.reconstruction(sigin, hop_s, ratio)
108 def reconstruction(self, sigin, hop_s, ratio):
109 buf_s = hop_s * ratio
110 f = pvoc(buf_s, hop_s)
112 r2 = f.rdo( f(sigin) )
113 for _ in range(1, ratio):
114 r2 = f.rdo( f(zeros) )
115 # compute square errors
116 sq_error = (r2 - sigin)**2
117 # make sure all square errors are less than desired precision
118 assert_array_less(sq_error, max_sq_error)
120 class aubio_pvoc_strange_params(TestCase):
122 def test_win_size_short(self):
123 with self.assertRaises(RuntimeError):
126 def test_hop_size_long(self):
127 with self.assertRaises(RuntimeError):
130 def test_large_input_timegrain(self):
134 with self.assertRaises(ValueError):
137 def test_small_input_timegrain(self):
141 with self.assertRaises(ValueError):
144 def test_large_input_fftgrain(self):
148 with self.assertRaises(ValueError):
151 def test_small_input_fftgrain(self):
155 with self.assertRaises(ValueError):
158 class aubio_pvoc_wrong_params(TestCase):
160 def test_wrong_buf_size(self):
162 with self.assertRaises(ValueError):
165 def test_buf_size_too_small(self):
167 with self.assertRaises(RuntimeError):
170 def test_hop_size_negative(self):
173 with self.assertRaises(ValueError):
176 def test_hop_size_too_small(self):
179 with self.assertRaises(RuntimeError):
182 def test_buf_size_not_power_of_two(self):
186 with self.assertRaises(RuntimeError):
188 except AssertionError:
189 # when compiled with fftw3, aubio supports non power of two fft sizes
190 self.skipTest('creating aubio.pvoc with size %d did not fail' % win_s)
192 if __name__ == '__main__':