0
# Fast Fourier Transform
1
2
Discrete Fourier Transform operations through numpy.fft for signal processing and frequency domain analysis. Provides 1D, 2D, and N-D transforms with both complex and real-valued inputs.
3
4
## Capabilities
5
6
### Standard FFTs
7
8
Standard discrete Fourier transforms for complex inputs.
9
10
```python { .api }
11
def fft.fft(a, n=None, axis=-1, norm=None, plan=None):
12
"""
13
Compute 1-D discrete Fourier Transform.
14
15
Parameters:
16
- a: array_like, input array
17
- n: int, length of transformed axis
18
- axis: int, axis over which to compute FFT
19
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
20
- plan: plan object (future use)
21
22
Returns:
23
ndarray: Complex-valued FFT result
24
"""
25
26
def fft.ifft(a, n=None, axis=-1, norm=None, plan=None):
27
"""
28
Compute 1-D inverse discrete Fourier Transform.
29
30
Parameters:
31
- a: array_like, input array
32
- n: int, length of transformed axis
33
- axis: int, axis over which to compute IFFT
34
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
35
- plan: plan object (future use)
36
37
Returns:
38
ndarray: Complex-valued inverse FFT result
39
"""
40
41
def fft.fft2(a, s=None, axes=(-2, -1), norm=None, plan=None):
42
"""
43
Compute 2-D discrete Fourier Transform.
44
45
Parameters:
46
- a: array_like, input array
47
- s: sequence of ints, shape of result
48
- axes: sequence of ints, axes over which to compute FFT
49
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
50
- plan: plan object (future use)
51
52
Returns:
53
ndarray: Complex-valued 2D FFT result
54
"""
55
56
def fft.ifft2(a, s=None, axes=(-2, -1), norm=None, plan=None):
57
"""
58
Compute 2-D inverse discrete Fourier Transform.
59
60
Parameters:
61
- a: array_like, input array
62
- s: sequence of ints, shape of result
63
- axes: sequence of ints, axes over which to compute IFFT
64
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
65
- plan: plan object (future use)
66
67
Returns:
68
ndarray: Complex-valued 2D inverse FFT result
69
"""
70
71
def fft.fftn(a, s=None, axes=None, norm=None, plan=None):
72
"""
73
Compute N-D discrete Fourier Transform.
74
75
Parameters:
76
- a: array_like, input array
77
- s: sequence of ints, shape of result
78
- axes: sequence of ints, axes over which to compute FFT
79
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
80
- plan: plan object (future use)
81
82
Returns:
83
ndarray: Complex-valued N-D FFT result
84
"""
85
86
def fft.ifftn(a, s=None, axes=None, norm=None, plan=None):
87
"""
88
Compute N-D inverse discrete Fourier Transform.
89
90
Parameters:
91
- a: array_like, input array
92
- s: sequence of ints, shape of result
93
- axes: sequence of ints, axes over which to compute IFFT
94
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
95
- plan: plan object (future use)
96
97
Returns:
98
ndarray: Complex-valued N-D inverse FFT result
99
"""
100
```
101
102
### Real FFTs
103
104
Optimized transforms for real-valued inputs.
105
106
```python { .api }
107
def fft.rfft(a, n=None, axis=-1, norm=None, plan=None):
108
"""
109
Compute 1-D discrete Fourier Transform for real input.
110
111
Parameters:
112
- a: array_like, real-valued input array
113
- n: int, length of transformed axis
114
- axis: int, axis over which to compute FFT
115
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
116
- plan: plan object (future use)
117
118
Returns:
119
ndarray: Complex-valued FFT result (length n//2 + 1)
120
"""
121
122
def fft.irfft(a, n=None, axis=-1, norm=None, plan=None):
123
"""
124
Compute inverse of rfft.
125
126
Parameters:
127
- a: array_like, complex input array
128
- n: int, length of output (should be even)
129
- axis: int, axis over which to compute IFFT
130
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
131
- plan: plan object (future use)
132
133
Returns:
134
ndarray: Real-valued inverse FFT result
135
"""
136
137
def fft.rfft2(a, s=None, axes=(-2, -1), norm=None, plan=None):
138
"""
139
Compute 2-D discrete Fourier Transform for real input.
140
141
Parameters:
142
- a: array_like, real-valued input array
143
- s: sequence of ints, shape of result
144
- axes: sequence of ints, axes over which to compute FFT
145
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
146
- plan: plan object (future use)
147
148
Returns:
149
ndarray: Complex-valued 2D FFT result
150
"""
151
152
def fft.irfft2(a, s=None, axes=(-2, -1), norm=None, plan=None):
153
"""
154
Compute 2-D inverse discrete Fourier Transform for real output.
155
156
Parameters:
157
- a: array_like, complex input array
158
- s: sequence of ints, shape of output
159
- axes: sequence of ints, axes over which to compute IFFT
160
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
161
- plan: plan object (future use)
162
163
Returns:
164
ndarray: Real-valued 2D inverse FFT result
165
"""
166
167
def fft.rfftn(a, s=None, axes=None, norm=None, plan=None):
168
"""
169
Compute N-D discrete Fourier Transform for real input.
170
171
Parameters:
172
- a: array_like, real-valued input array
173
- s: sequence of ints, shape of result
174
- axes: sequence of ints, axes over which to compute FFT
175
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
176
- plan: plan object (future use)
177
178
Returns:
179
ndarray: Complex-valued N-D FFT result
180
"""
181
182
def fft.irfftn(a, s=None, axes=None, norm=None, plan=None):
183
"""
184
Compute N-D inverse discrete Fourier Transform for real output.
185
186
Parameters:
187
- a: array_like, complex input array
188
- s: sequence of ints, shape of output
189
- axes: sequence of ints, axes over which to compute IFFT
190
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
191
- plan: plan object (future use)
192
193
Returns:
194
ndarray: Real-valued N-D inverse FFT result
195
"""
196
```
197
198
### Hermitian FFTs
199
200
Transforms for Hermitian symmetric inputs.
201
202
```python { .api }
203
def fft.hfft(a, n=None, axis=-1, norm=None, plan=None):
204
"""
205
Compute FFT of signal with Hermitian symmetry.
206
207
Parameters:
208
- a: array_like, input array with Hermitian symmetry
209
- n: int, length of transformed axis
210
- axis: int, axis over which to compute FFT
211
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
212
- plan: plan object (future use)
213
214
Returns:
215
ndarray: Real-valued FFT result
216
"""
217
218
def fft.ihfft(a, n=None, axis=-1, norm=None, plan=None):
219
"""
220
Compute inverse FFT of signal with Hermitian symmetry.
221
222
Parameters:
223
- a: array_like, real-valued input array
224
- n: int, length of transformed axis
225
- axis: int, axis over which to compute IFFT
226
- norm: {None, 'ortho', 'forward', 'backward'}, normalization mode
227
- plan: plan object (future use)
228
229
Returns:
230
ndarray: Complex-valued inverse FFT result
231
"""
232
```
233
234
### Helper Functions
235
236
Utility functions for working with FFT results.
237
238
```python { .api }
239
def fft.fftfreq(n, d=1.0):
240
"""
241
Return discrete Fourier Transform sample frequencies.
242
243
Parameters:
244
- n: int, window length
245
- d: scalar, sample spacing (inverse of sampling rate)
246
247
Returns:
248
ndarray: Sample frequencies
249
"""
250
251
def fft.rfftfreq(n, d=1.0):
252
"""
253
Return sample frequencies for rfft.
254
255
Parameters:
256
- n: int, window length
257
- d: scalar, sample spacing (inverse of sampling rate)
258
259
Returns:
260
ndarray: Sample frequencies for rfft
261
"""
262
263
def fft.fftshift(x, axes=None):
264
"""
265
Shift zero-frequency component to center of array.
266
267
Parameters:
268
- x: array_like, input array
269
- axes: int or shape tuple, axes over which to shift
270
271
Returns:
272
ndarray: Shifted array
273
"""
274
275
def fft.ifftshift(x, axes=None):
276
"""
277
Inverse of fftshift.
278
279
Parameters:
280
- x: array_like, input array
281
- axes: int or shape tuple, axes over which to shift
282
283
Returns:
284
ndarray: Shifted array
285
"""
286
```
287
288
## Usage Examples
289
290
### Basic 1D FFT
291
292
```python
293
import numpy as np
294
295
# Create a simple signal: sine wave + noise
296
fs = 500 # Sample rate
297
t = np.linspace(0, 1, fs)
298
signal = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 120 * t)
299
noise = 0.2 * np.random.normal(size=len(t))
300
noisy_signal = signal + noise
301
302
# Compute FFT
303
fft_result = np.fft.fft(noisy_signal)
304
frequencies = np.fft.fftfreq(len(noisy_signal), 1/fs)
305
306
# Get magnitude spectrum (first half due to symmetry)
307
magnitude = np.abs(fft_result[:len(fft_result)//2])
308
freq_positive = frequencies[:len(frequencies)//2]
309
```
310
311
### Real-valued FFT for Efficiency
312
313
```python
314
import numpy as np
315
316
# For real-valued signals, use rfft for efficiency
317
real_signal = np.cos(2 * np.pi * 10 * t) + np.sin(2 * np.pi * 20 * t)
318
319
# Real FFT (more efficient for real inputs)
320
rfft_result = np.fft.rfft(real_signal)
321
rfft_freqs = np.fft.rfftfreq(len(real_signal), 1/fs)
322
323
# Magnitude spectrum
324
magnitude_real = np.abs(rfft_result)
325
326
# Reconstruct original signal
327
reconstructed = np.fft.irfft(rfft_result)
328
```
329
330
### 2D FFT for Images
331
332
```python
333
import numpy as np
334
335
# Create a 2D signal (e.g., image with frequency content)
336
x = np.linspace(-5, 5, 100)
337
y = np.linspace(-5, 5, 100)
338
X, Y = np.meshgrid(x, y)
339
image = np.sin(2 * np.pi * X) * np.cos(2 * np.pi * Y)
340
341
# 2D FFT
342
fft2_result = np.fft.fft2(image)
343
fft2_shifted = np.fft.fftshift(fft2_result) # Center zero frequency
344
345
# Magnitude spectrum
346
magnitude_2d = np.abs(fft2_shifted)
347
phase_2d = np.angle(fft2_shifted)
348
349
# Inverse transform
350
reconstructed_2d = np.fft.ifft2(fft2_result)
351
```
352
353
### Filtering in Frequency Domain
354
355
```python
356
import numpy as np
357
358
# Low-pass filtering example
359
def lowpass_filter(signal, cutoff_freq, sample_rate):
360
# Compute FFT
361
fft_signal = np.fft.fft(signal)
362
frequencies = np.fft.fftfreq(len(signal), 1/sample_rate)
363
364
# Create filter (zero out high frequencies)
365
fft_filtered = fft_signal.copy()
366
fft_filtered[np.abs(frequencies) > cutoff_freq] = 0
367
368
# Inverse FFT to get filtered signal
369
filtered_signal = np.fft.ifft(fft_filtered).real
370
return filtered_signal
371
372
# Apply filter
373
fs = 1000
374
t = np.linspace(0, 1, fs)
375
noisy_signal = np.sin(2 * np.pi * 10 * t) + 0.5 * np.sin(2 * np.pi * 100 * t)
376
filtered = lowpass_filter(noisy_signal, cutoff_freq=50, sample_rate=fs)
377
```