What does the fftshift function do in MATLAB / RunMat?
fftshift(X) circularly shifts the output of an FFT so that the zero-frequency
component moves to the center of each transformed dimension. This makes spectra
easier to inspect and aligns with MATLAB's plotting conventions.
How does the fftshift function behave in MATLAB / RunMat?
- When called without a dimension list,
fftshiftshifts along every dimension byfloor(size(X, dim) / 2). fftshift(X, dims)shifts only the specified dimensions.dimscan be a scalar, vector, or logical mask.- Dimensions of length 0 or 1 are left unchanged.
- Inputs can be real or complex and may already reside on the GPU (
gpuArray).
fftshift Function GPU Execution Behaviour
RunMat asks the active acceleration provider to execute fftshift via the
circshift hook (with predetermined offsets). If the provider cannot satisfy
the request, the tensor is gathered exactly once, shifted on the host, and
optionally re-uploaded. Scalars remain on their existing device.
Examples of using the fftshift function in MATLAB / RunMat
Centering the spectrum of a 1-D FFT result with even length
x = [0 1 2 3 4 5 6 7];
fx = fft(x);
y = fftshift(fx);
Expected output:
y = [4 5 6 7 0 1 2 3];
Handling odd-length vectors
x = 1:5;
y = fftshift(x);
Expected output:
y = [4 5 1 2 3];
Centering both axes of a 2-D FFT
A = [1 2 3; 4 5 6];
C = fftshift(A);
Expected output:
C =
6 4 5
3 1 2
Shifting only one dimension of a matrix
A = [1 2 3; 4 5 6];
rowCentered = fftshift(A, 1); % shift rows only
Expected output:
rowCentered =
4 5 6
1 2 3
Applying fftshift to a gpuArray spectrum
G = gpuArray(0:7);
centered = fftshift(G);
H = gather(centered);
Expected output:
H = [4 5 6 7 0 1 2 3];
GPU residency in RunMat (Do I need gpuArray?)
RunMat's auto-offload keeps FFT spectra on the GPU whenever the active provider
exposes the circshift hook. In that case fftshift runs entirely on the
device and downstream fused operations continue without a gather. If no GPU
provider is registered—or it does not expose circshift—RunMat gathers the data
once, performs the host shift, and uploads the result back to the device so
subsequent work can still benefit from acceleration. You can always call
gpuArray explicitly when you need MATLAB compatibility or want to guarantee a
specific residency boundary.
FAQ
When should I call fftshift?
Call fftshift whenever you need to center FFT outputs before visualising
spectra, computing radial averages, or applying filters that expect zero
frequency in the middle of the array.
Does fftshift modify the phase or magnitude of the FFT?
No. fftshift only reorders the samples. Magnitudes, phases, and the overall
information content remain unchanged.
How do I undo fftshift?
Use ifftshift, which performs the inverse rearrangement. The sequence
ifftshift(fftshift(X)) returns X for all supported inputs.
Can I apply fftshift to only one dimension?
Yes. Pass a dimension index or vector, e.g. fftshift(X, 2) to shift column
channels only.
Does fftshift work with gpuArray inputs?
Yes. RunMat keeps data on the GPU whenever the provider exposes the circshift
hook, matching MATLAB's gpuArray behaviour.
How does fftshift handle empty inputs?
Empty arrays are returned unchanged with identical shape metadata.
Can I use fftshift on logical arrays?
Yes. Logical arrays are shifted without changing their logical element type.
See Also
fft, ifft, ifftshift, circshift, gpuArray, gather
Source & Feedback
- Source:
crates/runmat-runtime/src/builtins/math/fft/fftshift.rs - Found a bug? Open an issue with details and a minimal repro.