Index exceeds the number of array elements. Index must not exceed 1.
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
KSSV
on 1 Feb 2022
Edited: KSSV
on 1 Feb 2022
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 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!