Question about DFT and FFT comparision

14 views (last 30 days)
I am interested about FFT scaling in MATLAB, so I have studied discussion in https://www.mathworks.com/matlabcentral/answers/15770-scaling-the-fft-and-the-ifft and basing on the informations desribed there I wanted to compare the results obtained from FFT and hand-written DFT algorithm. Here is the code:
% DFT
fs=5; % Sampling Frequency
t=0:1/fs:7/fs; % Time vector with the length of 8 (2^3)
A=0.5;
s=A*sin(2*pi*2*t);
S=zeros(length(s));
L=length(s);
for m=1:L
for n=1:L
S(n,m)=s(1,m)*exp(-2*pi*1i*(m*n)/L); % DFT from the formula
end
end
S2=zeros(1,L);
for k=1:L
S2(k) = sum(S(k,:));
end
S2_mag =abs(S2)/fs % Magnitude response scaled by the 1/fs factor
Then, I've prepared code for FFT:
N=2^nextpow2(L);
S1=fft(s,N);
Sm=abs(S1)/fs
At the end I've compared vectors S2_mag and Sm, here are the results:
>> Sm
Sm =
0.0363 0.0407 0.0629 0.3632 0.1539 0.3632 0.0629 0.0407
>> S2_mag
S2_mag =
0.0407 0.0629 0.3632 0.1539 0.3632 0.0629 0.0407 0.0363
I've noticed that I didn't have to multiply the DFT result by the L*A/2 (although in the link I've attached there stands that FFT implementation is scaled by this factor), while I've noticed that the result of DFT is the same as FFT, but it is "shifted" somewhat, so the second element of Sm is equal to the first in S2_mag and so on. I'm a little bit confused. Does anybody know the reason of this result?

Accepted Answer

Abhishek Ballaney
Abhishek Ballaney on 12 Feb 2018
https://in.mathworks.com/help/signal/ug/discrete-fourier-transform.html
https://in.mathworks.com/help/matlab/ref/fft.html

More Answers (1)

Hawraa Fadhil
Hawraa Fadhil on 11 Jun 2021
x = [2 3 -1 4]; %DFT-------------------------------------- N = length(x); X = zeros(4,1) for k = 0:N-1 for n = 0:N-1 X(k+1) = X(k+1) + x(n+1)*exp(-j*pi/2*n*k) end end t = 0:N-1 subplot(311) stem(t,x); xlabel('Time (s)'); ylabel('Amplitude'); title('sequence') subplot(312) stem(t,X) xlabel('Frequency'); ylabel('|X(k)|'); title('DFT') %----------------------------------------------- %FFT subplot(313) Y = fft(x) ; stem(Y) title('FFT')

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!