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