0
# Fast Fourier Transform
1
2
GPU-accelerated discrete Fourier transforms for signal processing and frequency domain analysis. CuPy provides a complete FFT interface compatible with NumPy and SciPy, optimized for GPU execution using cuFFT.
3
4
## Capabilities
5
6
### Standard FFTs
7
8
One-dimensional and multi-dimensional complex-to-complex transforms.
9
10
```python { .api }
11
def fft(a, n=None, axis=-1, norm=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: {'backward', 'ortho', 'forward'}, normalization mode
20
21
Returns:
22
cupy.ndarray: Complex FFT result
23
"""
24
25
def ifft(a, n=None, axis=-1, norm=None):
26
"""Compute 1-D inverse discrete Fourier Transform."""
27
28
def fft2(a, s=None, axes=(-2, -1), norm=None):
29
"""
30
Compute 2-D discrete Fourier Transform.
31
32
Parameters:
33
- a: array-like, input array
34
- s: sequence of ints, shape of output
35
- axes: sequence of ints, axes over which to compute FFT
36
- norm: {'backward', 'ortho', 'forward'}, normalization mode
37
38
Returns:
39
cupy.ndarray: 2-D FFT result
40
"""
41
42
def ifft2(a, s=None, axes=(-2, -1), norm=None):
43
"""Compute 2-D inverse discrete Fourier Transform."""
44
45
def fftn(a, s=None, axes=None, norm=None):
46
"""
47
Compute N-D discrete Fourier Transform.
48
49
Parameters:
50
- a: array-like, input array
51
- s: sequence of ints, shape of output
52
- axes: sequence of ints, axes over which to compute FFT
53
- norm: {'backward', 'ortho', 'forward'}, normalization mode
54
55
Returns:
56
cupy.ndarray: N-D FFT result
57
"""
58
59
def ifftn(a, s=None, axes=None, norm=None):
60
"""Compute N-D inverse discrete Fourier Transform."""
61
```
62
63
### Real FFTs
64
65
Optimized transforms for real-valued input data.
66
67
```python { .api }
68
def rfft(a, n=None, axis=-1, norm=None):
69
"""
70
Compute 1-D real-input discrete Fourier Transform.
71
72
Parameters:
73
- a: array-like, real input array
74
- n: int, length of transformed axis
75
- axis: int, axis over which to compute FFT
76
- norm: {'backward', 'ortho', 'forward'}, normalization mode
77
78
Returns:
79
cupy.ndarray: Complex FFT result (positive frequencies only)
80
"""
81
82
def irfft(a, n=None, axis=-1, norm=None):
83
"""
84
Compute 1-D inverse real-input discrete Fourier Transform.
85
86
Parameters:
87
- a: array-like, complex input array
88
- n: int, length of output along transformed axis
89
- axis: int, axis over which to compute IFFT
90
- norm: {'backward', 'ortho', 'forward'}, normalization mode
91
92
Returns:
93
cupy.ndarray: Real IFFT result
94
"""
95
96
def rfft2(a, s=None, axes=(-2, -1), norm=None):
97
"""Compute 2-D real-input discrete Fourier Transform."""
98
99
def irfft2(a, s=None, axes=(-2, -1), norm=None):
100
"""Compute 2-D inverse real-input discrete Fourier Transform."""
101
102
def rfftn(a, s=None, axes=None, norm=None):
103
"""Compute N-D real-input discrete Fourier Transform."""
104
105
def irfftn(a, s=None, axes=None, norm=None):
106
"""Compute N-D inverse real-input discrete Fourier Transform."""
107
```
108
109
### Hermitian FFTs
110
111
Transforms for Hermitian symmetric input.
112
113
```python { .api }
114
def hfft(a, n=None, axis=-1, norm=None):
115
"""
116
Compute 1-D Hermitian-input discrete Fourier Transform.
117
118
Parameters:
119
- a: array-like, Hermitian input array
120
- n: int, length of output
121
- axis: int, axis over which to compute FFT
122
- norm: {'backward', 'ortho', 'forward'}, normalization mode
123
124
Returns:
125
cupy.ndarray: Real FFT result
126
"""
127
128
def ihfft(a, n=None, axis=-1, norm=None):
129
"""Compute 1-D inverse Hermitian-input discrete Fourier Transform."""
130
```
131
132
### Helper Functions
133
134
Utilities for frequency analysis and array manipulation.
135
136
```python { .api }
137
def fftfreq(n, d=1.0):
138
"""
139
Return discrete Fourier Transform sample frequencies.
140
141
Parameters:
142
- n: int, window length
143
- d: scalar, sample spacing
144
145
Returns:
146
cupy.ndarray: Sample frequencies
147
"""
148
149
def rfftfreq(n, d=1.0):
150
"""
151
Return sample frequencies for real-input FFT.
152
153
Parameters:
154
- n: int, window length
155
- d: scalar, sample spacing
156
157
Returns:
158
cupy.ndarray: Sample frequencies (positive only)
159
"""
160
161
def fftshift(x, axes=None):
162
"""
163
Shift zero-frequency component to center of spectrum.
164
165
Parameters:
166
- x: array-like, input array
167
- axes: int or shape tuple, axes over which to shift
168
169
Returns:
170
cupy.ndarray: Shifted array
171
"""
172
173
def ifftshift(x, axes=None):
174
"""
175
Inverse of fftshift.
176
177
Parameters:
178
- x: array-like, input array
179
- axes: int or shape tuple, axes over which to shift
180
181
Returns:
182
cupy.ndarray: Shifted array
183
"""
184
```
185
186
## Usage Examples
187
188
### Basic FFT Operations
189
190
```python
191
import cupy as cp
192
193
# 1D signal processing
194
N = 1024
195
t = cp.linspace(0, 1, N, endpoint=False)
196
signal = cp.sin(2 * cp.pi * 50 * t) + cp.sin(2 * cp.pi * 120 * t)
197
198
# Forward FFT
199
fft_result = cp.fft.fft(signal)
200
frequencies = cp.fft.fftfreq(N, d=1/N)
201
202
# Find dominant frequencies
203
magnitude = cp.abs(fft_result)
204
dominant_freq_idx = cp.argmax(magnitude[:N//2])
205
dominant_freq = frequencies[dominant_freq_idx]
206
207
# Inverse FFT
208
reconstructed = cp.fft.ifft(fft_result)
209
assert cp.allclose(signal, reconstructed.real)
210
```
211
212
### 2D Image Processing
213
214
```python
215
# 2D FFT for image processing
216
image = cp.random.random((512, 512))
217
218
# 2D FFT
219
fft_image = cp.fft.fft2(image)
220
fft_shifted = cp.fft.fftshift(fft_image)
221
222
# Apply frequency domain filter (low-pass)
223
rows, cols = image.shape
224
crow, ccol = rows // 2, cols // 2
225
mask = cp.zeros((rows, cols), dtype=bool)
226
r = 50 # Filter radius
227
y, x = cp.ogrid[:rows, :cols]
228
mask_area = (x - ccol)**2 + (y - crow)**2 <= r*r
229
mask[mask_area] = True
230
231
# Apply mask and inverse transform
232
fft_filtered = fft_shifted * mask
233
filtered_image = cp.fft.ifft2(cp.fft.ifftshift(fft_filtered))
234
filtered_image = cp.real(filtered_image)
235
```
236
237
### Real-valued Signal Processing
238
239
```python
240
# Efficient FFT for real signals
241
real_signal = cp.random.random(1000)
242
243
# Real FFT (more efficient for real input)
244
rfft_result = cp.fft.rfft(real_signal)
245
# Result has shape (501,) instead of (1000,)
246
247
# Frequency analysis
248
freqs = cp.fft.rfftfreq(len(real_signal))
249
power_spectrum = cp.abs(rfft_result)**2
250
251
# Inverse transform
252
reconstructed = cp.fft.irfft(rfft_result)
253
assert cp.allclose(real_signal, reconstructed)
254
```