comparison _misc/tools/.svn/text-base/localmaxmin.m.svn-base @ 8:b5b38998ef3b

added all that other stuff
author matthiasm
date Fri, 11 Apr 2014 15:54:25 +0100
parents
children
comparison
equal deleted inserted replaced
7:12abff5474c8 8:b5b38998ef3b
1 function [x,n]=localmaxmin(y,xn)
2 %LOCALMAXMIN(Y) Local Maxima and Minima.
3 % X = LOCALMAXMIN(Y) or LOCALMAXMIN(Y,'max') for vector Y returns a logical
4 % vector X the same size as Y such that Y(X) contains the local maxima in Y.
5 %
6 % N = LOCALMAXMIN(Y,'min') for vector Y returns a logical vector N the same
7 % size as Y such that Y(N) contains the local minima in Y.
8 %
9 % [X,N] = LOCALMAXMIN(Y) for vector Y returns logical vectors X and N such
10 % that Y(X) contains the local maxima and Y(N) contains the local minima.
11 %
12 % When Y is a matrix, outputs are logical array(s) the same size as Y, and
13 % the minima and/or maxima are computed down the rows of each column in Y.
14 %
15 % See also MAX, MIN.
16
17 % D.C. Hanselman, University of Maine, Orono, ME 04469
18 % MasteringMatlab@yahoo.com
19 % Mastering MATLAB 7
20 % 2006-03-08
21
22 mm=['min';'max'];
23 if nargin==1
24 xn='max';
25 elseif nargin~=2
26 error('localmaxmin:NotEnoughInputArguments',...
27 'One or Two Input Arguments Required.')
28 end
29 if ~ischar(xn) || isempty(strcmp(xn,mm))
30 error('localmaxmin:UnknownArgument','Unknown Second Input Argument.')
31 end
32 if ~isnumeric(y) || ~isreal(y)
33 error('localmaxmin:IncorrectDataType','Y Must be Real Valued.')
34 end
35 ysiz=size(y);
36 isrow=ysiz(1)==1;
37 if isrow % if row, convert to column for now
38 y=y';
39 ysiz(1)=ysiz(2);
40 ysiz(2)=1;
41 end
42 if ysiz(1)<4
43 error('localmaxmin:InsufficientData','Y Must Have at Least 4 Elements.')
44 end
45 x=false(ysiz);
46
47 sdiff=sign(diff(y));
48 zdiff=sdiff==0;
49 idz=find(sum(zdiff)); % columns where zero differences appear
50
51 if strcmp(xn,'min') || nargout==2 % get minima
52 x(2:end-1,:)=diff(sdiff)==2;
53 x(1,:)=sdiff(1,:)>0;
54 x(end,:)=sdiff(end,:)<0;
55 if any(zdiff)
56 ir=1:ysiz(1);
57 for k=idz % handle columns with zero differences
58 itmp=ir;
59 itmp(zdiff(:,k))=[];
60 if all(sdiff(:,k)==0) % entire column is flat
61 x(:,k)=true;
62 else
63 tmp=diff(sign(diff(y(itmp,k))))==2;
64 x(itmp(tmp)+1,k)=true;
65 if sdiff(1,k)==0
66 nr=find(sdiff(:,k)>0,1);
67 nf=find(sdiff(:,k)<0,1);
68 if ~(isempty(nr)||isempty(nf))
69 x(1,k)=nr<nf;
70 end
71 end
72 if sdiff(end,k)==0
73 nr=find(sdiff(:,k)>0,1,'last');
74 nf=find(sdiff(:,k)<0,1,'last');
75 if ~(isempty(nr)||isempty(nf))
76 x(end,k)=nr<nf;
77 end
78 end
79 end
80 end
81 end
82 if isrow
83 x=x.';
84 end
85 if nargout==2
86 n=x;
87 end
88 end
89 if strcmp(xn,'max') || nargout==2 % get maxima
90 x=false(ysiz);
91 x(2:end-1,:)=diff(sdiff)==-2;
92 x(1,:)=sdiff(1,:)<0;
93 x(end,:)=sdiff(end,:)>0;
94 if any(zdiff)
95 ir=1:ysiz(1);
96 for k=idz % handle columns with zero differences
97 itmp=ir;
98 itmp(zdiff(:,k))=[];
99 if all(sdiff(:,k)==0) % entire column is flat
100 x(:,k)=true;
101 else
102 tmp=diff(sign(diff(y(itmp,k))))==-2;
103 x(itmp(tmp)+1,k)=true;
104 if sdiff(1,k)==0
105 nr=find(sdiff(:,k)>0,1);
106 nf=find(sdiff(:,k)<0,1);
107 if ~(isempty(nr)||isempty(nf))
108 x(1,k)=nr>nf;
109 end
110 end
111 if sdiff(end,k)==0
112 nr=find(sdiff(:,k)>0,1,'last');
113 nf=find(sdiff(:,k)<0,1,'last');
114 if ~(isempty(nr)||isempty(nf))
115 x(end,k)=nr>nf;
116 end
117 end
118 end
119 end
120 end
121 if isrow
122 x=x';
123 end
124 end