Why am I getting Index in position 1 is invalid. Array indices must be positive integers or logical values.

1 visualizzazione (ultimi 30 giorni)
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)')

Risposte (3)

the cyclist
the cyclist il 12 Feb 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))
Index in position 1 is invalid. Array indices must be positive integers or logical values.
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.

David Hill
David Hill il 12 Feb 2022
Modificato: David Hill il 12 Feb 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)')

Image Analyst
Image Analyst il 12 Feb 2022

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by