1 from numpy.testing import TestCase, run_module_suite
2 from numpy.testing import assert_equal, assert_almost_equal
3 # WARNING: numpy also has an fft object
4 from _aubio import fft, fvec, cvec
5 from numpy import array, shape
8 class aubio_fft_test_case(TestCase):
10 def test_members(self):
12 assert_equal ([f.win_s, f.channels], [1024, 1])
14 assert_equal ([f.win_s, f.channels], [2048, 4])
16 def test_output_dimensions(self):
17 """ check the dimensions of output """
19 timegrain = fvec(win_s, chan)
21 fftgrain = f (timegrain)
22 assert_equal (array(fftgrain), 0)
23 assert_equal (shape(fftgrain), (chan * 2, win_s/2+1))
24 assert_equal (fftgrain.norm, 0)
25 assert_equal (shape(fftgrain.norm), (chan, win_s/2+1))
26 assert_equal (fftgrain.phas, 0)
27 assert_equal (shape(fftgrain.phas), (chan, win_s/2+1))
30 """ check the transform of zeros """
32 timegrain = fvec(win_s, chan)
34 fftgrain = f(timegrain)
35 assert_equal ( fftgrain.norm == 0, True )
36 assert_equal ( fftgrain.phas == 0, True )
38 def test_impulse(self):
39 """ check the transform of one impulse at a random place """
40 from random import random
41 from math import floor
43 i = floor(random()*win_s)
44 impulse = pi * random()
46 timegrain = fvec(win_s, chan)
47 timegrain[0][i] = impulse
48 fftgrain = f ( timegrain )
49 #self.plot_this ( fftgrain.phas[0] )
50 assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 )
51 assert_equal ( fftgrain.phas <= pi, True)
52 assert_equal ( fftgrain.phas >= -pi, True)
54 def test_impulse_negative(self):
55 """ check the transform of one impulse at a random place """
56 from random import random
57 from math import floor
62 timegrain = fvec(win_s, chan)
63 timegrain[0][i] = impulse
64 fftgrain = f ( timegrain )
65 #self.plot_this ( fftgrain.phas[0] )
66 assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 )
68 # phase can be pi or -pi, as it is not unwrapped
69 assert_almost_equal ( abs(fftgrain.phas[0][1:-1]) , pi, decimal = 6 )
70 assert_almost_equal ( fftgrain.phas[0][0], pi, decimal = 6)
71 assert_almost_equal ( fftgrain.phas[0][-1], pi, decimal = 6)
73 assert_equal ( fftgrain.phas[0][1:-1] == 0, True)
74 assert_equal ( fftgrain.phas[0][0] == 0, True)
75 assert_equal ( fftgrain.phas[0][-1] == 0, True)
76 # now check the resynthesis
77 synthgrain = f.rdo ( fftgrain )
78 #self.plot_this ( fftgrain.phas.T )
79 assert_equal ( fftgrain.phas <= pi, True)
80 assert_equal ( fftgrain.phas >= -pi, True)
81 #self.plot_this ( synthgrain - timegrain )
82 assert_almost_equal ( synthgrain, timegrain, decimal = 6 )
84 def test_impulse_at_zero(self):
85 """ check the transform of one impulse at a index 0 in one channel """
89 timegrain = fvec(win_s, chan)
90 timegrain[0][0] = impulse
91 fftgrain = f ( timegrain )
92 #self.plot_this ( fftgrain.phas )
93 assert_equal ( fftgrain.phas[0], 0)
94 assert_equal ( fftgrain.phas[1], 0)
95 assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 )
96 assert_equal ( fftgrain.norm[0], impulse)
98 def test_rdo_before_do(self):
99 """ check running fft.rdo before fft.do works """
100 win_s, chan = 1024, 2
103 fftgrain = cvec(win_s, chan)
104 t = f.rdo( fftgrain )
105 assert_equal ( t, 0 )
107 def plot_this(self, this):
108 from pylab import plot, show
112 if __name__ == '__main__':
113 from unittest import main