What does the ifft2 function do in MATLAB / RunMat?
ifft2(X) computes the two-dimensional inverse discrete Fourier transform (IDFT) of X. It
undoes the effect of fft2 by applying ifft along the first dimension and then along the
second dimension, preserving MATLAB’s column-major semantics.
How does the ifft2 function behave in MATLAB / RunMat?
ifft2(X)transforms along the first two dimensions whose sizes exceed one.ifft2(X, M, N)zero-pads or truncates the spectrum toMrows andNcolumns before the inverse.ifft2(X, SIZE)accepts a scalar or two-element vector describing the transform lengths.ifft2(..., 'symmetric')discards tiny imaginary parts and returns a real matrix when the spectrum is conjugate-symmetric.'nonsymmetric'keeps the complex result.- Higher-dimensional inputs are processed slice-by-slice across trailing dimensions.
- Empty sizes and zero padding mirror MATLAB behaviour, producing empty outputs when any requested length is zero.
Examples of using the ifft2 function in MATLAB / RunMat
Reconstructing an image patch from its 2-D spectrum
F = [10 -2 0 -2;
-4 0 0 0];
x = ifft2(F);
Expected output:
x =
1.0000 2.0000
3.0000 4.0000
Zero-padding before the inverse transform
F = fft2([1 0; 0 1], 4, 4);
spatial = ifft2(F, 4, 4);
spatial is a 4×4 matrix representing the zero-padded impulse response.
Supplying transform lengths with a size vector
Y = fft2(rand(3,4));
X = ifft2(Y, [5 2]); % pad rows to 5, truncate columns to 2
Forcing a real-valued result with 'symmetric'
F = fft2([1 2; 3 4]);
realImage = ifft2(F, 'symmetric');
realImage equals the original matrix and has no residual imaginary values.
Running ifft2 on gpuArray inputs
G = gpuArray(fft2(peaks(64)));
spatial = ifft2(G);
result = gather(spatial);
When the provider exposes ifft_dim, both passes execute on the GPU. Otherwise RunMat gathers G
and finishes on the host transparently.
Recovering each slice of a volume
spectra = fft2(rand(16, 16, 10));
volume = ifft2(spectra);
Every 16×16 slice along the third dimension is reconstructed independently.
GPU residency in RunMat (Do I need gpuArray?)
You usually do not need to call gpuArray manually. RunMat’s native acceleration layer keeps
intermediate tensors on the GPU whenever the provider implements ifft_dim. If the provider lacks
that hook (or detects an unsupported length), RunMat gathers the input once, executes the inverse
transform on the CPU with rustfft, and returns a MATLAB-compatible result automatically.
FAQ
-
Is
ifft2(X)equivalent toifft(ifft(X, [], 1), [], 2)?
Yes. RunMat literally performs two sequential 1-D inverse transforms so the behaviour matches MATLAB exactly. -
How do zero-length sizes behave?
Passing0for either transform length produces an output with zero elements along that dimension, just like MATLAB. -
Can I mix
[]with explicit sizes (e.g.,ifft2(X, [], 64))?
Yes.[]leaves that dimension unchanged while the other argument controls padding or truncation. -
What does the
'symmetric'flag do?
It tells RunMat to coerce the result to real values, assuming the spectrum is conjugate-symmetric. Imaginary parts are dropped. -
What happens when the input is real-valued?
RunMat promotes the data to complex with zero imaginary parts before applying the inverse. The output can still be coerced to real with'symmetric'. -
Will
ifft2run on the GPU automatically?
Yes when the active provider exposesifft_dim. Otherwise the runtime gathers to the host and evaluates the inverse usingrustfft. -
Does
ifft2normalise the result?
Yes. The builtin divides by the product of the transform lengths so thatifft2(fft2(X))reproducesX. -
Can I pass
gpuArraysize vectors or symmetry flags?
No. Length arguments must be host scalars or vectors, and the symmetry flag must be a host string, mirroring MATLAB’s restrictions. -
How are higher-dimensional arrays handled?
Transformations are applied to every 2-D slice defined by the first two dimensions; trailing dimensions are preserved unchanged. -
Does
'nonsymmetric'change the result?
It simply states the default behaviour (return complex outputs) but is accepted for MATLAB compatibility.
See Also
fft2, ifft, fft, gpuArray, gather
Source & Feedback
- Full source:
crates/runmat-runtime/src/builtins/math/fft/ifft2.rs - Found an issue? Open a ticket with a minimal reproduction.