or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-creation.mdarray-manipulation.mdbinary-operations.mdcuda.mdfft.mdindex.mdindexing-searching.mdlinalg.mdlogic-functions.mdmath-functions.mdmemory-performance.mdrandom.mdsorting-counting.mdstatistics.md

fft.mddocs/

0

# Fast Fourier Transform

1

2

GPU-accelerated FFT operations for 1D, 2D, and N-dimensional transforms. CuPy's FFT module provides comprehensive frequency domain processing capabilities powered by cuFFT library with NumPy-compatible interfaces.

3

4

## Capabilities

5

6

### One-Dimensional FFT

7

8

Standard 1D Fourier transforms for signal processing and frequency analysis.

9

10

```python { .api }

11

def fft(a, n=None, axis=-1, norm=None):

12

"""

13

One-dimensional discrete Fourier transform.

14

15

Parameters:

16

- a: array-like, input array

17

- n: int, length of transform, optional

18

- axis: int, axis for FFT, default -1

19

- norm: str, normalization mode, optional

20

21

Returns:

22

cupy.ndarray: Complex FFT result on GPU

23

"""

24

25

def ifft(a, n=None, axis=-1, norm=None):

26

"""

27

One-dimensional inverse discrete Fourier transform.

28

29

Parameters:

30

- a: array-like, input array

31

- n: int, length of transform, optional

32

- axis: int, axis for IFFT, default -1

33

- norm: str, normalization mode, optional

34

35

Returns:

36

cupy.ndarray: Complex IFFT result on GPU

37

"""

38

39

def rfft(a, n=None, axis=-1, norm=None):

40

"""

41

One-dimensional real discrete Fourier transform.

42

43

Parameters:

44

- a: array-like, real input array

45

- n: int, length of transform, optional

46

- axis: int, axis for FFT, default -1

47

- norm: str, normalization mode, optional

48

49

Returns:

50

cupy.ndarray: Complex FFT result for real input on GPU

51

"""

52

53

def irfft(a, n=None, axis=-1, norm=None):

54

"""

55

One-dimensional inverse real discrete Fourier transform.

56

57

Parameters:

58

- a: array-like, complex input array

59

- n: int, length of output, optional

60

- axis: int, axis for IFFT, default -1

61

- norm: str, normalization mode, optional

62

63

Returns:

64

cupy.ndarray: Real IFFT result on GPU

65

"""

66

67

def hfft(a, n=None, axis=-1, norm=None):

68

"""

69

Hermitian discrete Fourier transform.

70

71

Parameters:

72

- a: array-like, Hermitian input array

73

- n: int, length of transform, optional

74

- axis: int, axis for FFT, default -1

75

- norm: str, normalization mode, optional

76

77

Returns:

78

cupy.ndarray: Real FFT result for Hermitian input on GPU

79

"""

80

81

def ihfft(a, n=None, axis=-1, norm=None):

82

"""

83

Inverse Hermitian discrete Fourier transform.

84

85

Parameters:

86

- a: array-like, real input array

87

- n: int, length of output, optional

88

- axis: int, axis for IFFT, default -1

89

- norm: str, normalization mode, optional

90

91

Returns:

92

cupy.ndarray: Complex Hermitian IFFT result on GPU

93

"""

94

```

95

96

### Two-Dimensional FFT

97

98

2D Fourier transforms for image processing and 2D signal analysis.

99

100

```python { .api }

101

def fft2(a, s=None, axes=(-2, -1), norm=None):

102

"""

103

Two-dimensional discrete Fourier transform.

104

105

Parameters:

106

- a: array-like, input array

107

- s: tuple of ints, shape of transform, optional

108

- axes: tuple of ints, axes for 2D FFT, default (-2, -1)

109

- norm: str, normalization mode, optional

110

111

Returns:

112

cupy.ndarray: Complex 2D FFT result on GPU

113

"""

114

115

def ifft2(a, s=None, axes=(-2, -1), norm=None):

116

"""

117

Two-dimensional inverse discrete Fourier transform.

118

119

Parameters:

120

- a: array-like, input array

121

- s: tuple of ints, shape of transform, optional

122

- axes: tuple of ints, axes for 2D IFFT, default (-2, -1)

123

- norm: str, normalization mode, optional

124

125

Returns:

126

cupy.ndarray: Complex 2D IFFT result on GPU

127

"""

128

129

def rfft2(a, s=None, axes=(-2, -1), norm=None):

130

"""

131

Two-dimensional real discrete Fourier transform.

132

133

Parameters:

134

- a: array-like, real input array

135

- s: tuple of ints, shape of transform, optional

136

- axes: tuple of ints, axes for 2D FFT, default (-2, -1)

137

- norm: str, normalization mode, optional

138

139

Returns:

140

cupy.ndarray: Complex 2D FFT result for real input on GPU

141

"""

142

143

def irfft2(a, s=None, axes=(-2, -1), norm=None):

144

"""

145

Two-dimensional inverse real discrete Fourier transform.

146

147

Parameters:

148

- a: array-like, complex input array

149

- s: tuple of ints, shape of output, optional

150

- axes: tuple of ints, axes for 2D IFFT, default (-2, -1)

151

- norm: str, normalization mode, optional

152

153

Returns:

154

cupy.ndarray: Real 2D IFFT result on GPU

155

"""

156

```

