Index exceeds the number of array elements. Index must not exceed 1.

2 次查看(过去 30 天)
I have this error and im not sure how to correct it. I have read through other similar Q&A's but cant seem to figure out how to fix the error.
ERROR MESSAGE:
Index exceeds the number of array elements. Index must not exceed 1.
Error in untitled2 (line 6)
t4 = (0:N-1)/fs2(k); % Time vector
CODE:
close all;
N = 512; % Number of points, N
N2 = N/2; % Half N
fs2 = 1000; % Sample frequencies
for k = 1:2
t4 = (0:N-1)/fs2(k); % Time vector
f1 = (1:N)*fs2(k)/N; % Frequency vector
x4 = sin(2*pi*200*t4) + sin(2*pi*400*t4); % Signal
y4 = sin(2*pi*200*t4) + sin(2*pi*900*t4);
Xmag = abs(fft(x4)); % Magnitude spectrum
Ymag = abs(fft(y4));
plot(f1(1:N2),Xmag(1:N2),'r',f1(1:N2),Ymag(1:N2),'g');% Plot magnitude spectrum
end

采纳的回答

KSSV
KSSV 2022-2-1
编辑:KSSV 2022-2-1
You are expecting fs2 to be of size 1*2. You have take k = 1,2 but your fs2 has only one value. Either take k = 1 alone or define fs2 as 1*2.
close all;
N = 512; % Number of points, N
N2 = N/2; % Half N
fs2 = [1000 2000]; %<---- changed here % Sample frequencies
for k = 1:2
t4 = (0:N-1)/fs2(k); % Time vector
f1 = (1:N)*fs2(k)/N; % Frequency vector
x4 = sin(2*pi*200*t4) + sin(2*pi*400*t4); % Signal
y4 = sin(2*pi*200*t4) + sin(2*pi*900*t4);
Xmag = abs(fft(x4)); % Magnitude spectrum
Ymag = abs(fft(y4));
plot(f1(1:N2),Xmag(1:N2),'r',f1(1:N2),Ymag(1:N2),'g');% Plot magnitude spectrum
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by