Plotting DTFT in Matlab

216 Ansichten (letzte 30 Tage)
Tomasz Nowopolski
Tomasz Nowopolski am 26 Nov. 2022
Beantwortet: Ishan am 29 Nov. 2022
I'm trying to create a DTFT function in Matlab for my assignment.
I'm using the built-in FFT function to compare my results and wager if my results are correct (I'm assuming the results should be the same just with different error margins), but the result I'm getting is entirely different and I'm not sure why.
Main code:
L = 1000;
fg = 1000;
fs = 100000;
T = 1/fs;
tmin = 0;
K = 1;
n = tmin:L;
t = n*T;
%Base function calculation
x = xdp1(K, fg, t);
%Fourier transform calculation
w = t;
%X = dtft(x, t);
%X = dtft2(x, w, n);
X = dtft3(x, w, n);
X_FFT = fft(x);
% Top plot
tiledlayout(3,1)
nexttile
plot(t, x)
ylim([min(x) max(x)])
xlim([0 max(t)])
title('Signal')
% Middle plot
nexttile
plot(t, abs(X));
title('DTFT')
% Bottom plot
nexttile
plot(t, abs(X_FFT))
ylim([min(abs(X_FFT)) max(abs(X_FFT))])
xlim([0 max(t)])
title('FFT')
Signal calc code (xdp1):
function x = xdp1(K, fg, t)
x = K * 2 * fg * sinc(2 * pi * fg * t);
end
DTFT calc code (dtft3):
function X = dtft3(x, w, n)
X = exp(-1i*w'*n) * x.';
end
My other attempts that minimized the Matlab exclusive syntax:
DTFT calc code (dtft2):
function X = dtft2(x, w, n)
for i=1:length(w)
X(i)=sum(x.*exp(-1i*w(i)*n));
end
end
DTFT calc code (dtft):
function X = dtft(x, w)
X = zeros(1, length(x));
for n=1:length(x)
X = X + x(n) * exp(-1i * w * n);
end
end
Result plot:

Antworten (1)

Ishan
Ishan am 29 Nov. 2022
Hi Tomasz,
I understand you are trying to cross verify your DTFT function. You can refer to the functions in the file exchange attached below
Hope this helps out!

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by