Skip to content

Commit 200df73

Browse files
authored
Merge pull request #6 from AUAS-Pulsar/ffttest
added FFT tests
2 parents c375259 + d2f4a47 commit 200df73

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

.coverage

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ script:
1919
- pylint *.py
2020
- coverage run -a tests/test_filterbank.py
2121
- coverage run -a tests/test_header.py
22+
- coverage run -a tests/test_fft.py
2223

2324
after_success:
2425
- codecov

tests/test_fft.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,68 @@
1+
"""
2+
fourier.py test
3+
"""
4+
15
import unittest
26
import numpy as np
37

48
from context import fourier
59

6-
def fft1(x):
7-
L = len(x)
10+
def fft1(test_input):
11+
"""
12+
A simple fft implementation.
13+
This is the same function with which numpy tests their FFT funciton.
14+
"""
15+
L = len(test_input)
816
phase = -2j*np.pi*(np.arange(L)/float(L))
917
phase = np.arange(L).reshape(-1, 1) * phase
10-
return np.sum(x*np.exp(phase), axis=1)
18+
return np.sum(test_input*np.exp(phase), axis=1)
1119

1220
class TestFft(unittest.TestCase):
1321
"""
1422
Testclass for testing fourier transforms
1523
"""
1624

1725
def test_dft(self):
18-
x = np.random.random(30) + 1j*np.random.random(30)
19-
self.assertAlmostEqual(fft1(x), fourier.DFT_slow(x))
26+
"""
27+
Test for DFT function
28+
"""
29+
test_input = np.random.random(32) + 1j*np.random.random(32)
30+
test_input = np.asarray(test_input)
31+
self.assertAlmostEqual(fft1(test_input).all(), fourier.DFT_slow(test_input).all())
32+
2033

2134
def test_fft(self):
22-
x = np.random.random(30) + 1j*np.random.random(30)
23-
self.assertAlmostEqual(fft1(x), fourier.FFT_vectorized(x))
35+
"""
36+
Test for FFT_vectorized function
37+
"""
38+
test_input = np.random.random(32) + 1j*np.random.random(32)
39+
test_input = np.asarray(test_input)
40+
self.assertAlmostEqual(fft1(test_input).all(), fourier.FFT_vectorized(test_input).all())
41+
42+
43+
def test_wrong_array_size(self):
44+
"""
45+
Tests for correct input for the FFT_vectorized function.
46+
Fails if the input is not a power of 2.
47+
"""
48+
test_input = np.random.random(30) + 1j*np.random.random(30)
49+
with self.assertRaises(ValueError):
50+
fourier.FFT_vectorized(test_input)
51+
52+
53+
def test_fftfreq(self):
54+
"""
55+
Test for fftfreq function
56+
"""
57+
test_input = [0, 1, 2, 3, 4, -4, -3, -2, -1]
58+
test_input = np.asarray(test_input)
59+
self.assertAlmostEqual(9*fourier.fftfreq(9).all(), test_input.all())
60+
self.assertAlmostEqual(9*np.pi*fourier.fftfreq(9, np.pi).all(), test_input.all())
61+
test_input = [0, 1, 2, 3, 4, -5, -4, -3, -2, -1]
62+
test_input = np.asarray(test_input)
63+
self.assertAlmostEqual(10*fourier.fftfreq(10).all(), test_input.all())
64+
self.assertAlmostEqual(10*np.pi*fourier.fftfreq(10, np.pi).all(), test_input.all())
65+
2466

2567
if __name__ == '__main__':
2668
unittest.main()

tests/test_plot.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
import numpy as np
3+
4+
from context import plot
5+
6+
class TestPlot(unittest.TestCase):
7+
"""
8+
Testclass for testing the plot class
9+
"""
10+
11+
def test_psd(self):
12+

0 commit comments

Comments
 (0)