function [peaks,groups,criterion] = peaksandgroups(V,select,display) % ------------------------------------------------------------- % Find peaks and link each data point to a peak % --------------------------------------------- % This function looks for peaks in the data using the LAZYCLIMB method. % This won't help you much, since lazyclimb is a name I just made up for % this algorithm. % % input : % * V : data, a vector of length N % * select : either: % - select >1 : the number of peaks to detect % - 0current); if ~isempty(left) [~,closest_left] = min(abs(current-order(left))); closest_left = order(left(closest_left)); line = linspace(V(closest_left),V(current),current-closest_left+1)'; crit_left = sum(abs(line-V(closest_left:current))); else crit_left = inf; end if ~isempty(right) [~,closest_right] = min(abs(current-order(right))); closest_right = order(right(closest_right)); line = linspace(V(current),V(current),closest_right-current+1)'; crit_right = sum(abs(line-V(current:closest_right))); else crit_right=inf; end %Now sets the father as the one giving the smallest criterion if crit_leftmin(criterion(:)))),1000); HH = cumsum(HH); HH = HH/HH(end); p = 0.9+select*0.1; first = find(HH>p,1,'first'); peaks = find(log(criterion)>VV(first)); else [~,order] = sort(criterion,'descend'); peaks = order(1:select); end %Now propagate the group information from peaks to all points. Do a simple %dynamic programming stuf for this groups = zeros(n,1); groups(peaks) = 1:length(peaks); changed = 1; while changed changed=0; for pos = 1:n if ~groups(pos) groups(pos)= groups(fathers(pos)); changed=1; end end end %display if needed if display clf for igroup=1:length(peaks) hold on I = find(groups==igroup); plot(I,V(I),'Color',rand(3,1),'LineWidth',2) hold on plot(peaks(igroup),V(peaks(igroup)),'ro','MarkerSize',10,'LineWidth',2) end grid on title('Peaks and groups detection','FontSize',16); end