source: python/tests/test_fft.py @ 392ad1c

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

python/tests: fix most prospect warnings

  • Property mode set to 100755
File size: 6.1 KB
RevLine 
[75e715f]1#! /usr/bin/env python
[1ebf8770]2
[0b6d23d]3from unittest import main
[ad5eae8]4from numpy.testing import TestCase
[0536612]5from numpy.testing import assert_equal, assert_almost_equal
[0b6d23d]6import numpy as np
[4c01c0f]7from aubio import fvec, fft, cvec
[0b6d23d]8from math import pi, floor
9from random import random
[0536612]10
11class aubio_fft_test_case(TestCase):
12
[0a42239]13    def test_members(self):
14        """ check members are set correctly """
15        win_s = 2048
16        f = fft(win_s)
17        assert_equal (f.win_s, win_s)
[0536612]18
[0a42239]19    def test_output_dimensions(self):
20        """ check the dimensions of output """
21        win_s = 1024
22        timegrain = fvec(win_s)
23        f = fft (win_s)
24        fftgrain = f (timegrain)
[ad5eae8]25        del f
26        assert_equal (fftgrain.norm.shape, (win_s/2+1,))
27        assert_equal (fftgrain.phas.shape, (win_s/2+1,))
[0536612]28
[0a42239]29    def test_zeros(self):
30        """ check the transform of zeros is all zeros """
31        win_s = 512
32        timegrain = fvec(win_s)
33        f = fft (win_s)
34        fftgrain = f (timegrain)
35        assert_equal ( fftgrain.norm, 0 )
36        assert_equal ( fftgrain.phas, 0 )
[0536612]37
[0a42239]38    def test_impulse(self):
39        """ check the transform of one impulse at a random place """
40        win_s = 256
[2f4d9b7]41        i = int(floor(random()*win_s))
[0a42239]42        impulse = pi * random()
43        f = fft(win_s)
44        timegrain = fvec(win_s)
45        timegrain[i] = impulse
46        fftgrain = f ( timegrain )
47        #self.plot_this ( fftgrain.phas )
48        assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 )
49        assert_equal ( fftgrain.phas <= pi, True)
50        assert_equal ( fftgrain.phas >= -pi, True)
[0536612]51
[0a42239]52    def test_impulse_negative(self):
[0b6d23d]53        """ check the transform of a negative impulse at a random place """
[0a42239]54        win_s = 256
[0b6d23d]55        i = int(floor(random()*win_s))
56        impulse = -.1
[0a42239]57        f = fft(win_s)
58        timegrain = fvec(win_s)
[0b6d23d]59        timegrain[0] = 0
[0a42239]60        timegrain[i] = impulse
61        fftgrain = f ( timegrain )
62        #self.plot_this ( fftgrain.phas )
[0b6d23d]63        assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 5 )
[0a42239]64        if impulse < 0:
65            # phase can be pi or -pi, as it is not unwrapped
[0b6d23d]66            #assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 )
[0a42239]67            assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6)
[0b6d23d]68            assert_almost_equal ( np.fmod(fftgrain.phas[-1], pi), 0, decimal = 6)
[0a42239]69        else:
[0b6d23d]70            #assert_equal ( fftgrain.phas[1:-1] == 0, True)
71            assert_equal ( fftgrain.phas[0], 0)
72            assert_almost_equal ( np.fmod(fftgrain.phas[-1], pi), 0, decimal = 6)
[0a42239]73        # now check the resynthesis
74        synthgrain = f.rdo ( fftgrain )
75        #self.plot_this ( fftgrain.phas.T )
76        assert_equal ( fftgrain.phas <= pi, True)
77        assert_equal ( fftgrain.phas >= -pi, True)
78        #self.plot_this ( synthgrain - timegrain )
79        assert_almost_equal ( synthgrain, timegrain, decimal = 6 )
[0536612]80
[0a42239]81    def test_impulse_at_zero(self):
82        """ check the transform of one impulse at a index 0 """
83        win_s = 1024
84        impulse = pi
85        f = fft(win_s)
86        timegrain = fvec(win_s)
87        timegrain[0] = impulse
88        fftgrain = f ( timegrain )
89        #self.plot_this ( fftgrain.phas )
90        assert_equal ( fftgrain.phas[0], 0)
91        # could be 0 or -0 depending on fft implementation (0 for fftw3, -0 for ooura)
92        assert_almost_equal ( fftgrain.phas[1], 0)
93        assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 )
[0536612]94
[0a42239]95    def test_rdo_before_do(self):
96        """ check running fft.rdo before fft.do works """
97        win_s = 1024
98        f = fft(win_s)
99        fftgrain = cvec(win_s)
100        t = f.rdo( fftgrain )
101        assert_equal ( t, 0 )
[0536612]102
[0a42239]103    def plot_this(self, this):
104        from pylab import plot, show
105        plot ( this )
106        show ()
[0536612]107
[ee092a8]108    def test_local_fftgrain(self):
109        """ check aubio.fft() result can be accessed after deletion """
110        def compute_grain(impulse):
111            win_s = 1024
112            timegrain = fvec(win_s)
113            timegrain[0] = impulse
114            f = fft(win_s)
115            fftgrain = f ( timegrain )
116            return fftgrain
117        impulse = pi
118        fftgrain = compute_grain(impulse)
119        assert_equal ( fftgrain.phas[0], 0)
120        assert_almost_equal ( fftgrain.phas[1], 0)
121        assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 )
122
123    def test_local_reconstruct(self):
124        """ check aubio.fft.rdo() result can be accessed after deletion """
125        def compute_grain(impulse):
126            win_s = 1024
127            timegrain = fvec(win_s)
128            timegrain[0] = impulse
129            f = fft(win_s)
130            fftgrain = f ( timegrain )
131            r = f.rdo(fftgrain)
132            return r
133        impulse = pi
134        r = compute_grain(impulse)
135        assert_almost_equal ( r[0], impulse, decimal = 6)
136        assert_almost_equal ( r[1:], 0)
137
[d03ee4b]138    def test_large_input_timegrain(self):
139        win_s = 1024
140        f = fft(win_s)
141        t = fvec(win_s + 1)
142        with self.assertRaises(ValueError):
[437ef07]143            f(t)
[d03ee4b]144
145    def test_small_input_timegrain(self):
146        win_s = 1024
147        f = fft(win_s)
148        t = fvec(1)
149        with self.assertRaises(ValueError):
150            f(t)
151
[437ef07]152    def test_large_input_fftgrain(self):
153        win_s = 1024
154        f = fft(win_s)
[7498e48]155        s = cvec(win_s + 5)
[437ef07]156        with self.assertRaises(ValueError):
157            f.rdo(s)
158
[ad5eae8]159    def test_small_input_fftgrain(self):
[437ef07]160        win_s = 1024
161        f = fft(win_s)
162        s = cvec(16)
163        with self.assertRaises(ValueError):
164            f.rdo(s)
165
[fcef3fd]166class aubio_fft_wrong_params(TestCase):
167
168    def test_wrong_buf_size(self):
169        win_s = -1
170        with self.assertRaises(ValueError):
171            fft(win_s)
172
173    def test_buf_size_not_power_of_two(self):
174        # when compiled with fftw3, aubio supports non power of two fft sizes
175        win_s = 320
176        try:
177            with self.assertRaises(RuntimeError):
178                fft(win_s)
[0b6d23d]179        except AssertionError:
[fcef3fd]180            self.skipTest('creating aubio.fft with size %d did not fail' % win_s)
181
182    def test_buf_size_too_small(self):
183        win_s = 1
184        with self.assertRaises(RuntimeError):
185            fft(win_s)
186
[0536612]187if __name__ == '__main__':
[0a42239]188    main()
Note: See TracBrowser for help on using the repository browser.