157

158

### N-Dimensional FFT

159

160

Multi-dimensional Fourier transforms for complex data analysis.

161

162

```python { .api }

163

def fftn(a, s=None, axes=None, norm=None):

164

"""

165

N-dimensional discrete Fourier transform.

166

167

Parameters:

168

- a: array-like, input array

169

- s: tuple of ints, shape of transform, optional

170

- axes: tuple of ints, axes for N-D FFT, optional

171

- norm: str, normalization mode, optional

172

173

Returns:

174

cupy.ndarray: Complex N-D FFT result on GPU

175

"""

176

177

def ifftn(a, s=None, axes=None, norm=None):

178

"""

179

N-dimensional inverse discrete Fourier transform.

180

181

Parameters:

182

- a: array-like, input array

183

- s: tuple of ints, shape of transform, optional

184

- axes: tuple of ints, axes for N-D IFFT, optional

185

- norm: str, normalization mode, optional

186

187

Returns:

188

cupy.ndarray: Complex N-D IFFT result on GPU

189

"""

190

191

def rfftn(a, s=None, axes=None, norm=None):

192

"""

193

N-dimensional real discrete Fourier transform.

194

195

Parameters:

196

- a: array-like, real input array

197

- s: tuple of ints, shape of transform, optional

198

- axes: tuple of ints, axes for N-D FFT, optional

199

- norm: str, normalization mode, optional

200

201

Returns:

202

cupy.ndarray: Complex N-D FFT result for real input on GPU

203

"""

204

205

def irfftn(a, s=None, axes=None, norm=None):

206

"""

207

N-dimensional inverse real discrete Fourier transform.

208

209

Parameters:

210

- a: array-like, complex input array

211

- s: tuple of ints, shape of output, optional

212

- axes: tuple of ints, axes for N-D IFFT, optional

213

- norm: str, normalization mode, optional

214

215

Returns:

216

cupy.ndarray: Real N-D IFFT result on GPU

217

"""

218

```

219

220

### Frequency Sampling

221

222

Functions for generating frequency arrays and shifting frequency components.

223

224

```python { .api }

225

def fftfreq(n, d=1.0):

226

"""

227

Discrete Fourier transform sample frequencies.

228

229

Parameters:

230

- n: int, window length

231

- d: float, sample spacing, default 1.0

232

233

Returns:

234

cupy.ndarray: Frequency array on GPU

235

"""

236

237

def rfftfreq(n, d=1.0):

238

"""

239

Real discrete Fourier transform sample frequencies.

240

241

Parameters:

242

- n: int, window length

243

- d: float, sample spacing, default 1.0

244

245

Returns:

246

cupy.ndarray: Frequency array for real FFT on GPU

247

"""

248

249

def fftshift(x, axes=None):

250

"""

251

Shift zero-frequency component to center.

252

253

Parameters:

254

- x: array-like, input array

255

- axes: int or tuple, axes to shift, optional

256

257

Returns:

258

cupy.ndarray: Shifted array on GPU

259

"""

260

261

def ifftshift(x, axes=None):

262

"""

263

Inverse of fftshift.

264

265

Parameters:

266

- x: array-like, input array

267

- axes: int or tuple, axes to shift, optional

268

269

Returns:

270

cupy.ndarray: Inverse shifted array on GPU

271

"""

272

```

273

274

## Usage Examples

275

276

### Basic 1D FFT Operations

277

278

