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.)
49 assert_equal ( s.phas, 0.)
75 def test_resynth_steps_noise(self, hop_s, ratio):
76 """ check the resynthesis of a random signal is correct """
77 sigin = create_noise(hop_s)
78 self.reconstruction(sigin, hop_s, ratio)
82 (44100, 256, 4, 1203),
83 (44100, 256, 2, 3045),
87 (44100, 1024, 8, 445),
88 (44100, 1024, 4, 445),
89 (44100, 1024, 2, 445),
90 ( 8000, 1024, 2, 445),
91 (22050, 1024, 2, 445),
93 (96000, 1024, 8, 47000),
96 def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq):
97 """ check the resynthesis of a sine is correct """
98 sigin = create_sine(hop_s, freq, samplerate)
99 self.reconstruction(sigin, hop_s, ratio)
101 def reconstruction(self, sigin, hop_s, ratio):
102 buf_s = hop_s * ratio
103 f = pvoc(buf_s, hop_s)
105 r2 = f.rdo( f(sigin) )
106 for _ in range(1, ratio):
107 r2 = f.rdo( f(zeros) )
108 # compute square errors
109 sq_error = (r2 - sigin)**2
110 # make sure all square errors are less than desired precision
111 assert_array_less(sq_error, max_sq_error)
113 class aubio_pvoc_strange_params(TestCase):
115 def test_win_size_short(self):
116 with self.assertRaises(RuntimeError):
119 def test_hop_size_long(self):
120 with self.assertRaises(RuntimeError):
123 def test_large_input_timegrain(self):
127 with self.assertRaises(ValueError):
130 def test_small_input_timegrain(self):
134 with self.assertRaises(ValueError):
137 def test_large_input_fftgrain(self):
141 with self.assertRaises(ValueError):
144 def test_small_input_fftgrain(self):
148 with self.assertRaises(ValueError):
151 class aubio_pvoc_wrong_params(TestCase):
153 def test_wrong_buf_size(self):
155 with self.assertRaises(ValueError):
158 def test_buf_size_too_small(self):
160 with self.assertRaises(RuntimeError):
163 def test_hop_size_negative(self):
166 with self.assertRaises(ValueError):
169 def test_hop_size_too_small(self):
172 with self.assertRaises(RuntimeError):
175 def test_buf_size_not_power_of_two(self):
179 with self.assertRaises(RuntimeError):
181 except AssertionError:
182 # when compiled with fftw3, aubio supports non power of two fft sizes
183 self.skipTest('creating aubio.pvoc with size %d did not fail' % win_s)
185 if __name__ == '__main__':