File Exchange

## Easy clustering of a vector into groups

version 1.0 (3.18 KB) by

Automatic find peaks and associate each point to a peak in a vector

Updated

function [peaks,groups,criterion] = peaksandgroups(V,select,display)
% -------------------------------------------------------------
% Find peaks and link each data point to a peak, in effect clustering the data into groups
% ---------------------------------------------
% This function looks for peaks in the data using the LAZYCLIMB method.
% this algorithm.
%
% input :
% * V : data, a vector of length N
% * select : either:
% - select >1 : the number of peaks and groups to detect
% - 0<select<1 : the threshold to apply for finding peaks
% the closer to 1, the less groups, the closer to 0, the more groups
% * display : whether or not to display a figure for the results. 0 by
% default
% * ... and that's all ! that's the cool thing about the algorithm =)
%
% outputs :
% * peaks : indices of the peaks.
% * groups: the group each point is assigned to. Nx1 vector
% * criterion : the value of the computed criterion. Nx1 vector. A high
% value indicates a point which is likely to be a peak
%
% The algorithm goes as follows:
% 1°) Sort data in decreasing order, keep corresponding positions as order
% 2°) for all pos=1:n,
% * set order(pos) as the current point (from bigger to smaller)
% * look for the closest already processed points, at left and right
% * for each of them, compute a cost and select the one with lower cost
% as the father of current. The cost corresponds to the area of the
% difference between straight line and actual observed curve. The
% rationale is that if you've got a peak, it would be tiring to go from
% it to the place you were before, and this is measured as the
% difference with a straight line.
% 3°) This gives you a criterion for all point, as well as the father of
% each point
% 4°) Threshold the criterion so that you either got a given number of
% peaks, or so that you got few or many peaks
% 5°) Propagate the information to all points so that each point is given a
% group number
%
% I don't know if that kind of algorithm has already been published
% somewhere, I coded it myself and it works pretty nice, so.. enjoy !
% If you find it useful, please mention it in your studies =)
%
% running time IS PRETTY SLOW, due to the identification of the closest
% already processed points on left and right, and to the computation of the
% criterion. If you got some nice ways to have it go faster, please tell
% me.
% ---------------------------------------------------------------------
% (c) Antoine Liutkus, 2013
% ---------------------------------------------------------------------

Antoine Liutkus

### Antoine Liutkus (view profile)

interesting, could you please send me a mail with the data ? What is the length of your data

behruz

### behruz (view profile)

hi dear Antoine
the following error occured while running file by:
"peaksandgroups(c3,0.974,1);"
c3 is my 1D signal
the error is
"Undefined function or variable "closest_right"."
what is the cause of error?

Quirin Kellner

### Quirin Kellner (view profile)

Hi,
I'm quite a newbie to matlab.
Could you please let me know where I have to enter my input arguments?
I changed the name of Vector V to the vector that I wanted, but I am a bit confused about the remaining arguments.
Help would be much appreciated.
Thanks

xixor

### xixor (view profile)

A++++ would peak detect again

##### MATLAB Release
MATLAB 8.2 (R2013b)