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));
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Signal Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!