forked from athurnherr/LDEO_IX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localmax2.m
70 lines (55 loc) · 1.67 KB
/
localmax2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function [xout,yout,imaxout] = localmax2(X,Y);
% LOCALMAX2 - find local maxima by parabola fitting
% [xm,ym]=LOCALMAX2(x,y) returns maxima for every triplet of points (along 1st dim).
% xm(i,j)=NaN means that there is no local maximum of y(:,j)=F{x(:,j)} between x(i,j) and x(i+2,j)
% x may be the same size as y or as y(:,1)
% added to find the three highest points of array
% August 2001, [email protected]
% April 2004, Visbeck [email protected]
% fix array sizes
if size(X,1)==size(Y,1) & size(X,2)==1
X=repmat(X,1,size(Y,2));
end
% find maximum
[dum,imax]=max(Y);
xout=imax+nan;
yout=imax+nan;
imaxout=imax+nan;
% select valid indices for fit
iok=find(imax>1 & imax<(size(Y,1)));
% restrict to valid values
imaxout(iok)=imax(iok);
imaxn=imax(iok);
if length(iok)>0
disp([' localmax2: found ',int2str(length(iok)),' valid values'])
else
disp([' localmax2 found no valid values'])
return
end
for i=1:length(imaxn)
j=iok(i);
x1(i)=X(imaxn(i)-1,j);
x2(i)=X(imaxn(i),j);
x3(i)=X(imaxn(i)+1,j);
y1(i)=Y(imaxn(i)-1,j);
y2(i)=Y(imaxn(i),j);
y3(i)=Y(imaxn(i)+1,j);
end
% compute fit to parabola
a=(x3.*y2+x1.*y3-x1.*y2-y3.*x2-y1.*x3+y1.*x2)./...
(x3.*x2.^2-x1.*x2.^2+x1.*x3.^2-x1.^2.*x3+x1.^2.*x2-x3.^2.*x2);
b=-(-x2.^2.*y3+x2.^2.*y1-y2.*x1.^2+y3.*x1.^2-x3.^2.*y1+y2.*x3.^2)./...
((-x3+x2).*(x2.*x3-x2.*x1+x1.^2-x3.*x1) );
c=(x2.^2.*y1.*x3-x2.^2.*x1.*y3-x3.^2.*y1.*x2+...
y3.*x1.^2.*x2+x3.^2.*x1.*y2-y2.*x1.^2.*x3)./...
((-x3+x2).*(x2.*x3-x2.*x1+x1.^2-x3.*x1));
% check for maximum in range
xn=x1+nan;
ii=find(a<0);
xn(ii)=-b(ii)./a(ii)/2;
% compute max taget strength if desired
xout(iok)=xn;
if nargout>1
yn=xn.^2.*a+xn.*b+c;
yout(iok)=yn;
end