def test_fft_convolution(self):
x = np.array([1, 2, 3])
h = np.array([0, 1, 0.5])
expected_result = np.array([1., 2.5, 4.])
result_np = np.convolve(x, h, 'same')
self.assertTrue(np.array_equal(result_np, expected_result))
result_fft = Filter.fft_convolve_1d(x, h)
self.assertTrue(np.array_equal(result_fft, expected_result))
x = np.linspace(0, 1, num=10 ** 3).astype(np.complex64)
h = Filter.design_windowed_sinc_bandpass(0.1, 0.4, 0.01)
# fft convolve is faster if IR is round about 400 samples or windowed sinc has bandwidth of 0.01
#print(len(h))
t_np = time.time()
result_np = np.convolve(x, h, mode="same")
t_np = time.time() - t_np
t_fft = time.time()
result_fft = Filter.fft_convolve_1d(x, h)
t_fft = time.time() - t_fft
np.testing.assert_array_almost_equal(result_np, result_fft)
#print("fft convolve time", t_fft, "np convolve time", t_np)
评论列表
文章目录