Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: peak detection
Date: Fri, 25 Jul 2008 10:46:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 64
Message-ID: <g6catb$7oe$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1216982763 7950 172.30.248.35 (25 Jul 2008 10:46:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 25 Jul 2008 10:46:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1338633
Xref: news.mathworks.com comp.soft-sys.matlab:481788



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