3 from unittest import main
4 from numpy.testing import TestCase
5 from numpy.testing import assert_equal, assert_almost_equal
7 from aubio import fvec, fft, cvec
8 from math import pi, floor
9 from random import random
11 class aubio_fft_test_case(TestCase):
13 def test_members(self):
14 """ check members are set correctly """
17 assert_equal (f.win_s, win_s)
19 def test_output_dimensions(self):
20 """ check the dimensions of output """
22 timegrain = fvec(win_s)
24 fftgrain = f (timegrain)
26 assert_equal (fftgrain.norm.shape, (win_s/2+1,))
27 assert_equal (fftgrain.phas.shape, (win_s/2+1,))
30 """ check the transform of zeros is all zeros """
32 timegrain = fvec(win_s)
34 fftgrain = f (timegrain)
35 assert_equal ( fftgrain.norm, 0 )
36 assert_equal ( fftgrain.phas, 0 )
38 def test_impulse(self):
39 """ check the transform of one impulse at a random place """
41 i = int(floor(random()*win_s))
42 impulse = pi * random()
44 timegrain = fvec(win_s)
45 timegrain[i] = impulse
46 fftgrain = f ( timegrain )
47 #self.plot_this ( fftgrain.phas )
48 assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 )
49 assert_equal ( fftgrain.phas <= pi, True)
50 assert_equal ( fftgrain.phas >= -pi, True)
52 def test_impulse_negative(self):
53 """ check the transform of a negative impulse at a random place """
55 i = int(floor(random()*win_s))
58 timegrain = fvec(win_s)
60 timegrain[i] = impulse
61 fftgrain = f ( timegrain )
62 #self.plot_this ( fftgrain.phas )
63 assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 5 )
65 # phase can be pi or -pi, as it is not unwrapped
66 #assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 )
67 assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6)
68 assert_almost_equal ( np.fmod(fftgrain.phas[-1], pi), 0, decimal = 6)
70 #assert_equal ( fftgrain.phas[1:-1] == 0, True)
71 assert_equal ( fftgrain.phas[0], 0)
72 assert_almost_equal ( np.fmod(fftgrain.phas[-1], pi), 0, decimal = 6)
73 # now check the resynthesis
74 synthgrain = f.rdo ( fftgrain )
75 #self.plot_this ( fftgrain.phas.T )
76 assert_equal ( fftgrain.phas <= pi, True)
77 assert_equal ( fftgrain.phas >= -pi, True)
78 #self.plot_this ( synthgrain - timegrain )
79 assert_almost_equal ( synthgrain, timegrain, decimal = 6 )
81 def test_impulse_at_zero(self):
82 """ check the transform of one impulse at a index 0 """
86 timegrain = fvec(win_s)
87 timegrain[0] = impulse
88 fftgrain = f ( timegrain )
89 #self.plot_this ( fftgrain.phas )
90 assert_equal ( fftgrain.phas[0], 0)
91 # could be 0 or -0 depending on fft implementation (0 for fftw3, -0 for ooura)
92 assert_almost_equal ( fftgrain.phas[1], 0)
93 assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 )
95 def test_rdo_before_do(self):
96 """ check running fft.rdo before fft.do works """
99 fftgrain = cvec(win_s)
100 t = f.rdo( fftgrain )
101 assert_equal ( t, 0 )
103 def plot_this(self, this):
104 from pylab import plot, show
108 def test_local_fftgrain(self):
109 """ check aubio.fft() result can be accessed after deletion """
110 def compute_grain(impulse):
112 timegrain = fvec(win_s)
113 timegrain[0] = impulse
115 fftgrain = f ( timegrain )
118 fftgrain = compute_grain(impulse)
119 assert_equal ( fftgrain.phas[0], 0)
120 assert_almost_equal ( fftgrain.phas[1], 0)
121 assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 )
123 def test_local_reconstruct(self):
124 """ check aubio.fft.rdo() result can be accessed after deletion """
125 def compute_grain(impulse):
127 timegrain = fvec(win_s)
128 timegrain[0] = impulse
130 fftgrain = f ( timegrain )
134 r = compute_grain(impulse)
135 assert_almost_equal ( r[0], impulse, decimal = 6)
136 assert_almost_equal ( r[1:], 0)
138 def test_large_input_timegrain(self):
142 with self.assertRaises(ValueError):
145 def test_small_input_timegrain(self):
149 with self.assertRaises(ValueError):
152 def test_large_input_fftgrain(self):
156 with self.assertRaises(ValueError):
159 def test_small_input_fftgrain(self):
163 with self.assertRaises(ValueError):
166 class aubio_fft_wrong_params(TestCase):
168 def test_wrong_buf_size(self):
170 with self.assertRaises(ValueError):
173 def test_buf_size_not_power_of_two(self):
174 # when compiled with fftw3, aubio supports non power of two fft sizes
177 with self.assertRaises(RuntimeError):
179 except AssertionError:
180 self.skipTest('creating aubio.fft with size %d did not fail' % win_s)
182 def test_buf_size_too_small(self):
184 with self.assertRaises(RuntimeError):
187 if __name__ == '__main__':