Thread Subject: peak detection

Subject: peak detection

From: Dave Brackett

Date: 25 Jul, 2008 10:46:03

Message: 1 of 3

I have been using a peak detection m file from
http://www.billauer.co.il/peakdet.html. The code is shown below.

Does anyone know if its possible to vectorise this function
as it currently uses for loops? and if it is, could you show
me how please? thanks. more info can be found at the above link.



function [maxtab, mintab]=peakdet(v, delta)
%PEAKDET Detect peaks in a vector
% [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local
% maxima and minima ("peaks") in the vector V.
% A point is considered a maximum peak if it has the
maximal
% value, and was preceded (to the left) by a value
lower by
% DELTA. MAXTAB and MINTAB consists of two columns.
Column 1
% contains indices in V, and column 2 the found values.

% Eli Billauer, 3.4.05 (Explicitly not copyrighted).
% This function is released to the public domain; Any use is
allowed.

maxtab = [];
mintab = [];

v = v(:); % Just in case this wasn't a proper vector

if (length(delta(:)))>1
  error('Input argument DELTA must be a scalar');
end

if delta <= 0
  error('Input argument DELTA must be positive');
end

mn = Inf; mx = -Inf;
mnpos = NaN; mxpos = NaN;

lookformax = 1;

for i=1:length(v)
  this = v(i);
  if this > mx, mx = this; mxpos = i; end
  if this < mn, mn = this; mnpos = i; end
  
  if lookformax
    if this < mx-delta
      maxtab = [maxtab ; mxpos mx];
      mn = this; mnpos = i;
      lookformax = 0;
    end
  else
    if this > mn+delta
      mintab = [mintab ; mnpos mn];
      mx = this; mxpos = i;
      lookformax = 1;
    end
  end
end


Subject: peak detection

From: Lothar Schmidt

Date: 25 Jul, 2008 11:53:29

Message: 2 of 3

Dave Brackett schrieb:
> I have been using a peak detection m file from
> http://www.billauer.co.il/peakdet.html. The code is shown below.
>
> Does anyone know if its possible to vectorise this function
> as it currently uses for loops? and if it is, could you show
> me how please? thanks. more info can be found at the above link.
>
>
>
> function [maxtab, mintab]=peakdet(v, delta)
> %PEAKDET Detect peaks in a vector
> % [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local
> % maxima and minima ("peaks") in the vector V.
> % A point is considered a maximum peak if it has the
> maximal
> % value, and was preceded (to the left) by a value
> lower by
> % DELTA. MAXTAB and MINTAB consists of two columns.
> Column 1
> % contains indices in V, and column 2 the found values.
>
> % Eli Billauer, 3.4.05 (Explicitly not copyrighted).
> % This function is released to the public domain; Any use is
> allowed.
>
> maxtab = [];
> mintab = [];
>
> v = v(:); % Just in case this wasn't a proper vector
>
> if (length(delta(:)))>1
> error('Input argument DELTA must be a scalar');
> end
>
> if delta <= 0
> error('Input argument DELTA must be positive');
> end
>
> mn = Inf; mx = -Inf;
> mnpos = NaN; mxpos = NaN;
>
> lookformax = 1;
>
> for i=1:length(v)
> this = v(i);
> if this > mx, mx = this; mxpos = i; end
> if this < mn, mn = this; mnpos = i; end
>
> if lookformax
> if this < mx-delta
> maxtab = [maxtab ; mxpos mx];
> mn = this; mnpos = i;
> lookformax = 0;
> end
> else
> if this > mn+delta
> mintab = [mintab ; mnpos mn];
> mx = this; mxpos = i;
> lookformax = 1;
> end
> end
> end
>
>

index=2:length(v)-1;
MAX_pos=find(v(i-1)+delta<v(i) & v(i+1)+delta<v(i))+1;

for finding the minima you'll shurely find a similar solution :-)

Lothar




Subject: peak detection

From: Dave Brackett

Date: 25 Jul, 2008 13:17:01

Message: 3 of 3

Lothar Schmidt <vapooroop@gmx.net> wrote in message
<g6cerv$uc0$01$1@news.t-online.com>...
> Dave Brackett schrieb:
> > I have been using a peak detection m file from
> > http://www.billauer.co.il/peakdet.html. The code is
shown below.
> >
> > Does anyone know if its possible to vectorise this function
> > as it currently uses for loops? and if it is, could you show
> > me how please? thanks. more info can be found at the
above link.
> >
> >
> >
> > function [maxtab, mintab]=peakdet(v, delta)
> > %PEAKDET Detect peaks in a vector
> > % [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the
local
> > % maxima and minima ("peaks") in the vector V.
> > % A point is considered a maximum peak if it has the
> > maximal
> > % value, and was preceded (to the left) by a value
> > lower by
> > % DELTA. MAXTAB and MINTAB consists of two columns.
> > Column 1
> > % contains indices in V, and column 2 the found
values.
> >
> > % Eli Billauer, 3.4.05 (Explicitly not copyrighted).
> > % This function is released to the public domain; Any use is
> > allowed.
> >
> > maxtab = [];
> > mintab = [];
> >
> > v = v(:); % Just in case this wasn't a proper vector
> >
> > if (length(delta(:)))>1
> > error('Input argument DELTA must be a scalar');
> > end
> >
> > if delta <= 0
> > error('Input argument DELTA must be positive');
> > end
> >
> > mn = Inf; mx = -Inf;
> > mnpos = NaN; mxpos = NaN;
> >
> > lookformax = 1;
> >
> > for i=1:length(v)
> > this = v(i);
> > if this > mx, mx = this; mxpos = i; end
> > if this < mn, mn = this; mnpos = i; end
> >
> > if lookformax
> > if this < mx-delta
> > maxtab = [maxtab ; mxpos mx];
> > mn = this; mnpos = i;
> > lookformax = 0;
> > end
> > else
> > if this > mn+delta
> > mintab = [mintab ; mnpos mn];
> > mx = this; mxpos = i;
> > lookformax = 1;
> > end
> > end
> > end
> >
> >
>
> index=2:length(v)-1;
> MAX_pos=find(v(i-1)+delta<v(i) & v(i+1)+delta<v(i))+1;
>
> for finding the minima you'll shurely find a similar
solution :-)
>
> Lothar
>
>

so those 2 lines do the same as most of that code? nice.

I am keen to get it to handle an arbitrary number of vectors
in matrix form as currently it can only handle single
vectors. As such I have been looping the whole function for
each vector which is slow. could you show me how to adapt
your vectorised version to allow this please? thanks.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
peak detection Dave Brackett 25 Jul, 2008 06:50:08
peakdetm Dave Brackett 25 Jul, 2008 06:50:08
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com