```python

279

import cupy as cp

280

import numpy as np

281

282

# Create sample signal

283

t = cp.linspace(0, 1, 1000, endpoint=False)

284

signal = cp.sin(2 * cp.pi * 50 * t) + cp.sin(2 * cp.pi * 120 * t)

285

signal += 0.5 * cp.random.random(1000) # Add noise

286

287

# Forward FFT

288

fft_result = cp.fft.fft(signal)

289

frequencies = cp.fft.fftfreq(len(signal), 1/1000) # Sample rate 1000 Hz

290

291

# Magnitude spectrum

292

magnitude = cp.abs(fft_result)

293

power_spectrum = magnitude**2

294

295

# Inverse FFT to reconstruct signal

296

reconstructed = cp.fft.ifft(fft_result)

297

print(cp.allclose(signal, reconstructed.real)) # Should be True

298

```

299

300

### 2D FFT for Image Processing

301

302

```python

303

# Create 2D image-like data

304

image = cp.random.random((256, 256)).astype(cp.float32)

305

306

# Add some structure (sine wave pattern)

307

x, y = cp.meshgrid(cp.linspace(0, 10, 256), cp.linspace(0, 10, 256))

308

pattern = cp.sin(2 * cp.pi * x) + cp.cos(2 * cp.pi * y)

309

image += pattern

310

311

# 2D FFT

312

fft_image = cp.fft.fft2(image)

313

fft_shifted = cp.fft.fftshift(fft_image) # Center DC component

314

315

# Magnitude spectrum

316

magnitude_spectrum = cp.log(cp.abs(fft_shifted) + 1)

317

318

# Apply frequency domain filter (low-pass)

319

center = (128, 128)

320

radius = 50

321

mask = cp.zeros_like(fft_shifted)

322

y_grid, x_grid = cp.ogrid[:256, :256]

323

distance = cp.sqrt((x_grid - center[0])**2 + (y_grid - center[1])**2)

324

mask[distance <= radius] = 1

325

326

# Apply filter and inverse transform

327

filtered_fft = fft_shifted * mask

328

filtered_fft = cp.fft.ifftshift(filtered_fft)

329

filtered_image = cp.fft.ifft2(filtered_fft).real

330

```

331

332

### Real FFT for Efficient Processing

333

334

```python

335

# Real-valued signal processing

336

real_signal = cp.cos(2 * cp.pi * cp.linspace(0, 10, 1024))

337

338

# Real FFT (more efficient for real inputs)

339

rfft_result = cp.fft.rfft(real_signal)

340

rfreqs = cp.fft.rfftfreq(len(real_signal))

341

342

# Modify spectrum (e.g., remove high frequencies)

343

cutoff = len(rfft_result) // 4

344

rfft_result[cutoff:] = 0

345

346

# Inverse real FFT

347

filtered_signal = cp.fft.irfft(rfft_result)

348

349

# Compare original and filtered

350

print(f"Original signal length: {len(real_signal)}")

351

print(f"Filtered signal length: {len(filtered_signal)}")

352

```

353

354

### N-Dimensional FFT

355

356

```python

357

# 3D data (e.g., volumetric data)

358

volume = cp.random.random((64, 64, 64)).astype(cp.complex64)

359

360

# 3D FFT on all axes

361

fft_3d = cp.fft.fftn(volume)

362

363

# FFT on specific axes only

364

fft_2d_slices = cp.fft.fftn(volume, axes=(0, 1)) # FFT on first two dimensions

365

366

# Frequency analysis

367

freq_x = cp.fft.fftfreq(64)

368

freq_y = cp.fft.fftfreq(64)

369

freq_z = cp.fft.fftfreq(64)

370

371

# Power spectrum

372

power_3d = cp.abs(fft_3d)**2

373

total_power = cp.sum(power_3d)

374

```

375

376

### Advanced FFT Applications

377

378

```python

379

# Convolution using FFT (often faster for large arrays)

380

def fft_convolve(a, b):

381

# Pad arrays to avoid circular convolution artifacts

382

size = len(a) + len(b) - 1

383

fft_a = cp.fft.fft(a, n=size)

384

fft_b = cp.fft.fft(b, n=size)

385

return cp.fft.ifft(fft_a * fft_b).real[:size]

386

387

# Example signals

388

signal1 = cp.random.random(1000)

389

kernel = cp.array([0.25, 0.5, 0.25]) # Simple smoothing kernel

390

391

convolved = fft_convolve(signal1, kernel)

392

393

# Cross-correlation using FFT

394

def fft_correlate(a, b):

395

fft_a = cp.fft.fft(a, n=len(a) + len(b) - 1)

396

fft_b = cp.fft.fft(b[::-1], n=len(a) + len(b) - 1) # Reverse b

397

return cp.fft.ifft(fft_a * cp.conj(fft_b)).real

398

399

correlation = fft_correlate(signal1, kernel)

400

```