four1 â 1D and Multi-Dimensional Fast Fourier Transform (FFT) Routines
Overview & Purpose
The four1 module (code/subr/four1.c and code/subr/four1.h) provides in-place radix-2 Cooley-Tukey Fast Fourier Transform (FFT) routines adapted from Numerical Recipes in C (Press et al.). Originally integrated into the Ocean Mapping Group (subr/) codebase in the early 1990s for 1D spectral analysis (four1) and expanded in 2012 with the $N$-dimensional transform (fourn), it underpins frequency-domain matched filtering (pulse compression), power spectral density estimation, cross-spectral coherence analysis, and acoustic calibration across the OMG suite.
Key Conventions & Macros (four1.c)
1-Based Indexing Convention
Both four1 and fourn use the classic Numerical Recipes 1-based indexing convention for complex interleaved float arrays:
- A complex series of $N$ points is stored in a
floatarray of length $2N$ indexed fromdata[1]todata[2*N], where odd indices (data[1], data[3], ...) hold real parts and even indices (data[2], data[4], ...) hold imaginary parts. - Standard 0-based C callers pass a decremented pointer (
four1(complex_buf - 1, nn, isign)).
Helper Macro
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr: Used during the in-place bit-reversal permutation stage to swap real and imaginary sample pairs.
Public Functions & Signatures
1. One-Dimensional Radix-2 FFT
void four1(float data[], int nn, int isign);
- Purpose: Replaces the 1-indexed interleaved complex array
data[1..2*nn]by its discrete Fourier transform (whenisign == 1) or bynntimes its inverse discrete Fourier transform (whenisign == -1). - Parameters:
data: 1-based array of2 * nnsingle-precision floats containingnninterleaved complex samples $(\text{Re}_k, \text{Im}_k)$.nn: Number of complex samples; must be an integer power of 2 ($2^m$).isign: Transform direction (+1for forward FFT,-1for unnormalized inverse IFFT).
- Algorithm:
- Bit-Reversal Permutation: Iterates through indices $i = 1, 3, \dots, 2\text{nn}-1$ and maintains the bit-reversed index $j$, swapping complex pairs whenever $j > i$.
- Danielson-Lanczos Butterfly Stages: Repeatedly combines sub-transforms of length
mmax = 2, 4, 8, ..., nnusing double-precision trigonometric recurrences (wtemp = sin(0.5 * theta),wpr = -2.0 * wtemp * wtemp,wpi = sin(theta)) to prevent cumulative floating-point phase drift across large transforms. - Inverse Scaling Note: When
isign == -1, the output is unnormalized and must be divided bynnby the caller to recover exact amplitudes.
2. Multi-Dimensional Radix-2 FFT
void fourn(float data[], unsigned long nn[], int ndim, int isign);
- Purpose: Computes the in-place $N$-dimensional discrete Fourier transform (
isign == 1) or unnormalized inverse transform (isign == -1) of a multi-dimensional complex array. - Parameters:
data: 1-based interleaved complexfloatarray of total length $2 \times \prod_{idim=1}^{ndim} \text{nn}[idim]$.nn: 1-based arraynn[1..ndim]specifying the complex dimension lengths along each axis (each dimension must be a power of 2).ndim: Number of dimensions (e.g.,2for 2D wavenumber/wavenumber-frequency transforms).isign: Transform direction (+1forward,-1unnormalized inverse).
- Algorithm: Loops over each dimension
idim = ndim ... 1, performing multi-dimensional stride-aware bit-reversal followed by Danielson-Lanczos butterfly stages along the stridenprevof the current axis.
How It Fits Into the OMG Architecture
four1 is the foundational FFT engine throughout the OMG signal-processing stack:
- FM Chirp Pulse Compression (
ek_IQ):matched_filter_one_trace()incode/subr/ek_IQ.ccallsfour1(..., 1)on zero-padded complex basebanded transceiver traces and replica chirp pulses, multiplies by the complex conjugate in the frequency domain, and callsfour1(..., -1)to recover compressed range profiles. - 1D Spectral & Coherence Estimation (
dspectra):code/subr/dspectra.ccallsfour1()insidespectrum(),cross_spectra(), andcoherence()to compute Welch-style windowed periodograms and transfer functions. - Acoustic Calibration & Split-Beam Diagnostics (
Echo_calib,EKlook,jview,DelayEditor): Used inEK_FFT.c,ek_fft_funcs.c, andJV_play_WAV.cfor passband spectral inspection, Doppler shift analysis, and acoustic target frequency response characterization.