Errors when trying to computer DTFT?

조회 수: 2 (최근 30일)
kishintai
kishintai 2014년 2월 15일
댓글: Wayne King 2014년 2월 15일
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