Ifft2 results do not match that of irfft2 in Pyhton

5 views (last 30 days)
Hi All,
I am using the rfft2 function in Python to get the Fourier trasnformed data and use the ifft2 function in Matlab to get back the original data.
I firstly, load the spectral data from Python
clear
clc
rfft2_real = load('rfft2_real.txt');
rfft2_imag = load('rfft2_imag.txt');
Then I construct the whole spectral domain by adding the imaginary and real terms together
% Reconstruct the complex rfft2 output
rfft2_result = rfft2_real + 1i * rfft2_imag;
since rfft2 in Python stores only the positive frequencies for one of the dimensions I had to account for the missing negative frequencies in the follwoing way:
% Compute full FFT2 result by mirroring the data
[m, n_half] = size(rfft2_result);
n = 2 * (n_half - 1); % Reconstruct full FFT size
% Reconstruct the full frequency domain
fft2_result = zeros(m, n);
fft2_result(:, 1:n_half) = rfft2_result; % Copy half-spectrum
fft2_result(:, n_half+1:n) = conj(fliplr(rfft2_result(:, 2:end-1))); % Mirror
% Compute the inverse FFT to get back the original matrix
reconstructed_x = ifft2(fft2_result)
I played with this code and it gives me the right answer for some signals but for others it does not. Anybody knows the issue?
  1 Comment
Matt J
Matt J on 28 Feb 2025
Edited: Matt J on 28 Feb 2025
Please provide the input data files, both the cases that give the right answer and the ones that don't.

Sign in to comment.

Answers (1)

Matt J
Matt J on 28 Feb 2025
Edited: Matt J on 28 Feb 2025
I'm not a Python user, but if rfft2 only gives you positive x-frequencies, I would imagine it also only gives you positive y-frequencies as well, no? If so, why are you only mirroring across columns?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!