Why am I getting Index in position 1 is invalid. Array indices must be positive integers or logical values.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (3)
the cyclist
el 12 de Feb. de 2022
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 comentarios
David Hill
el 12 de Feb. de 2022
Editada: David Hill
el 12 de Feb. de 2022
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 comentarios
Image Analyst
el 12 de Feb. de 2022
Full explanation in the FAQ:
and in your duplicate post.
0 comentarios
Ver también
Categorías
Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!