0001 function [index1, index2]=localmax2(x, wing, plotOpt)
0002
0003
0004
0005
0006
0007
0008
0009
0010
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
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
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
0046 function selfdemo
0047
0048 x=rand(1,50);
0049 wing=8;
0050 plotOpt=1;
0051 [index1, index2]=feval(mfilename, x, wing, plotOpt);
0052 title(sprintf('Yellow line: local max, Red line: local max which is global within [-%g, +%g] (seconds)', wing, wing));