Signal processing · FFT sizing
nextpow2 and FFT Lengths
The nextpow2 function answers one question: what is the smallest integer p such that 2p is greater than or equal to the absolute value of a given number? The answer is the exponent, not the power itself, so nextpow2(1000) is 10, and 2¹⁰ = 1024 is the padded length. It exists mainly to choose a length for the fast Fourier transform, whose classic radix-2 form runs fastest when the length is a power of two. The argument can also be a fraction: the value of e⁻¹ is about 0.3679, and nextpow2 of it is −1, since 2⁻¹ = 0.5 is the first power of two above it. Padding decisions like this one recur throughout signal processing.
The rule, with values
Formally p = ⌈log₂|x|⌉. Because the ceiling is taken, a number that already is a power of two returns its own exponent, and anything just above rises to the next one.
| x | log₂ x | p | 2p |
|---|---|---|---|
| 100 | 6.64 | 7 | 128 |
| 1000 | 9.97 | 10 | 1024 |
| 1024 | 10 | 10 | 1024 |
| 1025 | 10.0014 | 11 | 2048 |
| 3000 | 11.55 | 12 | 4096 |
| 48000 | 15.55 | 16 | 65536 |
The jump from 1024 to 1025 is the thing to remember: a single extra sample doubles the padded size. When a record is only slightly too long, trimming a few samples to fit 1024 is often better than padding to 2048.
Fractions and negative exponents
Nothing restricts the argument to integers. The method works for any nonzero real number, and for values below one the exponent is zero or negative. A common numeric curiosity is the value of e raised to the power −1, which is 1/e ≈ 0.367879. Then log₂(0.3679) ≈ −1.44, and the ceiling of that is −1. So nextpow2(e−1) = −1, and 2−1 = 0.5 is the smallest power of two that is not below 0.3679. The next one down, 0.25, is too small. Such a use shows up when rescaling amplitudes or choosing scaling steps rather than lengths.
Padding for fast convolution
The main practical use is linear convolution through the FFT. Convolving a signal of length 1000 with a kernel of length 24 gives an output of length 1000 + 24 − 1 = 1023. Both inputs must be zero-padded to at least 1023 samples so that the circular wraparound of the transform has no room to overlap the true result. The smallest power-of-two length that works is 2nextpow2(1023) = 1024. The example sits one sample below a boundary; with a kernel of length 26, the output is 1025 samples long and the padded length doubles to 2048. The procedure itself is described under convolution of vectors.
Spectral resolution and padding
Zero-padding does not add information. It interpolates the spectrum, placing more bins along the same underlying curve. A 1000-sample record at 8000 Hz has bins 8 Hz apart. Padded to 1024, the spacing becomes 8000/1024 = 7.8125 Hz. The peaks are drawn on a finer grid but cannot be separated any better than before, because the resolving power depends on the record length, not on the padding. Building the corresponding axis is shown on the frequency axis page.
Modern libraries and other sizes
Current FFT libraries handle lengths with small prime factors such as 2, 3, and 5 almost as fast as powers of two, so rounding up to 1024 is no longer mandatory for speed. A length like 1000 = 2³ · 5³ is perfectly efficient. What remains true is that lengths with a large prime factor are slow in practice, and the power-of-two choice is a safe default that costs at most a factor of two in size. Where speed matters, benchmark both a power of two and the nearest friendly factorization; the inverse fast Fourier transform article describes why the radix-2 case is the textbook baseline.
Pseudocode
function nextpow2(x):
p = 0
while 2^p < abs(x): p = p + 1 # for abs(x) >= 1
return p
For arguments below one, count downward instead, stopping at the first p where 2p−1 is already smaller than the argument. A library routine does this with a logarithm and a ceiling, but it must guard against rounding in log₂ at exact powers of two.