Fast Fourier transforms
The discrete Fourier transform (DFT) of samples is
and its inverse is . It decomposes a signal into the frequencies it contains, and turns convolution into multiplication. Computed directly it costs operations; the fast Fourier transform (FFT) computes exactly the same numbers in , which made digital signal processing, spectral analysis, image compression, fast polynomial and integer multiplication, and spectral methods for differential equations practical. For a million points the saving is a factor of fifty thousand.
The central questions
- What length? The classical FFTs factor ; powers of two are the fastest and simplest. Any length, including primes, can be handled in by turning the DFT into a convolution (Bluestein’s algorithm).
- Real or complex data? Real signals have conjugate-symmetric spectra, and a transform that exploits this does less work (real FFTs).
- What symmetry or boundary conditions? Cosine and sine transforms correspond to even and odd extensions of the data, which avoid the artificial jump of the periodic extension (DCT and DST).
- One dimension or more? Multidimensional transforms are one-dimensional transforms along each axis in turn.
Accuracy
The FFT is not just faster than the direct sum but more accurate: its error grows like rather than , as long as the twiddle factors are themselves accurate. Every engine in the library is compared below with an exactly known spectrum (a sum of complex exponentials at integer frequencies):
Figure 8.1. FFT accuracy.
The textbook engine whose twiddles are generated by a recurrence shows the expected growth, and is kept as a lesson.
Testing against SciPy
The references come from SciPy 1.17.1’s scipy.fft (the pocketfft library), written by book/generator/data/make_fft_refs.py; the inputs are formulas that the generator repeats. Each entry is the largest difference relative to the largest magnitude of SciPy’s result:
Table 8.1. FFTs against SciPy.
| transform (SciPy reference) | largest relative difference |
|---|---|
complex DFT, 8 points (power of two) (fft.fft) | 2e-17 |
complex DFT, 64 points (power of two) (fft.fft) | 2e-16 |
complex DFT, 512 points (power of two) (fft.fft) | 2e-16 |
complex DFT, 1000 points (Bluestein) (fft.fft) | 2e-16 |
complex DFT, 997 points (prime, Bluestein) (fft.fft) | 4e-16 |
inverse DFT, 64 points, scaled by 1/n (fft.ifft) | 2e-16 |
inverse DFT, 997 points, scaled by 1/n (fft.ifft) | 7e-16 |
real DFT, 16 points (fft.rfft) | 2e-16 |
real DFT, 17 points (fft.rfft) | 2e-16 |
2D DFT, 8 x 12 (fft.fft2) | 3e-16 |
3D DFT, 4 x 6 x 5 (fft.fftn) | 4e-16 |
fftshift, 9 points (fft.fftshift) | 0 |
Table 8.2. DCT and DST against SciPy, in both normalisations.
DCT, DST and inverse DCT, 32 points (fft.dct, fft.dst, fft.idct) | unnormalised (backward) | orthonormal (ortho) |
|---|---|---|
| type I | 2e-16 | 2e-16 |
| type II | 2e-16 | 2e-16 |
| type III | 2e-16 | 2e-16 |
| type IV | 2e-16 | 2e-16 |
All of them agree to rounding error, including the Bluestein transforms of prime length and every type and normalisation of the trigonometric transforms.
Two crates
lib_jsl_ffts (API reference) is the production crate. Its Fft handles any length,
choosing BestFft (radix-4 passes, AVX2/FMA kernels, cache blocking) for powers of two and BluesteinFft otherwise. It also provides
SimdFft (vectorised radix-2 kernels for x86, ARM and RISC-V), FractionalFft, RealFft, multidimensional transforms and the DCT and
DST of types I to IV. All engines implement the FftEngine1D trait: plan once for a length, scaling, direction and input ordering,
then execute repeatedly.
lib_jsl_ffts_reference (API reference) holds three readable power-of-two engines for
study (the textbook radix-2 algorithm, radix 2 with exact twiddles, and split radix) that implement the same trait;
cargo bench -p lib_jsl_ffts_reference races them against the production engines.
Choosing a transform
| You need | Use | Entry |
|---|---|---|
| The DFT of complex data, any length | Fft | The fast Fourier transform |
| The DFT of a power-of-two length, a specific engine | BestFft, SimdFft | The fast Fourier transform |
| A prime or awkward length, or a chirp-z transform | BluesteinFft (inside Fft) | Bluestein and fractional |
| A rotation in the time-frequency plane | FractionalFft, frft | Bluestein and fractional |
| The spectrum of real data | RealFft, rfft, irfft | Real and multidimensional |
| Images and volumes | fft2, fft3, fftn, fftshift | Real and multidimensional |
| Compression, or smooth non-periodic data | dct, dst and inverses | DCT and DST |