forked from ebezzam/python-dev-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fftconvolve.py
More file actions
56 lines (35 loc) · 1.14 KB
/
Copy pathtest_fftconvolve.py
File metadata and controls
56 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from pydevtips.fftconvolve import RFFTConvolve, FFTConvolve
import numpy as np
# create random signal
n = 1000
signal = np.random.randn(n)
# create filter
filter = np.random.randn(n)
# reference output
fft_naive = np.convolve(signal, filter, mode="full")
def test_rfft():
# create object
rfft_convolver = RFFTConvolve(filt=filter, length=len(signal))
# convolve
rfft_out = rfft_convolver(signal)
# check results
assert np.allclose(rfft_out, fft_naive)
def test_fft():
# create object
fft_convolver = FFTConvolve(filter=filter, length=len(signal))
# convolve
fft_out = fft_convolver(signal)
# check results
assert np.allclose(fft_out, fft_out)
def test_fft_complex():
# create complex signal
signal = np.random.randn(n) + 1j * np.random.randn(n)
# create complex filter
filter = np.random.randn(n) + 1j * np.random.randn(n)
# create object
fft_convolver = FFTConvolve(filter=filter, length=len(signal))
# convolve
fft_out = fft_convolver(signal)
# check results
fft_naive = np.convolve(signal, filter, mode="full")
assert np.allclose(fft_out, fft_naive)