代码如下:
function sw = dft(st)
% DFT - Discrete Fourier Transform
M = length(st);
N = M;
WN = exp(2*pi*j/N);
% Main loop
for n=0:N-1
temp = 0;
for m=0:M-1
mn = m'*n;
WNmn = WN.^mn;
temp = temp + st(m+1)*WNmn;
end
sw(n+1) = temp;
end
end
在matlab命令行中输入如下命令
s = sin(((1:64)-1)2pi*12.5/100);
figure(1);plot(s);figure(2);stem4(dft(s));figure(3);stem4(fft(s));
得到
经验证,该DFT函数得到的频域波形与FFT相同,正确。