Home > utility > localMaxWinged.m

localMaxWinged

PURPOSE ^

LOCALMAX2 Local maxima which are global maxima within [-wing, +wing]

SYNOPSIS ^

function [index1, index2]=localmax2(x, wing, plotOpt)

DESCRIPTION ^

LOCALMAX2 Local maxima which are global maxima within [-wing, +wing]
    Usage: [index1, index2]=localmax2(x, wing, plotOpt)
        index1: index of local maxima
        index2: index of local maxima that are global maxima within [-wing, +wing]
        x: input vector
        wing: [-wing, +wing] (inclusive) is the span for determining index2 (unit for wing is sample point)
        plotOpt: plot option

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [index1, index2]=localmax2(x, wing, plotOpt)
0002 %LOCALMAX2 Local maxima which are global maxima within [-wing, +wing]
0003 %    Usage: [index1, index2]=localmax2(x, wing, plotOpt)
0004 %        index1: index of local maxima
0005 %        index2: index of local maxima that are global maxima within [-wing, +wing]
0006 %        x: input vector
0007 %        wing: [-wing, +wing] (inclusive) is the span for determining index2 (unit for wing is sample point)
0008 %        plotOpt: plot option
0009 
0010 %    Roger Jang, 20020829
0011 
0012 if nargin<1, selfdemo; return; end
0013 if nargin<2, wing=10; end
0014 if nargin<3, plotOpt=0; end
0015 
0016 index1=find(localmax(x));
0017 
0018 % ====== Keep local maxima that are largest within [-wing, +wing]
0019 index2=[];
0020 for i=1:length(index1),
0021     from=max(1, index1(i)-wing);
0022     to=min(length(x), index1(i)+wing);
0023     if x(index1(i))==max(x(from:to)),
0024         index2=[index2, index1(i)];
0025     end
0026 end
0027 
0028 % ====== Plotting
0029 if plotOpt,
0030     time=1:length(x);
0031     plot(time, x, '.-');
0032     set(gca, 'xlim', [-inf inf]);
0033     axisLimit=axis;
0034     for i=1:length(index1),
0035         line(time(index1(i))*[1,1], axisLimit(3:4), 'color', 'g');
0036         line(time(index1(i)), x(index1(i)), 'marker', 'o', 'color', 'g', 'linestyle', 'none');
0037     end
0038     for i=1:length(index2),
0039         line(time(index2(i))*[1,1], axisLimit(3:4), 'color', 'r');
0040         line(time(index2(i)), x(index2(i)), 'marker', 'square', 'color', 'r', 'linestyle', 'none');
0041     end
0042     xlabel('Sample index');
0043 end
0044 
0045 % ====== self demo
0046 function selfdemo
0047 x=rand(1,50);
0048 wing=8;
0049 plotOpt=1;
0050 [index1, index2]=feval(mfilename, x, wing, plotOpt);
0051 title(sprintf('Green line: local max, Red line: local max which is global within [-%g, +%g]', wing, wing));

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