Moving window FFT, Index exceeds array bounds error

I keep getting an index exceeds array bounds error in the following code and I am unsure why. The following is my current code after I made a change in attempt to fix it.
%%%%%%
ch1 = data(datastart(1):datastart(2)); %create ch1 data vector (waveform to analyze)
ch2 = data(dataend(1):dataend(2)); %create ch2 data vector (heart rate)
c_1 = 0.003522616; %PIVA constants
c_2 = 0.001656697;
c_3 = 0.012566030;
window = 8192;
r = floor(length(ch1)/window)*window; %truncate to even multiple of window
lng =(r/window)+1; %determine number of windows
rch1 = ch1(1:r); %new ch1 with multiple of window length
%rch1 = ch1;
formatOut = 'HH:MM:SS:FFF';
DateNumber = (blocktimes+((1:length(ch1))/1000/60/60/24))-0.001/60/60/24; % store date numbers for each data point
%TIME = datestr(DateNumber, formatOut); %Compute every time value
Y = zeros(lng-1,window+1); %initialize variables
A = zeros(lng-1,1);
PY = zeros(lng-1,window+1);
tstart = cell(lng-1,1);
tstop = cell(lng-1,1);
pkVals=zeros(lng-1,3);
pkLocs=zeros(lng-1,3);
PIVA_Table = cell2table({});
f = tickrate/window*(0:window/2); %fft frequency
j = 1;
i=1;
k = 1;
for i = 1:lng-1 %run for number of windows
k = j+window; %increment as window size
tstart{i,:} = datestr(DateNumber(1,j), formatOut); %start of window
tstop{i,:} = datestr(DateNumber(1,k), formatOut); %end of window
t(i,:) = (DateNumber(1,j));
Y(i,:) = fft(rch1(j:k)); %store fft values
A(i,:) = (nanmean(ch2(1,j:k)))/60; %compute mean BPM in window
PY(i,:) = Y(i,:).*conj(Y(i,:))/window; %store power
for m=1:3 %search for f1, f2, f3
P = PY(i,1:length(f)); %pull out particular PY and store as P for use
ind = f >= m*A(i,:)-0.3 & f <= m*(A(i,:))+0.3; %check in range based on mean BPM
[peakVals,peakLocs]=findpeaks(P(ind),f(ind),'sortstr','descend'); %extract peaks
if isempty(peakVals) %if no peak found, set to 0
pkVals(i,m)=0;
pkLocs(i,m)=0;
else
pkVals(i,m)=peakVals(1); %store found peaks and locs
pkLocs(i,m)=peakLocs(1);
end
end
PIVA(i,1) = (pkVals(i,1)*c_1 + pkVals(i,2)*c_2 + pkVals(i,3)*c_3); %compute PIVA
PIVA_Table(i,:) = table(t(i,:),tstart(i,:),tstop(i,:),pkVals(i,1), pkVals(i,2), pkVals(i,3),PIVA(i,1),...
'VariableNames',{'Time','Start','Stop', 'F1','F2','F3', 'PIVA'}); %store everything in a table
j = k;
i=i+1;
end
%writetable(PIVA_Table)
%plot(f,PY(10,1:window/2+1))
The code is meant to compute the fft on a moving window across a series of data points (typically many 10s of thousands and using .matlab data file imported from LabChart).
Originally, the for loop that iterates over the number of windows was 'for i = 1:lng-1', which worked as far as I could tell. However, I've found that if a data series is less than twice the size of the window (8192*2 data points), the script does nothing. When there is only 1 multiple of 8192 data points, lng = 1 so the for loop is now 'for i = 1:0', which implies to me that it should do nothing! My attempt to fix this is to change the loop to 'for i = 1:lng' so it should now resolve as 'for i = 1:1' and should loop 1 time, correct? This change results in the following error:
Index exceeds array bounds.
Error in FFT_script (line 46)
Y(i,:) = fft(rch1(j:k));

 Accepted Answer

William - does the error occur for the first iteration of the for loop (so when i is 1) or for some other iteration? What are the dimensions of rch1? The error messages is telling you that the indices referenced by j:k fall "outside" of the array. For example,
x = zeros(1,3);
x(4)
returns the same error message as you.
The problem may be with your k
k = j+window;
Note that j is one and window is 8192 so k will be 8193. Which, if your rch1 is of length 8192 (samples), then
fft(rch1(j:k));
will cause the above error. You probably want k to be set to
k = j + window - 1;
And on subsequent iterations of the loop, you probably want to set j to be
j = k + 1;

1 Comment

Ahah! I was barking up the wrong tree... I thought it was an error on the left hand side. Changing the indexing variables for the window size fixes the problem.
To answer your questions, however, the error occurs for some other iteration and rch1 has dimensions 1xn where n is the truncated data length (up to the greatest whole multiple of 8192 closest to the full data length).

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!