Signal processing · spectra
fftshift and the Centered Spectrum
An FFT returns its bins in a fixed order: zero frequency first, then the positive frequencies, then the negative ones. Plotted as it comes, a spectrum looks split in two, with the interesting low frequencies stuck at both ends. The fftshift operation rotates the vector so zero frequency lands in the middle and the axis runs from the most negative frequency to the most positive. It is a small but constant companion of spectral work in signal processing.
What the reordering does
Let N be the length. Bins 0 to N/2 − 1 hold zero and the positive frequencies, and the remaining bins hold the negative ones, which the periodic nature of the transform makes equivalent to frequencies above the Nyquist point. fftshift is a circular shift by ⌊N/2⌋ positions: the second half moves in front of the first.
For N = 8 the bin numbers 0, 1, 2, 3, 4, 5, 6, 7 become 4, 5, 6, 7, 0, 1, 2, 3. Zero frequency, originally in position 0, ends up in position 4 (counting from zero), with four bins to its left and three to its right. The bin that landed first, number 4, is the Nyquist bin, and it appears on the negative side by convention.
The odd-length case
For an odd length the halves are unequal. With N = 7 the bin numbers 0 to 6 become 4, 5, 6, 0, 1, 2, 3. Now three bins sit on each side of zero frequency and the axis is symmetric, from −3 to +3 bins. There is no Nyquist bin at all in this case. The shift amount is ⌊7/2⌋ = 3, and that is also why the shift for odd lengths is not its own inverse: shifting twice does not return the original order. The companion operation, usually called ifftshift, shifts by ⌈N/2⌉ and undoes it exactly. For even lengths the two are identical.
Building the matching axis
Reordering the values is half the job. The frequencies must be reordered too, or the plot is mislabeled. For sampling rate fs and N points, bin k corresponds to k·fs/N before shifting. Take fs = 1000 Hz and N = 8. The spacing is 125 Hz and the raw frequencies are 0, 125, 250, 375, 500, 625, 750, 875 Hz, where the last four are equivalent to −500, −375, −250, −125.
After fftshift the axis is −500, −375, −250, −125, 0, 125, 250, 375 Hz. Notice that +500 Hz never appears: its alias, −500, takes the slot. A frequency axis built this way is the subject of the frequency axis page, and the time-domain counterpart is on the time axis page.
A short check on a real signal
A cosine of frequency 250 Hz sampled at 1000 Hz for 8 points has two nonzero bins, at raw positions 2 and 6, each of height N/2 = 4. Raw position 2 is +250 Hz and position 6 is the alias of −250 Hz. After fftshift the two peaks trade places: the peak at position 2 now reads −250 Hz, because position 4 is zero and each step is 125 Hz, and the peak at position 6 reads +250 Hz.
The picture is mirror-symmetric around position 4. That symmetry is a useful test, since a real signal always produces a spectrum whose magnitude mirrors around zero frequency. If the peaks are lopsided, either the shift or the axis is wrong.
Pseudocode
function fftshift(X):
N = length(X)
s = floor(N / 2)
return concat(X[N-s .. N-1], X[0 .. N-s-1])
The function is a pure reordering and loses no information. When the spectrum is later inverted, undo it first with the inverse shift, then apply the inverse discrete Fourier transform. Without that step the phases are scrambled in a way that is easy to mistake for a filtering error.