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

조회 수: 1 (최근 30일)
이 질문에 Cris LaPierre 님이 플래그를 지정함
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)')

답변 (3개)

the cyclist
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))
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 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)')

Image Analyst
Image Analyst 2022년 2월 12일

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by