Fast Fourier Transform (fft)#
The fft module provides functions for computing discrete Fourier transforms.
Example#
import numpycore as np
# Create a signal
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 120 * t)
# Compute FFT
spectrum = np.fft.fft(signal)
# Compute inverse FFT
reconstructed = np.fft.ifft(spectrum)
# Frequency bins
freqs = np.fft.fftfreq(len(signal), d=t[1] - t[0])
Standard FFTs#
Function |
Description |
Example |
|---|---|---|
fft(a) |
1-D discrete Fourier Transform |
|
ifft(a) |
1-D inverse discrete Fourier Transform |
|
fft2(a) |
2-D discrete Fourier Transform |
|
ifft2(a) |
2-D inverse discrete Fourier Transform |
|
fftn(a) |
N-D discrete Fourier Transform |
|
ifftn(a) |
N-D inverse discrete Fourier Transform |
Real FFTs#
Hermitian FFTs#
Helper Functions#
Code Examples#
The following examples are extracted from the test suite.
rfft2 (test_phase4_fft.py:23)
13 print(f" NumpyCore result: {nc_result}")
14 sys.exit(1)
15 print(f"[OK] {test_name}: PASSED")
16
17def test_rfft2():
18 """Test 2D real FFT"""
19 data_np = test_bind.np.random.randn(8, 8)
20 data_nc = test_bind.numpycore.array(data_np.tolist())
21
22 result_np = test_bind.np.fft.rfft2(data_np)
23 result_nc = test_bind.numpycore.fft.rfft2(data_nc)
24
25 assert_arrays_equal(result_np, result_nc, "rfft2")
26
27def test_irfft2():
28 """Test inverse 2D real FFT"""
29 data_np = test_bind.np.random.randn(8, 8)
30 data_nc = test_bind.numpycore.array(data_np.tolist())
31
32 fft_np = test_bind.np.fft.rfft2(data_np)
33 fft_nc = test_bind.numpycore.fft.rfft2(data_nc)
irfft2 (test_phase4_fft.py:36)
26def test_irfft2():
27 """Test inverse 2D real FFT"""
28 data_np = test_bind.np.random.randn(8, 8)
29 data_nc = test_bind.numpycore.array(data_np.tolist())
30
31 fft_np = test_bind.np.fft.rfft2(data_np)
32 fft_nc = test_bind.numpycore.fft.rfft2(data_nc)
33
34 result_np = test_bind.np.fft.irfft2(fft_np)
35 result_nc = test_bind.numpycore.fft.irfft2(fft_nc)
36
37 assert_arrays_equal(result_np, result_nc, "irfft2")
38
39def test_rfftn():
40 """Test N-D real FFT"""
41 data_np = test_bind.np.random.randn(4, 4, 4)
42 data_nc = test_bind.numpycore.array(data_np.tolist())
43
44 result_np = test_bind.np.fft.rfftn(data_np)
45 result_nc = test_bind.numpycore.fft.rfftn(data_nc)
rfftn (test_phase4_fft.py:46)
36 result_nc = test_bind.numpycore.fft.irfft2(fft_nc)
37
38 assert_arrays_equal(result_np, result_nc, "irfft2")
39
40def test_rfftn():
41 """Test N-D real FFT"""
42 data_np = test_bind.np.random.randn(4, 4, 4)
43 data_nc = test_bind.numpycore.array(data_np.tolist())
44
45 result_np = test_bind.np.fft.rfftn(data_np)
46 result_nc = test_bind.numpycore.fft.rfftn(data_nc)
47
48 assert_arrays_equal(result_np, result_nc, "rfftn")
49
50def test_irfftn():
51 """Test inverse N-D real FFT"""
52 data_np = test_bind.np.random.randn(4, 4, 4)
53 data_nc = test_bind.numpycore.array(data_np.tolist())
54
55 fft_np = test_bind.np.fft.rfftn(data_np)
56 fft_nc = test_bind.numpycore.fft.rfftn(data_nc)
irfftn (test_phase4_fft.py:59)
49def test_irfftn():
50 """Test inverse N-D real FFT"""
51 data_np = test_bind.np.random.randn(4, 4, 4)
52 data_nc = test_bind.numpycore.array(data_np.tolist())
53
54 fft_np = test_bind.np.fft.rfftn(data_np)
55 fft_nc = test_bind.numpycore.fft.rfftn(data_nc)
56
57 result_np = test_bind.np.fft.irfftn(fft_np)
58 result_nc = test_bind.numpycore.fft.irfftn(fft_nc)
59
60 assert_arrays_equal(result_np, result_nc, "irfftn")
61
62def test_hfft():
63 """Test Hermitian FFT"""
64 # Create a Hermitian symmetric array
65 # For a real signal, rfft produces Hermitian symmetric output
66 signal_np = test_bind.np.random.randn(16)
67 hermitian_np = test_bind.np.fft.rfft(signal_np)
68 hermitian_nc = test_bind.numpycore.array(hermitian_np.tolist())
hfft (test_phase4_fft.py:72)
62def test_hfft():
63 """Test Hermitian FFT"""
64 # Create a Hermitian symmetric array
65 # For a real signal, rfft produces Hermitian symmetric output
66 signal_np = test_bind.np.random.randn(16)
67 hermitian_np = test_bind.np.fft.rfft(signal_np)
68 hermitian_nc = test_bind.numpycore.array(hermitian_np.tolist())
69
70 result_np = test_bind.np.fft.hfft(hermitian_np)
71 result_nc = test_bind.numpycore.fft.hfft(hermitian_nc)
72
73 assert_arrays_equal(result_np, result_nc, "hfft")
74
75def test_ihfft():
76 """Test inverse Hermitian FFT"""
77 data_np = test_bind.np.random.randn(16)
78 data_nc = test_bind.numpycore.array(data_np.tolist())
79
80 result_np = test_bind.np.fft.ihfft(data_np)
81 result_nc = test_bind.numpycore.fft.ihfft(data_nc)
ihfft (test_phase4_fft.py:82)
72 result_nc = test_bind.numpycore.fft.hfft(hermitian_nc)
73
74 assert_arrays_equal(result_np, result_nc, "hfft")
75
76def test_ihfft():
77 """Test inverse Hermitian FFT"""
78 data_np = test_bind.np.random.randn(16)
79 data_nc = test_bind.numpycore.array(data_np.tolist())
80
81 result_np = test_bind.np.fft.ihfft(data_np)
82 result_nc = test_bind.numpycore.fft.ihfft(data_nc)
83
84 assert_arrays_equal(result_np, result_nc, "ihfft")
85
86def test_round_trip_rfft2():
87 """Test rfft2 -> irfft2 round trip"""
88 data_np = test_bind.np.random.randn(6, 8)
89 data_nc = test_bind.numpycore.array(data_np.tolist())
90
91 # Forward transform
92 fft_np = test_bind.np.fft.rfft2(data_np)
fftshift (test_fft_and_bitwise.py:185)
175 assert_arrays_equal(np_result, nc_result, "rfftfreq")
176
177
178def test_fftshift():
179 """Test np.fft.fftshift() to shift zero-frequency to center"""
180 np_arr = test_bind.np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
181
182 nc_arr = test_bind.numpycore.arange(0.0, 8.0, 1.0)
183
184 np_result = test_bind.np.fft.fftshift(np_arr)
185 nc_result = test_bind.numpycore.fft.fftshift(nc_arr) # Use numpycore implementation
186
187 assert_arrays_equal(np_result, nc_result, "fftshift")
188
189
190def test_ifftshift():
191 """Test np.fft.ifftshift() to undo fftshift"""
192 np_arr = test_bind.np.array([4.0, 5.0, 6.0, 7.0, 0.0, 1.0, 2.0, 3.0])
193
194 nc_arr = test_bind.numpycore.zeros([8])
195 for i, val in enumerate([4.0, 5.0, 6.0, 7.0, 0.0, 1.0, 2.0, 3.0]):
ifftshift (test_fft_and_bitwise.py:199)
189def test_ifftshift():
190 """Test np.fft.ifftshift() to undo fftshift"""
191 np_arr = test_bind.np.array([4.0, 5.0, 6.0, 7.0, 0.0, 1.0, 2.0, 3.0])
192
193 nc_arr = test_bind.numpycore.zeros([8])
194 for i, val in enumerate([4.0, 5.0, 6.0, 7.0, 0.0, 1.0, 2.0, 3.0]):
195 nc_arr.itemset(i, val)
196
197 np_result = test_bind.np.fft.ifftshift(np_arr)
198 nc_result = test_bind.numpycore.fft.ifftshift(nc_arr) # Use numpycore implementation
199
200 assert_arrays_equal(np_result, nc_result, "ifftshift")
201
202
203def test_fftn_1d():
204 """Test np.fft.fftn() for 1D FFT"""
205 np_signal = test_bind.np.array([1.0, 2.0, 3.0, 4.0])
206 nc_signal = test_bind.numpycore.arange(1.0, 5.0, 1.0)
207
208 np_result = test_bind.np.fft.fftn(np_signal)