function beats = maxcenter(threshecg, w) % beats = maxcenter(ecg,1) % threshecg = the thresholded ecg % l = the half window to find max points in squared = threshecg > (mean(threshecg(find(threshecg)))/20); indicies = find([(diff([0 squared]) == 1)]); peaks = zeros(1,length(indicies)); l = length(threshecg); for(i = 1:length(indicies)) i1 = round(indicies(i) - w); i2 = round(indicies(i) + w); if(i1 < 1) i1 = 1; end if(i2 >= l) i2 = l - 1; end [a, b] = max(threshecg(i1:i2)); b = b + i1 - 1; peaks(i) = b; end nodups = find(peaks(1:(length(peaks)-1)) ~= peaks(2:length(peaks))); peaks = peaks(nodups); spacing = diff(peaks); errors = find(spacing < w); while(errors) i1 = errors(1); i2 = errors(1)+1; [trash,index] = min([threshecg(peaks(i1)) threshecg(peaks(i2))]); peaks(i1+index-1) = -50; peaks = peaks(find(peaks ~= -50)); spacing = diff(peaks); errors = find(spacing < w); end if(length(indicies)~=0) c = find([diff(peaks) 1]); beats = peaks(c); else beats = []; end