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
0 件のコメント
回答 (2 件)
Wayne King
2014 年 2 月 15 日
編集済み: Wayne King
2014 年 2 月 15 日
you misspelled
length()
There is no MATLAB function, lentght()
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 件のコメント
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!