Understanding FFT in MATLAB

3 次查看(过去 30 天)
Janee
Janee 2024-3-13
回答: Paul 2024-3-13
I am exploring the differences between fftw in MATLAB and C/C++. I think this maybe a straightforward question but for some reason I don't see it.
Assume the following MATLAB code:
Nx = 8;
Ny = 8;
Lx =16;
%-----------
xi_x = (2*pi)/Lx;
xi = ((0:Nx-1)/Nx)*(2*pi);
x = xi/xi_x;
ylow = 0;
yupp =6;
ygl = cos ( pi * ( 0 : Ny ) / Ny )';
ygl = (1/2)*(((yupp-ylow)*ygl) + (yupp+ylow));
[X,Y] = meshgrid(x,ygl);
%define function u:
u = (Y-ylow) .* (Y-yupp) .* sin( (2*pi / Lx) * X);
uh = fft(u,[],2);
%pad uh with zeros
[Ny Nx]=size(uh);
m=Nx*3/2;
uhp=[uh(:,1:Nx/2) zeros(Ny,(m-Nx)) uh(:,Nx/2+1:Nx)];
%take ifft
up=real(ifft(uhp,[],2));
u1 = real(ifft(uh,[],2)); %same as u
My question above since both ``uh`` and ``uhp`` are the same values with extra zero padding, why is the inverse of fft of these two different? Meaning if ``uh`` is:
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 5.2721i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 - 5.2721i
-0.0000 + 0.0000i 0.0000 +18.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -18.0000i
-0.0000 + 0.0000i 0.0000 +30.7279i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -30.7279i
-0.0000 + 0.0000i 0.0000 +36.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -36.0000i
-0.0000 + 0.0000i 0.0000 +30.7279i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -30.7279i
-0.0000 + 0.0000i 0.0000 +18.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -18.0000i
0.0000 + 0.0000i 0.0000 + 5.2721i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 - 5.2721i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
and ``uhp`` is:
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 5.2721i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 - 5.2721i
-0.0000 + 0.0000i 0.0000 +18.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -18.0000i
-0.0000 + 0.0000i 0.0000 +30.7279i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -30.7279i
-0.0000 + 0.0000i 0.0000 +36.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -36.0000i
-0.0000 + 0.0000i 0.0000 +30.7279i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -30.7279i
-0.0000 + 0.0000i 0.0000 +18.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 -18.0000i
0.0000 + 0.0000i 0.0000 + 5.2721i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 - 5.2721i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
Then why the `` real(ifft(uh,[],2))`` equals:
0 0 0 0 0 0 0 0
0 -0.9320 -1.3180 -0.9320 -0.0000 0.9320 1.3180 0.9320
-0.0000 -3.1820 -4.5000 -3.1820 -0.0000 3.1820 4.5000 3.1820
0 -5.4320 -7.6820 -5.4320 -0.0000 5.4320 7.6820 5.4320
-0.0000 -6.3640 -9.0000 -6.3640 -0.0000 6.3640 9.0000 6.3640
0 -5.4320 -7.6820 -5.4320 -0.0000 5.4320 7.6820 5.4320
-0.0000 -3.1820 -4.5000 -3.1820 -0.0000 3.1820 4.5000 3.1820
0 -0.9320 -1.3180 -0.9320 -0.0000 0.9320 1.3180 0.9320
0 0 0 0 0 0 0 0
and ``up=ifft(uhp,[],2); `` is:
0 0 0 0 0 0 0 0 0 0 0 0
0 -0.4393 -0.7610 -0.8787 -0.7610 -0.4393 -0.0000 0.4393 0.7610 0.8787 0.7610 0.4393
-0.0000 -1.5000 -2.5981 -3.0000 -2.5981 -1.5000 -0.0000 1.5000 2.5981 3.0000 2.5981 1.5000
0 -2.5607 -4.4352 -5.1213 -4.4352 -2.5607 -0.0000 2.5607 4.4352 5.1213 4.4352 2.5607
-0.0000 -3.0000 -5.1962 -6.0000 -5.1962 -3.0000 -0.0000 3.0000 5.1962 6.0000 5.1962 3.0000
0 -2.5607 -4.4352 -5.1213 -4.4352 -2.5607 -0.0000 2.5607 4.4352 5.1213 4.4352 2.5607
-0.0000 -1.5000 -2.5981 -3.0000 -2.5981 -1.5000 -0.0000 1.5000 2.5981 3.0000 2.5981 1.5000
0 -0.4393 -0.7610 -0.8787 -0.7610 -0.4393 -0.0000 0.4393 0.7610 0.8787 0.7610 0.4393
0 0 0 0 0 0 0 0 0 0 0 0
The reason I ask b/c in C/C++ when padding my arrays similarly and taking the ifft my code returns the same output, so ``real(ifft(uh,[],2))`` = ``real(ifft(uhp,[],2))``

回答(1 个)

Paul
Paul 2024-3-13
Hi Janee,
Speaking only to the Matlab side of this question, why would it expected that the rows of u1 and up be the same? Each row of u1 and up will have a different number of elements. Based that fact alone, how could u1 and up be the same?
I'm suprised with that result in C++.

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by