def test_butterfly_4pt_18_17(self): d_real = np.array([1, 1, 1, 1], dtype=np.int32) d_imag = np.array([0, 0, 0, 0], dtype=np.int32) fpt = FixedPointType(18, 17) out_real, out_imag = fp_fft.butterfly_r2(d_real, d_imag, 1, 2, fpt, fpt, fpt, False) np.testing.assert_equal(out_real, np.array([2, 2, 0, 0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0, 0, 0, 0], dtype=np.int32))
def test_fft_8pt_dc_large(self): d_real = np.array([65536] * 8, dtype=np.int32) d_imag = np.array([0] * 8, dtype=np.int32) fpt = FixedPointType(18, 17) shifts = np.array([1, 1, 1]) out_real, out_imag = fp_fft.fft_r2(d_real, d_imag, 3, fpt, fpt, fpt, shifts) np.testing.assert_equal(out_real, np.array([65536,0,0,0,0,0,0,0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0,0,0,0,0,0,0,0], dtype=np.int32))
def test_fft_8pt_two_tone(self): d_real = np.array([1, 0,-1, 0, 1, 0,-1, 0], dtype=np.int32) + 1 d_imag = np.array([0, 1, 0,-1, 0, 1, 0,-1], dtype=np.int32) fpt = FixedPointType(18, 17) shifts = np.array([0, 0, 0]) out_real, out_imag = fp_fft.fft_r2(d_real, d_imag, 3, fpt, fpt, fpt, shifts) np.testing.assert_equal(out_real, np.array([8,0,8,0,0,0,0,0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0,0,0,0,0,0,0,0], dtype=np.int32))
def test_butterfly_4pt_neg1f(self): d_real = np.array([1, 0, -1, 0], dtype=np.int32) d_imag = np.array([0, -1, 0, 1], dtype=np.int32) fpt = FixedPointType(8, 7) out_real, out_imag = fp_fft.butterfly_r2(d_real, d_imag, 1, 2, fpt, fpt, fpt, False) np.testing.assert_equal(out_real, np.array([0, 0, 2, 0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0, 0, 0, -2], dtype=np.int32)) out_real, out_imag = fp_fft.butterfly_r2(out_real, out_imag, 2, 2, fpt, fpt, fpt, False) np.testing.assert_equal(out_real, np.array([0, 0, 0, 4], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0, 0, 0, 0], dtype=np.int32))
def test_butterfly_shift(self): d_real = np.array([1, 1, 1, 1], dtype=np.int32) d_imag = np.array([0, 0, 0, 0], dtype=np.int32) fpt = FixedPointType(8, 7) out_real, out_imag = fp_fft.butterfly_r2(d_real, d_imag, 1, 2, fpt, fpt, fpt, True) np.testing.assert_equal(out_real, np.array([1, 1, 0, 0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0, 0, 0, 0], dtype=np.int32)) out_real, out_imag = fp_fft.butterfly_r2(out_real, out_imag, 2, 2, fpt, fpt, fpt, True) np.testing.assert_equal(out_real, np.array([1, 0, 0, 0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0, 0, 0, 0], dtype=np.int32))
def test_fft_batch(self): NSTAGES = 3 NCHAN = 2**NSTAGES NBATCH = 1 d_real = np.array([[1] * NCHAN] * NBATCH, dtype=np.int32) d_imag = np.array([[0] * NCHAN] * NBATCH, dtype=np.int32) fpt = FixedPointType(18, 17) out_real, out_imag = fp_fft.fft_r2(d_real, d_imag, NSTAGES, fpt, fpt, fpt ) np.testing.assert_equal(out_real[...,0], np.array([NCHAN] * NBATCH, dtype=np.int32)) np.testing.assert_equal(out_real[...,1:], 0)
def twiddle_r2(stage, stages, fptype): ind = np.arange(2**(stages - 1), dtype=np.int32) ind = bit_reverse(ind >> (stages - stage), stage - 1) theta = -np.pi * FixedPointType(stage, stage - 1).to_float(ind) tw_real = np.cos(theta) tw_imag = np.sin(theta) mx = (2**(fptype.bit_width - 1) - 1) / 2**(fptype.bit_width - 1) tw_real = fptype.from_float(tw_real.clip(-mx, mx)) tw_imag = fptype.from_float(tw_imag.clip(-mx, mx)) return tw_real, tw_imag
def test_twiddle_r2(self): fpt = FixedPointType(18, 17) tw_r, tw_i = fp_fft.twiddle_r2(1, 3, fpt) self.assertEqual(tw_r.shape, (4,)) self.assertEqual(tw_i.shape, (4,)) theta = np.arctan2(fpt.to_float(tw_i), fpt.to_float(tw_r)) np.testing.assert_almost_equal(theta, np.array([0, 0, 0, 0])) tw_r, tw_i = fp_fft.twiddle_r2(2, 3, fpt) theta = np.arctan2(fpt.to_float(tw_i), fpt.to_float(tw_r)) np.testing.assert_almost_equal(theta, np.array([0, 0, -np.pi/2, -np.pi/2])) tw_r, tw_i = fp_fft.twiddle_r2(3, 3, fpt) theta = np.arctan2(fpt.to_float(tw_i), fpt.to_float(tw_r)) np.testing.assert_almost_equal(theta, np.array([0, -np.pi/2, -np.pi/4, -3*np.pi/4]))
def test_fft_8pt(self): d_real = np.array([7, 7, 7, 7, 7, 7, 7, 7], dtype=np.int32) d_imag = np.array([0, 0, 0, 0, 0, 0, 0, 0], dtype=np.int32) fpt = FixedPointType(8, 7) shifts = np.array([1, 1, 1]) out_real, out_imag = fp_fft.fft_r2(d_real, d_imag, 3, fpt, fpt, fpt, shifts) np.testing.assert_equal(out_real, np.array([7,0,0,0,0,0,0,0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0,0,0,0,0,0,0,0], dtype=np.int32)) shifts = np.array([0, 0, 0]) out_real, out_imag = fp_fft.fft_r2(d_real, d_imag, 3, fpt, fpt, fpt, shifts) np.testing.assert_equal(out_real, np.array([56,0,0,0,0,0,0,0], dtype=np.int32)) np.testing.assert_equal(out_imag, np.array([0 ,0,0,0,0,0,0,0], dtype=np.int32))