Why am I getting Index in position 1 is invalid. Array indices must be positive integers or logical values.
1 次查看(过去 30 天)
显示 更早的评论
clear
syms t;
Fs = 1.5; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T;
Yo=4+4.005*(sin(t)+sin(3*t)/3+sin(5*t)/5+sin(7*t)/7);
n = 2^nextpow2(L);
y = fft(Yo);
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);
plot(0:(Fs/n):(Fs/2-Fs/n),p1(i,1:n/2))
title ('Amplitude Spectrum')
xlabel('time')
ylabel('y(t)')
0 个评论
回答(3 个)
the cyclist
2022-2-12
clear
syms t;
Fs = 1.5; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T;
Yo=4+4.005*(sin(t)+sin(3*t)/3+sin(5*t)/5+sin(7*t)/7);
n = 2^nextpow2(L);
y = fft(Yo);
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);
plot(0:(Fs/n):(Fs/2-Fs/n),p1(i,1:n/2))
title ('Amplitude Spectrum')
xlabel('time')
ylabel('y(t)')
In the expression
p1(i,1:n/2)
you have not defined i, so MATLAB is trying to use the complex value sqrt(-1) as an index.
0 个评论
David Hill
2022-2-12
编辑:David Hill
2022-2-12
syms t;
Fs = 1.5; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T;
Yo=4+4.005*(sin(t)+sin(3*t)/3+sin(5*t)/5+sin(7*t)/7);
n = 2^nextpow2(L);
y = fft(Yo);
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);
plot(t(1:length(p1)),p1);%what is the i? The length of your t array must be equal to the length of p1
title ('Amplitude Spectrum')
xlabel('time')
ylabel('y(t)')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!