Ifft2 results do not match that of irfft2 in Pyhton

5 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2025-2-28
编辑:Matt J 2025-2-28
Please provide the input data files, both the cases that give the right answer and the ones that don't.

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2025-2-28
编辑:Matt J 2025-2-28
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?

类别

Help CenterFile Exchange 中查找有关 Filter Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by