Index exceeds array bounds despite a loop to prevent this?
Mostrar comentarios más antiguos
function [peak_dat_avg] = FindMuscStrength8chan_Cfs(wavedata,channel,stim_freq,stim_time,lat1,lat2)
artefact_dat = wavedata(:,9,:);
emg_dat = wavedata(:,channel,:);
nframes = size(wavedata,3);
npulse = single(stim_freq*stim_time);
emgpeak_dat = zeros(npulse,1,nframes);
peak_vals = zeros(npulse,1);
for k = 1:nframes
[~, peak_locs] = findpeaks(artefact_dat(:,:,k),'NPeaks',npulse,'MinPeakProminence',0.025,'MaxPeakWidth',5,'MinPeakDistance',700);
start_idx = round(peak_locs + lat1);
end_idx = round(peak_locs + lat2);
numb_peaks = numel(peak_locs);
for i = 1:numb_peaks
for n = 1:numb_peaks
if (start_idx(n) > 6000)
start_idx(n) = 6000;
end_idx(n) = 6000;
end
end
peak_vals(i) = peak2peak(emg_dat(start_idx(i):end_idx(i),:,k));
end
emgpeak_dat(:,:,k) = peak_vals;
end
peak_dat_avg = mean(nonzeros(emgpeak_dat,1));
end
This function is designed to extract a small window of EMG data after locating a stimulation artefact on channel 9 of the data. The issue comes on line 28 where the error 'Index in position 1 exceeds array bounds; Index can't exceed 6000' pops up. I understand this as when trying to select the window of emg_dat it is attempting to start from a sample higher than 6000. However, I tried to implement the if loop above to locate any index values greater than the range of the data and set them to the maximum. I would really appreciate help on fixing this issue
2 comentarios
dpb
el 3 de Jul. de 2024
W/o a sample of the data to work with, it's hard to tell what it is you're actually trying to operate on and return which isn't clearly defined here in your explanation.
However, in
for k = 1:nframes
[~, peak_locs] = findpeaks(artefact_dat(:,:,k),'NPeaks',npulse,'MinPeakProminence',0.025,'MaxPeakWidth',5,'MinPeakDistance',700);
start_idx = round(peak_locs + lat1);
end_idx = round(peak_locs + lat2);
numb_peaks = numel(peak_locs);
for i = 1:numb_peaks
for n = 1:numb_peaks
if (start_idx(n) > 6000)
start_idx(n) = 6000;
end_idx(n) = 6000;
end
end
peak_vals(i) = peak2peak(emg_dat(start_idx(i):end_idx(i),:,k));
you first set the entire value of the variables start_idx, end_ix then set an element of an array using index n, then refer to the i element of the array. None of those indexing expressions are consistent with each other and so you're reference to the indices in the selection statement is not using the bounded values you just tried to create.
It would appear that what you need to do instead is to ensure when you create start_idx, end_ix in each pass over the number of frames would be to ensure the end_idx array is no greater than the length of the signal you've passed into findpeaks.
I don't follow the purpose of having the doubly nested loop of i and n, it would seem only the i loop would be sufficient to handle each peak within each frame, but you would need to have the peak_vals array doubley dimensioned over nframesXmax(numbpeaks) to save each peak by frame or use a cell array to store the peak values by frame in an array for each frame.
for k = 1:nframes
trace=artefact_dat(:,:,k);
N=numel(trace);
[~, peak_locs] = findpeaks(trace,'NPeaks',npulse,'MinPeakProminence',0.025,'MaxPeakWidth',5,'MinPeakDistance',700);
start_idx = round(peak_locs + lat1);
end_idx = round(peak_locs + lat2);
end_idx=min(end_idx,N); % ensure don't run over end
...
The above assumes you wouldn't set the value of lat1 such that start_idx would be past the end of the trace, but bounding it similarly would ensure you wouldn't exceed the data length. But, it would appear that if you can't pull a full peak out of the trace, you might want to simply just skip over the last one in a trace in which case testing for index >N and using continue over the loop over number of peaks would just ignore it leaving you with one less artifact. Of course, if lat2 were too large, you could have the case that the starting location for a subsequent artifact/peak could be before the end of the previous; hopefully your screening with findpeaks is eliminating that potential problem.
Attach a data file and somebody is bound to come along and look at it thoroughly...
Alexander H
el 3 de Jul. de 2024
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Timing and presenting 2D and 3D stimuli en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!