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 Center および File ExchangeNumber Theory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by