source: interfaces/python/test_fft.py @ 0536612

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 0536612 was 0536612, checked in by Paul Brossier <piem@piem.org>, 14 years ago

interfaces/python: added more tests

  • Property mode set to 100644
File size: 3.8 KB
Line 
1from numpy.testing import TestCase, run_module_suite
2from numpy.testing import assert_equal, assert_almost_equal
3# WARNING: numpy also has an fft object
4from _aubio import fft, fvec, cvec
5from numpy import array, shape
6from math import pi
7
8class aubio_fft_test_case(TestCase):
9
10  def test_members(self):
11    f = fft()
12    assert_equal ([f.win_s, f.channels], [1024, 1])
13    f = fft(2048, 4)
14    assert_equal ([f.win_s, f.channels], [2048, 4])
15
16  def test_output_dimensions(self):
17    """ check the dimensions of output """
18    win_s, chan = 1024, 3
19    timegrain = fvec(win_s, chan)
20    f = fft(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))
28
29  def test_zeros(self):
30    """ check the transform of zeros """
31    win_s, chan = 512, 3
32    timegrain = fvec(win_s, chan)
33    f = fft(win_s, chan)
34    fftgrain = f(timegrain)
35    assert_equal ( fftgrain.norm == 0, True )
36    assert_equal ( fftgrain.phas == 0, True )
37
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
42    win_s, chan = 256, 1
43    i = floor(random()*win_s)
44    impulse = pi * random() 
45    f = fft(win_s, chan)
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)
53
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
58    win_s, chan = 256, 1
59    i = 0 
60    impulse = -10. 
61    f = fft(win_s, chan)
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 )
67    if impulse < 0:
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)
72    else:
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 )
83
84  def test_impulse_at_zero(self):
85    """ check the transform of one impulse at a index 0 in one channel """
86    win_s, chan = 1024, 2
87    impulse = pi
88    f = fft(win_s, chan)
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)
97
98  def test_rdo_before_do(self):
99    """ check running fft.rdo before fft.do works """
100    win_s, chan = 1024, 2
101    impulse = pi
102    f = fft(win_s, chan)
103    fftgrain = cvec(win_s, chan)
104    t = f.rdo( fftgrain )
105    assert_equal ( t, 0 )
106
107  def plot_this(self, this):
108    from pylab import plot, show
109    plot ( this )
110    show ()
111
112if __name__ == '__main__':
113  from unittest import main
114  main()
115
Note: See TracBrowser for help on using the repository browser.