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)
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.assertEqual(len(expected_result), len(result_fft)) for i in range(len(expected_result)): self.assertAlmostEqual(expected_result[i], result_fft[i], places=8, msg=str(i)) 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 result_np = np.convolve(x, h, mode="same") result_fft = Filter.fft_convolve_1d(x, h) np.testing.assert_array_almost_equal(result_np, result_fft)
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.assertEqual(len(expected_result), len(result_fft)) for i in range(len(expected_result)): self.assertAlmostEqual(expected_result[i], result_fft[i], places=8, msg=str(i)) 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 result_np = np.convolve(x, h, mode="same") result_fft = Filter.fft_convolve_1d(x, h) np.testing.assert_array_almost_equal(result_np, result_fft)
def test_bandpass_h(self): f_low = 0.31 f_high = 0.44 bw = 0.01 h = Filter.design_windowed_sinc_bandpass(f_low=f_low, f_high=f_high, bw=bw) #h = Filter.design_windowed_sinc_lpf(0.42, bw=0.08) impulse = np.exp(1j * np.linspace(0, 1, 50)) plt.subplot("221") plt.title("f_low={} f_high={} bw=0.05".format(f_low, f_high, bw)) plt.plot(np.fft.rfft(h)) plt.subplot("222") plt.plot(h) plt.show()