Why am I getting Index exceeds the number of array elements. Index must not exceed 751.

1 visualización (últimos 30 días)
clear
syms t;
Fs = 1.5; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T;
x1 = 4+4.005*(sin(t)); % First row wave
x2 = sin(3*t)/3; % Second row wave
x3 = sin(5*t)/5; % Third row wave
x4 = sin(7*t)/7; % Fourth row wave
X = [x1; x2; x3; x4];
n = 2^nextpow2(L);
dim = 2;
Y = fft(X,n,dim);
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);
for i=1:4
plot(0:(Fs/n):(Fs/2-Fs/n),p1(1:n/2))
title ('Amplitude Spectrum')
xlabel('time')
ylabel('y(t)')
end
Index exceeds the number of array elements. Index must not exceed 751.
  1 comentario
David Hill
David Hill el 12 de Feb. de 2022
for i=1:4
plot(0:(Fs/n):(Fs/2-Fs/n),p1(1:n/2))
title ('Amplitude Spectrum')
xlabel('time')
ylabel('y(t)')
end
Above makes no sense. You are repeating the same plot four times (nothing changes). You need to stop your code and look at the sizes of your arrays. (see my previous answer to your other same question)

Iniciar sesión para comentar.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 12 de Feb. de 2022
Editada: Cris LaPierre el 12 de Feb. de 2022
The error is due to the fact that you are inconsitant with the use of n and L. Once you define n, you should use that instead of L (when calculating Y, p2, and p1).
There is another issue. You create X as a 4x1500 but then never distinguish downstream rows and columns when indexing variables created from X. The result is that linear indexing is used, which does not get you the same result. This is likely why you are using a for loop to plot - once for each signal
Try this:
Fs = 1.5; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T;
x1 = 4+4.005*(sin(t)); % First row wave
x2 = sin(3*t)/3; % Second row wave
x3 = sin(5*t)/5; % Third row wave
x4 = sin(7*t)/7; % Fourth row wave
X = [x1; x2; x3; x4];
n = 2^nextpow2(L);
dim = 2;
Y = fft(X,n,dim)'; % ###### transposed
f = 2*pi*Fs*(0:(n/2))/n; % <- multiplying ###### Changed L to n
p2 = abs(Y/n); % ###### Changed L to n
p1=p2(1:n/2+1,:); % ###### Changed L to n
p1(2:end-1,:) = 2*p1(2:end-1,:);
% for i=1:4 ###### Modifed so for loop is not needed
plot(0:(Fs/n):(Fs/2-Fs/n),p1(1:n/2,:))
title ('Amplitude Spectrum')
xlabel('time')
ylabel('y(t)')
legend

Categorías

Más información sobre Fourier Analysis and Filtering en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by