Errors when trying to computer DTFT?
4 次查看(过去 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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!