Errors when trying to computer DTFT?

2 次查看(过去 30 天)
Hello everyone,
I just started a MATLAB course and my first excercise is to write a MATLAB function that computes the DTFT of a signal x[k] at a given number of frequencies. Also a magnitude and phase plot must be made.
My code:
% X=dtft(x,phi);
%
% discrete-time Fourier transform
%
% INPUTS
% x : time-domain signal
% phi : vector containing the frequencies relative to the sampling frequency
% for which the DTFT has to be evaluated
% OUTPUT
% X : DTFT of x
function X=dtft(x,phi)
X=zeros(length(phi),1); % initialize X
for k=1:1:lentgth(x)-1 % compute the DTFT for all phi
for phi=0:1:360
x(k)*exp(1)^(1j*2*pi*phi*k);
end
end
subplot(2,1,1) % make a Bode plot of X
plot(phi,abs(X))
xlabel('frequency relative to sampling frequency')
ylabel('magnitude)')
grid
subplot(2,1,2)
plot(phi,unwrap(angle(X))*180/pi)
xlabel('frequency relative to sampling frequency')
ylabel('phase (degrees)')
grid
I get the following errors:
1 Undefined function 'lentgth' for input arguments of type 'double'.
2 Error in dtft (line 16) for k=1:1:lentgth(x)-1 % compute the DTFT for all phi
This is my first experience with MATLAB, so my apologies for such questions. Thanks in advance

回答(2 个)

Wayne King
Wayne King 2014-2-15
编辑:Wayne King 2014-2-15
you misspelled
length()
There is no MATLAB function, lentght()
  1 个评论
kishintai
kishintai 2014-2-15
编辑:kishintai 2014-2-15
Of course, yes excuse me for that.
Is it normal that when I run the function, nothing is plotted?
EDIT: Nevermind, my pc is just slow. The output of X is 0, so my function isn't correct

请先登录,再进行评论。


Wayne King
Wayne King 2014-2-15
You're not assigning X anything in your function except zeros
Minimally, in your for loop, you should be doing something like
X(k) = x(k)*exp(1)^(1j*2*pi*phi*k);
But even then you have a number of other problems with this function.
  2 个评论
kishintai
kishintai 2014-2-15
What do you mean by numer of other problems?
Wayne King
Wayne King 2014-2-15
I don't want to simply do your homework problem for you, but for example, why do you even ask for an input argument of phi, when you simply create a vector of phi in the for loop? In other words, you use the phi as a loop index, so you're not using the input argument at all.
I answered your original question, so you should accept that answer.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by