2022-09-03 14:07:44 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Mon Jan 15 19:45:33 2018
|
|
|
|
|
|
|
|
@author: anne
|
|
|
|
"""
|
|
|
|
import numpy as np
|
|
|
|
from lasp import Fft, getFreq
|
|
|
|
|
|
|
|
def test_forward_fft():
|
|
|
|
"""
|
|
|
|
Test that our FFT implementation equals Numpy's rfft implementation
|
|
|
|
"""
|
|
|
|
nfft = 2048
|
|
|
|
t = np.linspace(0, 1.0, nfft, endpoint=False)
|
|
|
|
freq = 10
|
|
|
|
omg = 2*np.pi*freq
|
|
|
|
sig = np.cos(omg*t)+10
|
|
|
|
sig = np.random.randn(nfft)
|
|
|
|
fft_lasp = Fft(nfft)
|
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
res_lasp = fft_lasp.fft(sig)
|
2022-09-03 14:07:44 +00:00
|
|
|
res_npy = np.fft.rfft(sig)
|
|
|
|
assert(np.isclose(np.linalg.norm(res_lasp- res_npy), 0))
|
|
|
|
|
|
|
|
def test_backward_fft():
|
|
|
|
"""
|
|
|
|
Test that our backward FFT implementation equals Numpy's rfft implementation
|
|
|
|
"""
|
|
|
|
nfft = 2048
|
|
|
|
freq = getFreq(nfft, nfft)
|
|
|
|
|
2024-06-03 15:28:51 +00:00
|
|
|
# Sig = zeros(nfft//2+1, dtype=complex)
|
2022-09-03 14:07:44 +00:00
|
|
|
Sigr = np.random.randn(nfft//2+1)
|
|
|
|
Sigi = np.random.randn(nfft//2+1)
|
|
|
|
|
|
|
|
Sig = Sigr + 1j*Sigi
|
|
|
|
|
|
|
|
fft_lasp = Fft(nfft)
|
2022-10-11 12:50:44 +00:00
|
|
|
sig_lasp = fft_lasp.ifft(Sig)
|
2022-09-03 14:07:44 +00:00
|
|
|
sig_py = np.fft.irfft(Sig)
|
|
|
|
|
|
|
|
assert(np.isclose(np.linalg.norm(sig_py- sig_lasp), 0))
|
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_forward_fft()
|
2022-10-11 12:50:44 +00:00
|
|
|
test_backward_fft()
|