why am I getting "Array indices must be positive integers or logical values"
    7 次查看(过去 30 天)
  
       显示 更早的评论
    
clear 
syms t; 
Fs=5; 
T=1/Fs; 
L=1500; 
t=(0:L-1)*T; 
Yo=4+4.005*(sin(t)+sin(3*t)/3+sin(5*t)/5+sin(7*t)/7); 
y = fft(Yo); 
f=2*pi*Fs(0:(L/2))/L; 
p2 = abs(y/L); 
p1=p2(1:L/2+1); 
p1(2:end-1) = 2*p1(2:end-1); 
%f=2*pi*Fs(0:(L/2))/L; 
plot(f,p1) 
title ('Amplitude Spectrum') 
xlabel('time') 
ylabel('y(t)')
采纳的回答
  Voss
      
      
 2022-2-11
        You were indexing Fs with a vector that had a 0 in it. Looks like you meant to be multiplying Fs by that vector instead:
clear 
syms t; 
Fs=5; 
T=1/Fs; 
L=1500; 
t=(0:L-1)*T; 
Yo=4+4.005*(sin(t)+sin(3*t)/3+sin(5*t)/5+sin(7*t)/7); 
y = fft(Yo); 
% f=2*pi*Fs(0:(L/2))/L; % <- indexing
f=2*pi*Fs*(0:(L/2))/L; % <- multiplying
p2 = abs(y/L); 
p1=p2(1:L/2+1); 
p1(2:end-1) = 2*p1(2:end-1); 
%f=2*pi*Fs(0:(L/2))/L; 
plot(f,p1) 
title ('Amplitude Spectrum') 
xlabel('time') 
ylabel('y(t)')
0 个评论
更多回答(2 个)
  Jan
      
      
 2022-2-11
        The variable Fs has the value 5. Then the expression:
Fs(0:(L/2))
is treated as indices. 0 is not a valid index. In addition Fs is a scalar and indexing cannot work apart from the index 1.
Maybe you mean:
Fs * (0:(L/2))
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





