Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

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.

Contact us