Home > utility > localMax.m

localMax

PURPOSE ^

LOCALMAX Find local maxima of a vector

SYNOPSIS ^

function index = localmax(x, plotOpt)

DESCRIPTION ^

 LOCALMAX Find local maxima of a vector
    The output is a 0-1 index vector indicating where the local maxima are.
    Usage: index = localmax(x, plotOpt)

    For example:
        t = 0:0.2:2*pi;
        x = sin(t)+randn(size(t));
        index = localmax(x, 1);

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function index = localmax(x, plotOpt)
0002 % LOCALMAX Find local maxima of a vector
0003 %    The output is a 0-1 index vector indicating where the local maxima are.
0004 %    Usage: index = localmax(x, plotOpt)
0005 %
0006 %    For example:
0007 %        t = 0:0.2:2*pi;
0008 %        x = sin(t)+randn(size(t));
0009 %        index = localmax(x, 1);
0010 
0011 %    Roger Jang, 1999, 20071009
0012 
0013 if nargin<1, selfdemo; return; end
0014 if nargin<2, plotOpt=0; end
0015 
0016 b1 = x(2:end-1)>x(1:end-2);
0017 b2 = x(2:end-1)>x(3:end);
0018 index = 0*x;
0019 index(2:end-1) = b1 & b2;
0020 if x(1)>x(2), index(1)=1; end
0021 if x(end)>x(end-1), index(end)=1; end
0022 
0023 if plotOpt,
0024     time = 1:length(x);
0025     plot(time, x, time(logical(index)), x(logical(index)), 'ro');
0026 end
0027 
0028 % ====== Self demo
0029 function selfdemo
0030 t = 0:0.2:2*pi;
0031 x = sin(t)+randn(size(t));
0032 index = feval(mfilename, x, 1);

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003