[0536612] | 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 |
---|
[4c01c0f] | 4 | from aubio import fvec, fft, cvec |
---|
[0536612] | 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() |
---|
[1a6ef2c] | 12 | assert_equal (f.win_s, 1024) |
---|
[0536612] | 13 | |
---|
| 14 | def test_output_dimensions(self): |
---|
| 15 | """ check the dimensions of output """ |
---|
[1a6ef2c] | 16 | win_s = 1024 |
---|
| 17 | timegrain = fvec(win_s) |
---|
| 18 | f = fft(win_s) |
---|
[0536612] | 19 | fftgrain = f (timegrain) |
---|
| 20 | assert_equal (fftgrain.norm, 0) |
---|
[1a6ef2c] | 21 | assert_equal (shape(fftgrain.norm), (win_s/2+1,)) |
---|
[0536612] | 22 | assert_equal (fftgrain.phas, 0) |
---|
[1a6ef2c] | 23 | assert_equal (shape(fftgrain.phas), (win_s/2+1,)) |
---|
[0536612] | 24 | |
---|
| 25 | def test_zeros(self): |
---|
| 26 | """ check the transform of zeros """ |
---|
[1a6ef2c] | 27 | win_s = 512 |
---|
| 28 | timegrain = fvec(win_s) |
---|
| 29 | f = fft(win_s) |
---|
[0536612] | 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 |
---|
[1a6ef2c] | 38 | win_s = 256 |
---|
[0536612] | 39 | i = floor(random()*win_s) |
---|
| 40 | impulse = pi * random() |
---|
[1a6ef2c] | 41 | f = fft(win_s) |
---|
| 42 | timegrain = fvec(win_s) |
---|
| 43 | timegrain[i] = impulse |
---|
[0536612] | 44 | fftgrain = f ( timegrain ) |
---|
[1a6ef2c] | 45 | #self.plot_this ( fftgrain.phas ) |
---|
[0536612] | 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 |
---|
[1a6ef2c] | 54 | win_s = 256 |
---|
[0536612] | 55 | i = 0 |
---|
| 56 | impulse = -10. |
---|
[1a6ef2c] | 57 | f = fft(win_s) |
---|
| 58 | timegrain = fvec(win_s) |
---|
| 59 | timegrain[i] = impulse |
---|
[0536612] | 60 | fftgrain = f ( timegrain ) |
---|
[1a6ef2c] | 61 | #self.plot_this ( fftgrain.phas ) |
---|
[0536612] | 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 |
---|
[1a6ef2c] | 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) |
---|
[0536612] | 68 | else: |
---|
[1a6ef2c] | 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) |
---|
[0536612] | 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): |
---|
[1a6ef2c] | 81 | """ check the transform of one impulse at a index 0 """ |
---|
| 82 | win_s = 1024 |
---|
[0536612] | 83 | impulse = pi |
---|
[1a6ef2c] | 84 | f = fft(win_s) |
---|
| 85 | timegrain = fvec(win_s) |
---|
| 86 | timegrain[0] = impulse |
---|
[0536612] | 87 | fftgrain = f ( timegrain ) |
---|
| 88 | #self.plot_this ( fftgrain.phas ) |
---|
| 89 | assert_equal ( fftgrain.phas[0], 0) |
---|
| 90 | assert_equal ( fftgrain.phas[1], 0) |
---|
[1a6ef2c] | 91 | assert_almost_equal (fftgrain.norm[0], impulse, decimal = 6 ) |
---|
[0536612] | 92 | |
---|
| 93 | def test_rdo_before_do(self): |
---|
| 94 | """ check running fft.rdo before fft.do works """ |
---|
[1a6ef2c] | 95 | win_s = 1024 |
---|
[0536612] | 96 | impulse = pi |
---|
[1a6ef2c] | 97 | f = fft(win_s) |
---|
| 98 | fftgrain = cvec(win_s) |
---|
[0536612] | 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 | |
---|