How to implement 2D discrete fourier transform in matlab without using fft2 built in function
4 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone,
I have an assignment that asks me to implement the 2D discrete fourier transform in matlab without using fft2 function.
I wrote a code that seems to be right (according to me) but when I compare the result I get with the result with the fft2 function, they are not the same.
If someone could tell me what's wrong, here's the code:
function [A] = fourier1 (X)
[M,N]=size(X);
A=zeros(M,N);
ro=0;
co=0;
for u=1:M
for v=1:N
for m=1:M
for n=1:N
co=co+X(m,n)*exp(1i*(-2)*pi*((u*m/M)+(v*n/N)));
end
ro=ro+co;
end
A(u,v)=ro;
ro=0;
co=0;
end
end
Thanks everyone!
2 件のコメント
Jonathan Doucette
2019 年 11 月 23 日
I don't know if it matters to you anymore, but I'll put this in here for others. Your problem was that you needed to use (u-1), (m-1), (v-1), and (n-1) in place of u, m, v, and n, respectively, as Matlab indexes from 1 but the equations index from 0. Also, I'm not sure if this matters, but I changed where the column zeroes out to after the ro=ro+co line.
Prateek Mittal
2022 年 10 月 17 日
編集済み: Prateek Mittal
2022 年 10 月 17 日
I am not sure whether it is needed now. I am posting here the updated version of the code for the sake of others.
function [A] = fourier1 (X)
[M,N]=size(X);
A=zeros(M,N);
ro=0;
co=0;
for u=1:M
for v=1:N
for m=1:M
for n=1:N
co=co+X(m,n)*exp(1i*(-2)*pi*(((u-1)*(m-1)/M)+((v-1)*(n-1)/N)));
end
ro=ro+co;
co = 0;
end
A(u,v)=ro;
ro=0;
co=0;
end
end
回答 (1 件)
KSSV
2018 年 8 月 21 日
No tested...test on your data:
function X = myFFT(x) %only works if N = 2^k
N = numel(x);
xp = x(1:2:end);
xpp = x(2:2:end);
if N>=8
Xp = myFFT(xp);
Xpp = myFFT(xpp);
X = zeros(N,1);
Wn = exp(-1i*2*pi*((0:N/2-1)')/N);
tmp = Wn .* Xpp;
X = [(Xp + tmp);(Xp -tmp)];
else
switch N
case 2
X = [1 1;1 -1]*x';
case 4
X = [1 0 1 0; 0 1 0 -1i; 1 0 -1 0;0 1 0 1i]*[1 0 1 0;1 0 -1 0;0 1 0 1;0 1 0 -1]*x';
otherwise
error('N not correct.');
end
end
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Fourier Analysis and Filtering についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!