fft2 — Compute two-dimensional discrete Fourier transforms in MATLAB and RunMat.
fft2(X) computes the two-dimensional discrete Fourier transform of X. It is equivalent to applying fft along the first two dimensions, with shape and ordering behavior consistent with MATLAB and RunMat.
Syntax
Y = fft2(X)
Y = fft2(X, SIZE)
Y = fft2(X, M, N)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Input array. |
SIZE | NumericArray | No | [] | Scalar N or two-element [M N] size vector. |
M | NumericScalar | No | [] | Output row count for transform. |
N | NumericScalar | No | [] | Output column count for transform. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | 2-D complex Fourier spectrum output. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:fft2:ArgCount | More than three input arguments are supplied. | fft2: invalid argument count |
RunMat:fft2:InvalidLength | Length/size arguments are invalid. | fft2: invalid transform length argument |
RunMat:fft2:InvalidSizeVector | Single SIZE argument is invalid. | fft2: invalid size vector argument |
RunMat:fft2:InvalidInput | Input cannot be converted to supported numeric/complex domain. | fft2: invalid input |
RunMat:fft2:Internal | FFT2 execution or tensor shaping fails. | fft2: internal error |
How fft2 works
fft2(X)transforms along the first and second dimensions that have size greater than one.fft2(X, M, N)zero-pads or truncatesXtoMrows andNcolumns before evaluating the 2-D transform.fft2(X, SIZE)accepts a two-element vector (or scalar) specifying the transform lengths.- Real inputs produce complex outputs; complex inputs are transformed element-wise with no additional conversion.
- Higher-dimensional inputs are transformed slice-by-slice across trailing dimensions, matching MATLAB behaviour.
- Empty dimensions yield empty outputs; zero padding with
0produces a zero-sized complex tensor. - GPU arrays execute on-device when the provider advertises the
fft_dimhook; otherwise RunMat gathers the data and performs the transform on the host usingrustfft.
GPU memory and residency
You usually do NOT need to call gpuArray manually. The fusion planner and native acceleration layer keep tensors on the GPU when a provider offers FFT kernels. If the provider lacks fft_dim, RunMat gathers inputs, evaluates the FFT pair on the host, and returns a MATLAB-compatible complex tensor. You can still use gpuArray for explicit residency control, particularly when interoperating with MATLAB code that expects it.
Examples
Computing the 2-D FFT of a small matrix
X = [1 2; 3 4];
Y = fft2(X)Expected output:
Y =
10 + 0i -2 + 0i
-4 + 0i 0 + 0iZero-padding an image patch before fft2
patch = [1 0 1; 0 1 0; 1 0 1];
F = fft2(patch, 8, 8)Specifying transform lengths with a size vector
X = rand(4, 6);
F = fft2(X, [8 4]); % pad rows to 8 and truncate columns to 4Using fft2 on gpuArray data
G = gpuArray(rand(256, 256));
F = fft2(G);
R = gather(F)Applying fft2 to each slice of a 3-D volume
V = rand(64, 64, 10);
spectra = fft2(V)Verifying fft2 against sequential fft calls
X = rand(5, 7);
sequential = fft(fft(X, [], 1), [], 2);
direct = fft2(X)Using fft2 with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how fft2 changes the result.
Run a small fft2 example, explain the result, then change one input and compare the output.
FAQ
Is fft2(X) the same as fft(fft(X, [], 1), [], 2)?⌄
Yes. RunMat literally performs the two sequential transforms so the results match MATLAB exactly.
How do zero-length transform sizes behave?⌄
Passing 0 for either M or N produces a complex tensor with zero elements along that dimension.
Can I use a single scalar for the size argument?⌄
Yes. fft2(X, K) is shorthand for fft2(X, [K K]), padding or truncating both dimensions to K.
What happens when X has more than two dimensions?⌄
RunMat applies fft2 to every 2-D slice defined by the first two dimensions, leaving higher dimensions untouched.
Do I get complex outputs for real inputs?⌄
Always. Even when the imaginary parts are zero, outputs are stored as complex tensors to mirror MATLAB semantics.
Will fft2 run on the GPU automatically?⌄
Yes if the active provider implements fft_dim. Otherwise RunMat gathers to the host and performs the transform with rustfft.
Does fft2 normalise the output?⌄
No. Like MATLAB, the forward FFT leaves scaling untouched; use ifft2 for the inverse with 1/(M*N) scaling.
Can I mix [] with explicit sizes (e.g., fft2(X, [], 128))?⌄
Yes. Passing [] leaves that dimension unchanged while applying the specified size to the other dimension.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fft2 is executed, line by line, in Rust.
- View the source for fft2 in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.