A robot diagnostics dashboard can display a spectrum with negative frequencies on the left and positive frequencies on the right. The display depends on a paired reorder. The frequency coordinates and complex FFT coefficients must move together. Reordering the magnitudes alone leaves a plausible-looking plot with the wrong frequencies attached to its points. In the example below, a peak at positive 2 Hz acquires a label of negative 4 Hz when it is read against the original frequency array.
The example uses a five-sample complex sinusoid and NumPy 2.5.2. A complex signal makes one signed frequency easy to track. It is a synthetic diagnostic fixture, with no readings from a robot sensor and no implication that a real vibration trace normally has energy on only one side of a two-sided spectrum.
Build the frequency bins from the transform length
Take five samples spaced 0.1 seconds apart. The bin spacing is one divided by the sample count times the sample interval, or 2 Hz here. NumPy's fftfreq returns frequencies in the order produced by its ordinary FFT. For five samples the order is zero, two positive frequencies, then two negative frequencies. The negative frequencies appear in increasing order within their part of the array.
import numpy as np
n = 5
dt = 0.1
t = np.arange(n) * dt
signal = np.exp(2j * np.pi * 2 * t)
bins = np.fft.fftfreq(n, d=dt)
spectrum = np.fft.fft(signal)
assert bins.tolist() == [0., 2., 4., -4., -2.]
assert bins[np.argmax(np.abs(spectrum))] == 2.
Passing the sample spacing in seconds makes the returned frequencies cycles per second. Leaving it at its default would give cycles per sample. A diagnostic log that stores timestamps in milliseconds needs to convert the time step before assigning labels in hertz. The example is uniformly sampled. A frequency axis computed from one sample spacing does not account for time jitter or missing samples in an actual trace.
Shift both arrays for a centered view
The fftshift operation moves the zero-frequency bin to the center. Apply it independently to the frequency bins and to the spectrum, with the same order along the frequency axis. Computing magnitude after the shift preserves the relationship of each coefficient to its frequency. The peak moves from position one to position three in the five-element array.
centered_bins = np.fft.fftshift(bins)
centered_spectrum = np.fft.fftshift(spectrum)
peak_index = np.argmax(np.abs(centered_spectrum))
assert centered_bins.tolist() == [-4., -2., 0., 2., 4.]
assert peak_index == 3
assert centered_bins[peak_index] == 2.
assert bins[peak_index] == -4.
Reading the shifted peak against the original coordinates produces the negative label even though the transform itself is correct. The raw peak magnitude is 5 in this example under NumPy's default forward normalization. The shift changes the position of that magnitude without changing its value. The fixture checks frequency identity and ordering. It does not use 5 as a calibrated amplitude or power.
Choose the axis in a batch
When a telemetry array has channels along rows and sample time along columns, specify the transform axis and shift that axis only. NumPy's fftshift defaults to shifting every axis. Applying that default to a matrix of channels and frequencies can also rotate the channel rows. The channel names will then describe the wrong coefficients unless the application moves them too. Choosing the frequency axis keeps channel order intact.
For a channels-by-samples array, one pattern is to compute np.fft.fft(samples, axis=-1), then call np.fft.fftshift on the result with axes=-1. Build one frequency vector from the length of the array along that axis and apply fftshift to that vector. Keep the channel labels separately. If inputs have different sample intervals or FFT lengths, the frequency coordinates may have to be computed individually before being combined in a report.
Reverse the shift when reconstructing the signal
If the centered complex coefficients are passed to an inverse transform, first restore the ordinary FFT order with ifftshift. For an even length, the two shifts agree. For an odd length, they differ by one position. Applying fftshift twice to this five-element spectrum does not give back the original arrangement.
recovered_spectrum = np.fft.ifftshift(centered_spectrum)
recovered_signal = np.fft.ifft(recovered_spectrum)
assert np.array_equal(recovered_spectrum, spectrum)
assert np.allclose(recovered_signal, signal)
An inverse transform needs complex coefficients. A display that keeps only their magnitudes has discarded the phase information that the reconstruction uses. Keep display arrays separate from any reconstruction input, or record the layout and value type explicitly when storing a transformed signal.
Inspect a known signed-frequency fixture in both views. Assert that the peak carries the same frequency label before and after centering, and compare reconstruction with the original complex samples. Use both even and odd lengths and include multiple channels with distinct expected peaks. A real cosine can also be useful for testing the expected symmetric pair of peaks. The asymmetric complex fixture catches sign and label mistakes that a symmetric spectrum can make harder to see.
Check the frequency vector where even and odd lengths diverge
With six samples at the same spacing, the shifted bins begin at negative 5 Hz, the ordinary FFT convention for the Nyquist bin in an even-length transform. At five samples, the shifted bins begin at negative 4 Hz, with a matching positive 4 Hz bin at the other end. The two cases have different spacing as well as different endpoints. A plotting routine that builds a frequency axis from inclusive endpoints can slip by one bin at the even length. Derive the vector from fftfreq and reorder it when you reorder the coefficients. If a plot uses a one-sided real transform, use the corresponding real-transform frequency vector and keep its representation explicit.
Sources checked November 28, 2026
NumPy, numpy.fft.fftfreq
https://numpy.org/doc/stable/reference/generated/numpy.fft.fftfreq.html
NumPy, numpy.fft.fftshift
https://numpy.org/doc/stable/reference/generated/numpy.fft.fftshift.html
NumPy, numpy.fft.ifftshift
https://numpy.org/doc/stable/reference/generated/numpy.fft.ifftshift.html
Illustration. AI-generated concept of a centered spectrum with a highlighted positive-frequency bin, shown on a robot diagnostic bench.