Errors when trying to computer DTFT?

1 view (last 30 days)
kishintai
kishintai on 15 Feb 2014
Commented: Wayne King on 15 Feb 2014
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

Answers (2)

Wayne King
Wayne King on 15 Feb 2014
Edited: Wayne King on 15 Feb 2014
you misspelled
length()
There is no MATLAB function, lentght()
  1 Comment
kishintai
kishintai on 15 Feb 2014
Edited: kishintai on 15 Feb 2014
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

Sign in to comment.


Wayne King
Wayne King on 15 Feb 2014
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 Comments
kishintai
kishintai on 15 Feb 2014
What do you mean by numer of other problems?
Wayne King
Wayne King on 15 Feb 2014
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.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!