The issue is in line 52, where matlab states "Index exceeds the number of array elements. Index must not exceed 1."

5 views (last 30 days)
This algorithm is made to produce a magnitude, phase and frequency of 360 sine waves.
%% part one declearing variables and setting loop
load('mlab2_timeandrawdata.mat')%loads file
clearvars -except ex2data %clear all variables of data except the ex2data file
figure
T = ex2data(1,:); %listing all the data points in the first row of the
%% loop starting
for i = 2:1:10 %declearing variable i to be sampled at interval of 1, from 2 to 361
y = ex2data(i,:);%delcearing all data from the remaining rows 2-361
signal = y; %adds the two variable y and t to get the signal wave
plot(T,y,'r-') %plotting y and x red with dash line
axis ([0 2 -83 83]) %sets the limit for the
hold on; %adding below
plot(T, zeros(1,length(T)), 'k-'); %plotting zero line
%% find next point after zero crossing
sigPos = logical(signal>0); % find all positive points
cross = sigPos - circshift(sigPos,1); % find changeover points
plot(T(logical(cross)), signal(logical(cross)), 'bo');
%% assume straight line approximation between adjacent points
crossInd = find(cross); % x indices of cross points
nCross = length(crossInd); % number of cross points found
x0 = NaN(1,nCross); % vector of x-values for cross points
for aa = 2:nCross%from 2 to ncross
thisCross = crossInd(aa);%to track the variables
%% interpolate to get x coordinate of approx 0
x1 = thisCross-1; % before-crossing x-value
x2 = thisCross; % after-crossing x-value
y1 = signal (thisCross-1); % before-crossing y-value
y2 = signal(thisCross); % after-crossing y-value
ratio = (0-y1) / (y2-y1); % interpolate between points to find 0
x0(aa) = T(x1) + (ratio*(T(x2)-T(x1))); % estimate of x-value
end
% add to plot the zero line of function
plot(x0, zeros(1,nCross), 'go');
%% to avoid the outcome of have a NaN
if (isnan(x0(1)))
x0 = x0(2:length(x0));
end
%% set as a function of i
P = x0(3)-x0(1);% firs period given the two points on wave first and third point
d = x0(5)-x0(1);% second period with points 5 and 1 on zero line
PS(i) = d/P * 360; % Phase in radians, of the function of i to tracked
f(i) = 1/abs(x0(5)-x0(1)); %frequency of the function of i to tracked
M(i) = max(y);%sotres the magnitude
end
%% stores variables in a matrix
z = [M;f;PS]; %stores the matrix for M, f, and PS in column

Answers (1)

Chetan
Chetan on 16 Nov 2023
I understand you're working on signal processing in MATLAB and facing an "Index exceeds the number of array elements" error at line 52.
This typically occurs when you're trying to access an element beyond the size of the array.
In the code, you're trying to access the 3rd and 5th elements of the `x0` array:
P = x0(3)-x0(1);
d = x0(5)-x0(1);
The error likely arises because `x0` may not have enough elements, especially during the initial iterations of your loop.
To resolve this, add a condition to check if `x0` has sufficient elements before accessing them:
if length(x0) >= 5
P = x0(3)-x0(1);
d = x0(5)-x0(1);
PS(i) = d/P * 360;
f(i) = 1/abs(x0(5)-x0(1));
M(i) = max(y);
This ensures you only access the 3rd and 5th elements if they exist.
For more details on array indexing in MATLAB, refer to the following MathWorks Documentation https://www.mathworks.com/help/matlab/math/array-indexing.html
A more specific solution might require additional information about the data and the issues you're encountering.
I hope this helps!

Community Treasure Hunt

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

Start Hunting!