Help with DFT Algorithm (No FFT)
Show older comments
(Please refer to my first comment for further details regarding the issue) I am currently programming a script that plots the magnitude spectrum of a sinusoid, x(t) = 16cos(2*pi*100t), at Fs = 2kHz using discrete fourier transform (DFT) without using the FFT function as a way to fully understand the concept. The program is mostly complete, but I suspect there is something wrong with the way that I am collecting the complex numbers in the sum as the plot for magnitude spectrum does not represent what I believe it should represent once additional zeros and cycles are incorporated (see my first comment). Any advice to put me on the right track would be appreciated. The code that executes the DFT computation will be displayed below:
%% Input Vector Initialization Process
Fs = 2000; % Hz
n = 0:20-1;
x = 16 * cos(2 * pi * (1/20) * n); % Input Vector
%% Adding Additional Cycles (if needed)
cycles = 20;
xx = x; % x (expanded) = x
if cycles > 1
for j=2:cycles
xx = [xx x];
end
end
% Number of Zeroes Padded for x(n)
Z = 200;
%% Zero-Padding Process
xx = [xx zeros(1,Z)];
%% DFT Algorithm
X = []; %X(omega)
N = length(xx);
sum = 0;
df = Fs / N;
fr = (0:N-1)*df;
for k=0:N-1
for n=0:N-1
sum = sum + xx(n+1) * exp(-1i*2*pi*k*n / N);
end
X = [X sum];
sum = 0;
end
%% Plots
% Plotting Within Nyquist Range
nyq = ceil(length(X) / 2);
X = X(1:nyq);
fr = fr(1:nyq);
stem(fr,abs(X))
xlabel('Frequency (Hz)')
ylabel('|X(\omega)|')
title('Amplitude Spectrum of X(\omega)')
3 Comments
Matt J
on 3 Aug 2022
In what way does it seem "off"?
Devin Hunter
on 3 Aug 2022
Jan
on 3 Aug 2022
Note: This is not twitter. No # before the tags. Thanks.
Accepted Answer
More Answers (0)
Categories
Find more on Spectral Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

