0
# numpy.fft Interface
1
2
Drop-in replacement for numpy.fft functions with additional optimization parameters, enabling existing code to benefit from FFTW performance with minimal changes. This interface maintains full API compatibility while adding FFTW-specific performance options.
3
4
## Capabilities
5
6
### Complex FFT Functions
7
8
Core complex-to-complex transform functions compatible with numpy.fft.
9
10
```python { .api }
11
def fft(
12
a,
13
n=None,
14
axis=-1,
15
norm=None,
16
overwrite_input=False,
17
planner_effort='FFTW_ESTIMATE',
18
threads=1,
19
auto_align_input=True,
20
auto_contiguous=True
21
):
22
"""
23
1D discrete Fourier transform.
24
25
Parameters:
26
- a: Input array
27
- n: Length of transform (default: a.shape[axis])
28
- axis: Axis over which to compute FFT (default: -1)
29
- norm: Normalization mode ('backward', 'ortho', 'forward', None)
30
- overwrite_input: Whether input can be overwritten (default: False)
31
- planner_effort: FFTW planning effort (default: 'FFTW_ESTIMATE')
32
- threads: Number of threads (default: 1)
33
- auto_align_input: Auto-align input for SIMD (default: True)
34
- auto_contiguous: Make input contiguous (default: True)
35
36
Returns:
37
- Complex array containing the transform
38
"""
39
40
def ifft(
41
a,
42
n=None,
43
axis=-1,
44
norm=None,
45
overwrite_input=False,
46
planner_effort='FFTW_ESTIMATE',
47
threads=1,
48
auto_align_input=True,
49
auto_contiguous=True
50
):
51
"""
52
1D inverse discrete Fourier transform.
53
54
Parameters: Same as fft()
55
56
Returns:
57
- Complex array containing the inverse transform
58
"""
59
60
def fft2(
61
a,
62
s=None,
63
axes=(-2, -1),
64
norm=None,
65
overwrite_input=False,
66
planner_effort='FFTW_ESTIMATE',
67
threads=1,
68
auto_align_input=True,
69
auto_contiguous=True
70
):
71
"""
72
2D discrete Fourier transform.
73
74
Parameters:
75
- a: Input array
76
- s: Shape of transform (default: a.shape[axes])
77
- axes: Axes over which to compute FFT (default: (-2, -1))
78
- Other parameters: Same as fft()
79
80
Returns:
81
- Complex array containing the 2D transform
82
"""
83
84
def ifft2(
85
a,
86
s=None,
87
axes=(-2, -1),
88
norm=None,
89
overwrite_input=False,
90
planner_effort='FFTW_ESTIMATE',
91
threads=1,
92
auto_align_input=True,
93
auto_contiguous=True
94
):
95
"""
96
2D inverse discrete Fourier transform.
97
98
Parameters: Same as fft2()
99
100
Returns:
101
- Complex array containing the 2D inverse transform
102
"""
103
104
def fftn(
105
a,
106
s=None,
107
axes=None,
108
norm=None,
109
overwrite_input=False,
110
planner_effort='FFTW_ESTIMATE',
111
threads=1,
112
auto_align_input=True,
113
auto_contiguous=True
114
):
115
"""
116
N-dimensional discrete Fourier transform.
117
118
Parameters:
119
- a: Input array
120
- s: Shape of transform (default: a.shape[axes])
121
- axes: Axes over which to compute FFT (default: all axes)
122
- Other parameters: Same as fft()
123
124
Returns:
125
- Complex array containing the N-D transform
126
"""
127
128
def ifftn(
129
a,
130
s=None,
131
axes=None,
132
norm=None,
133
overwrite_input=False,
134
planner_effort='FFTW_ESTIMATE',
135
threads=1,
136
auto_align_input=True,
137
auto_contiguous=True
138
):
139
"""
140
N-dimensional inverse discrete Fourier transform.
141
142
Parameters: Same as fftn()
143
144
Returns:
145
- Complex array containing the N-D inverse transform
146
"""
147
```
148
149
### Real FFT Functions
150
151
Real-to-complex and complex-to-real transform functions.
152
153
```python { .api }
154
def rfft(
155
a,
156
n=None,
157
axis=-1,
158
norm=None,
159
overwrite_input=False,
160
planner_effort='FFTW_ESTIMATE',
161
threads=1,
162
auto_align_input=True,
163
auto_contiguous=True
164
):
165
"""
166
1D real-to-complex discrete Fourier transform.
167
168
Parameters: Same as fft()
169
170
Returns:
171
- Complex array with shape [..., n//2 + 1] along transform axis
172
"""
173
174
def irfft(
175
a,
176
n=None,
177
axis=-1,
178
norm=None,
179
overwrite_input=False,
180
planner_effort='FFTW_ESTIMATE',
181
threads=1,
182
auto_align_input=True,
183
auto_contiguous=True
184
):
185
"""
186
1D complex-to-real inverse discrete Fourier transform.
187
188
Parameters: Same as fft()
189
190
Returns:
191
- Real array with specified length n along transform axis
192
"""
193
194
def rfft2(
195
a,
196
s=None,
197
axes=(-2, -1),
198
norm=None,
199
overwrite_input=False,
200
planner_effort='FFTW_ESTIMATE',
201
threads=1,
202
auto_align_input=True,
203
auto_contiguous=True
204
):
205
"""
206
2D real-to-complex discrete Fourier transform.
207
208
Parameters: Same as fft2()
209
210
Returns:
211
- Complex array with last axis shape s[-1]//2 + 1
212
"""
213
214
def irfft2(
215
a,
216
s=None,
217
axes=(-2, -1),
218
norm=None,
219
overwrite_input=False,
220
planner_effort='FFTW_ESTIMATE',
221
threads=1,
222
auto_align_input=True,
223
auto_contiguous=True
224
):
225
"""
226
2D complex-to-real inverse discrete Fourier transform.
227
228
Parameters: Same as fft2()
229
230
Returns:
231
- Real array with specified shape s
232
"""
233
234
def rfftn(
235
a,
236
s=None,
237
axes=None,
238
norm=None,
239
overwrite_input=False,
240
planner_effort='FFTW_ESTIMATE',
241
threads=1,
242
auto_align_input=True,
243
auto_contiguous=True
244
):
245
"""
246
N-dimensional real-to-complex discrete Fourier transform.
247
248
Parameters: Same as fftn()
249
250
Returns:
251
- Complex array with last transform axis shape s[-1]//2 + 1
252
"""
253
254
def irfftn(
255
a,
256
s=None,
257
axes=None,
258
norm=None,
259
overwrite_input=False,
260
planner_effort='FFTW_ESTIMATE',
261
threads=1,
262
auto_align_input=True,
263
auto_contiguous=True
264
):
265
"""
266
N-dimensional complex-to-real inverse discrete Fourier transform.
267
268
Parameters: Same as fftn()
269
270
Returns:
271
- Real array with specified shape s
272
"""
273
```
274
275
### Hermitian FFT Functions
276
277
Functions for transforms of Hermitian (conjugate-symmetric) data.
278
279
```python { .api }
280
def hfft(
281
a,
282
n=None,
283
axis=-1,
284
norm=None,
285
overwrite_input=False,
286
planner_effort='FFTW_ESTIMATE',
287
threads=1,
288
auto_align_input=True,
289
auto_contiguous=True
290
):
291
"""
292
FFT of Hermitian (conjugate-symmetric) sequence.
293
294
Parameters: Same as fft()
295
296
Returns:
297
- Real array containing the transform
298
"""
299
300
def ihfft(
301
a,
302
n=None,
303
axis=-1,
304
norm=None,
305
overwrite_input=False,
306
planner_effort='FFTW_ESTIMATE',
307
threads=1,
308
auto_align_input=True,
309
auto_contiguous=True
310
):
311
"""
312
Inverse FFT of Hermitian sequence.
313
314
Parameters: Same as fft()
315
316
Returns:
317
- Complex array containing the inverse transform
318
"""
319
```
320
321
## Usage Examples
322
323
### Drop-in Replacement
324
325
```python
326
# Original numpy code
327
import numpy as np
328
329
data = np.random.randn(1024) + 1j * np.random.randn(1024)
330
result_numpy = np.fft.fft(data)
331
332
# Replace with pyFFTW - same interface, better performance
333
from pyfftw.interfaces import numpy_fft
334
335
result_pyfftw = numpy_fft.fft(data)
336
print(f"Results match: {np.allclose(result_numpy, result_pyfftw)}")
337
```
338
339
### Optimized Parameters
340
341
```python
342
from pyfftw.interfaces import numpy_fft
343
import numpy as np
344
345
# Use additional performance parameters
346
data = np.random.randn(1024, 512) + 1j * np.random.randn(1024, 512)
347
348
# Standard call with optimization
349
result = numpy_fft.fft2(
350
data,
351
overwrite_input=True, # Allow input modification
352
planner_effort='FFTW_MEASURE', # Better planning
353
threads=4, # Use multiple threads
354
auto_align_input=True # Ensure optimal alignment
355
)
356
```
357
358
### Real Data Processing
359
360
```python
361
from pyfftw.interfaces import numpy_fft
362
import numpy as np
363
364
# Process real signal
365
signal = np.random.randn(2048)
366
367
# Real-to-complex transform
368
spectrum = numpy_fft.rfft(signal, planner_effort='FFTW_PATIENT')
369
print(f"Signal shape: {signal.shape}")
370
print(f"Spectrum shape: {spectrum.shape}") # (1025,) - n//2 + 1
371
372
# Modify spectrum (e.g., filtering)
373
spectrum[100:] = 0 # Low-pass filter
374
375
# Transform back to real
376
filtered_signal = numpy_fft.irfft(spectrum, n=len(signal))
377
print(f"Filtered signal shape: {filtered_signal.shape}")
378
```
379
380
### 2D Image Processing
381
382
```python
383
from pyfftw.interfaces import numpy_fft
384
import numpy as np
385
386
# Process 2D image data
387
image = np.random.randn(512, 512)
388
389
# 2D real FFT for image processing
390
freq_domain = numpy_fft.rfft2(
391
image,
392
threads=2,
393
auto_align_input=True
394
)
395
396
# Apply frequency domain filter
397
center_y, center_x = np.array(freq_domain.shape) // 2
398
y, x = np.ogrid[:freq_domain.shape[0], :freq_domain.shape[1]]
399
mask = (y - center_y)**2 + (x - center_x)**2 > 50**2
400
freq_domain[mask] = 0
401
402
# Transform back
403
filtered_image = numpy_fft.irfft2(freq_domain, s=image.shape)
404
```
405
406
### Normalization Modes
407
408
```python
409
from pyfftw.interfaces import numpy_fft
410
import numpy as np
411
412
data = np.random.randn(128) + 1j * np.random.randn(128)
413
414
# Different normalization modes
415
forward = numpy_fft.fft(data, norm='forward') # 1/n scaling on forward
416
backward = numpy_fft.fft(data, norm='backward') # 1/n scaling on inverse (default)
417
ortho = numpy_fft.fft(data, norm='ortho') # 1/sqrt(n) scaling on both
418
419
# Verify orthogonal normalization preserves energy
420
print(f"Original energy: {np.sum(np.abs(data)**2)}")
421
print(f"Ortho energy: {np.sum(np.abs(ortho)**2)}")
422
```
423
424
### N-D Processing
425
426
```python
427
from pyfftw.interfaces import numpy_fft
428
import numpy as np
429
430
# 3D volume data
431
volume = np.random.randn(64, 64, 64) + 1j * np.random.randn(64, 64, 64)
432
433
# Transform only specific axes
434
result_2d = numpy_fft.fftn(volume, axes=(0, 2)) # Transform first and last axes
435
result_all = numpy_fft.fftn(volume) # Transform all axes
436
437
print(f"2D transform shape: {result_2d.shape}")
438
print(f"3D transform shape: {result_all.shape}")
439
```