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 fvec, fft, cvec |
---|
5 | from numpy import array, shape |
---|
6 | from math import pi |
---|
7 | |
---|
8 | class aubio_fft_test_case(TestCase): |
---|
9 | |
---|
10 | def test_members(self): |
---|
11 | f = fft() |
---|
12 | assert_equal (f.win_s, 1024) |
---|
13 | |
---|
14 | def test_output_dimensions(self): |
---|
15 | """ check the dimensions of output """ |
---|
16 | win_s = 1024 |
---|
17 | timegrain = fvec(win_s) |
---|
18 | f = fft(win_s) |
---|
19 | fftgrain = f (timegrain) |
---|
20 | assert_equal (fftgrain.norm, 0) |
---|
21 | assert_equal (shape(fftgrain.norm), (win_s/2+1,)) |
---|
22 | assert_equal (fftgrain.phas, 0) |
---|
23 | assert_equal (shape(fftgrain.phas), (win_s/2+1,)) |
---|
24 | |
---|
25 | def test_zeros(self): |
---|
26 | """ check the transform of zeros """ |
---|
27 | win_s = 512 |
---|
28 | timegrain = fvec(win_s) |
---|
29 | f = fft(win_s) |
---|
30 | fftgrain = f(timegrain) |
---|
31 | assert_equal ( fftgrain.norm == 0, True ) |
---|
32 | assert_equal ( fftgrain.phas == 0, True ) |
---|
33 | |
---|
34 | def test_impulse(self): |
---|
35 | """ check the transform of one impulse at a random place """ |
---|
36 | from random import random |
---|
37 | from math import floor |
---|
38 | win_s = 256 |
---|
39 | i = floor(random()*win_s) |
---|
40 | impulse = pi * random() |
---|
41 | f = fft(win_s) |
---|
42 | timegrain = fvec(win_s) |
---|
43 | timegrain[i] = impulse |
---|
44 | fftgrain = f ( timegrain ) |
---|
45 | #self.plot_this ( fftgrain.phas ) |
---|
46 | assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 ) |
---|
47 | assert_equal ( fftgrain.phas <= pi, True) |
---|
48 | assert_equal ( fftgrain.phas >= -pi, True) |
---|
49 | |
---|
50 | def test_impulse_negative(self): |
---|
51 | """ check the transform of one impulse at a random place """ |
---|
52 | from random import random |
---|
53 | from math import floor |
---|
54 | win_s = 256 |
---|
55 | i = 0 |
---|
56 | impulse = -10. |
---|
57 | f = fft(win_s) |
---|
58 | timegrain = fvec(win_s) |
---|
59 | timegrain[i] = impulse |
---|
60 | fftgrain = f ( timegrain ) |
---|
61 | #self.plot_this ( fftgrain.phas ) |
---|
62 | assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 ) |
---|
63 | if impulse < 0: |
---|
64 | # phase can be pi or -pi, as it is not unwrapped |
---|
65 | assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 ) |
---|
66 | assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6) |
---|
67 | assert_almost_equal ( fftgrain.phas[-1], pi, decimal = 6) |
---|
68 | else: |
---|
69 | assert_equal ( fftgrain.phas[1:-1] == 0, True) |
---|
70 | assert_equal ( fftgrain.phas[0] == 0, True) |
---|
71 | assert_equal ( fftgrain.phas[-1] == 0, True) |
---|
72 | # now check the resynthesis |
---|
73 | synthgrain = f.rdo ( fftgrain ) |
---|
74 | #self.plot_this ( fftgrain.phas.T ) |
---|
75 | assert_equal ( fftgrain.phas <= pi, True) |
---|
76 | assert_equal ( fftgrain.phas >= -pi, True) |
---|
77 | #self.plot_this ( synthgrain - timegrain ) |
---|
78 | assert_almost_equal ( synthgrain, timegrain, decimal = 6 ) |
---|
79 | |
---|
80 | def test_impulse_at_zero(self): |
---|
81 | """ check the transform of one impulse at a index 0 """ |
---|
82 | win_s = 1024 |
---|
83 | impulse = pi |
---|
84 | f = fft(win_s) |
---|
85 | timegrain = fvec(win_s) |
---|
86 | timegrain[0] = impulse |
---|
87 | fftgrain = f ( timegrain ) |
---|
88 | #self.plot_this ( fftgrain.phas ) |
---|
89 | assert_equal ( fftgrain.phas[0], 0) |
---|
90 | assert_equal ( abs(fftgrain.phas[1]), 0) |
---|
91 | assert_almost_equal (fftgrain.norm[0], impulse, decimal = 6 ) |
---|
92 | |
---|
93 | def test_rdo_before_do(self): |
---|
94 | """ check running fft.rdo before fft.do works """ |
---|
95 | win_s = 1024 |
---|
96 | impulse = pi |
---|
97 | f = fft(win_s) |
---|
98 | fftgrain = cvec(win_s) |
---|
99 | t = f.rdo( fftgrain ) |
---|
100 | assert_equal ( t, 0 ) |
---|
101 | |
---|
102 | def plot_this(self, this): |
---|
103 | from pylab import plot, show |
---|
104 | plot ( this ) |
---|
105 | show () |
---|
106 | |
---|
107 | if __name__ == '__main__': |
---|
108 | from unittest import main |
---|
109 | main() |
---|
110 | |
---|