3 from numpy.testing import TestCase, assert_equal, assert_array_less
4 from aubio import fvec, cvec, pvoc, float_type
5 from numpy import array, shape
6 from numpy.random import random
11 def create_sine(hop_s, freq, samplerate):
12 t = np.arange(hop_s).astype(float_type)
13 return np.sin( 2. * np.pi * t * freq / samplerate)
15 def create_noise(hop_s):
16 return np.random.rand(hop_s).astype(float_type) * 2. - 1.
18 class aubio_pvoc_test_case(TestCase):
19 """ pvoc object test case """
21 def test_members_automatic_sizes_default(self):
22 """ check object creation with default parameters """
24 assert_equal ([f.win_s, f.hop_s], [1024, 512])
26 def test_members_unnamed_params(self):
27 """ check object creation with unnamed parameters """
29 assert_equal ([f.win_s, f.hop_s], [2048, 128])
31 def test_members_named_params(self):
32 """ check object creation with named parameters """
33 f = pvoc(hop_s = 128, win_s = 2048)
34 assert_equal ([f.win_s, f.hop_s], [2048, 128])
37 """ check the resynthesis of zeros gives zeros """
38 win_s, hop_s = 1024, 256
39 f = pvoc (win_s, hop_s)
41 for time in range( int ( 4 * win_s / hop_s ) ):
44 assert_equal ( array(t), 0)
45 assert_equal ( s.norm, 0)
46 assert_equal ( s.phas, 0)
49 def test_resynth_8_steps_sine(self):
50 """ check the resynthesis of is correct with 87.5% overlap """
53 freq = 445; samplerate = 22050
54 sigin = create_sine(hop_s, freq, samplerate)
55 self.reconstruction( sigin, hop_s, ratio)
57 def test_resynth_8_steps(self):
58 """ check the resynthesis of is correct with 87.5% overlap """
61 sigin = create_noise(hop_s)
62 self.reconstruction(sigin, hop_s, ratio)
64 def test_resynth_4_steps_sine(self):
65 """ check the resynthesis of is correct with 87.5% overlap """
68 freq = 445; samplerate = 22050
69 sigin = create_sine(hop_s, freq, samplerate)
70 self.reconstruction(sigin, hop_s, ratio)
72 def test_resynth_4_steps(self):
73 """ check the resynthesis of is correct with 75% overlap """
76 sigin = create_noise(hop_s)
77 self.reconstruction(sigin, hop_s, ratio)
79 def test_resynth_2_steps_sine(self):
80 """ check the resynthesis of is correct with 50% overlap """
83 freq = 445; samplerate = 22050
84 sigin = create_sine(hop_s, freq, samplerate)
85 self.reconstruction(sigin, hop_s, ratio)
87 def test_resynth_2_steps(self):
88 """ check the resynthesis of is correct with 50% overlap """
91 sigin = create_noise(hop_s)
92 self.reconstruction(sigin, hop_s, ratio)
94 def reconstruction(self, sigin, hop_s, ratio):
96 f = pvoc(buf_s, hop_s)
98 r2 = f.rdo( f(sigin) )
99 for i in range(1, ratio):
100 r2 = f.rdo( f(zeros) )
101 # compute square errors
102 sq_error = (r2 - sigin)**2
103 # make sure all square errors are less than desired precision
104 assert_array_less(sq_error, max_sq_error)
106 if __name__ == '__main__':
107 from unittest import main