-
Notifications
You must be signed in to change notification settings - Fork 3
/
sli2.m
246 lines (209 loc) · 7.19 KB
/
sli2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
function z_sli2 = sli2(sx, sy, x, y)
%SLI2 Spline-based Least-squares integration.
% D * Z = G (G is mainly composed by spline estimated values).
%
% Reference:
% L. Huang, J. Xue, B. Gao, C. Zuo, and M. Idir, "Spline based least
% squares integration for two-dimensional shape or wavefront
% reconstruction," Optics and Lasers in Engineering 91, 221-226 (2017)
% Copyright since 2016 by Lei Huang. All Rights Reserved.
% E-mail: [email protected]
% 2016-09-29 Original Version
% 2016-11-01 Revised for x and y increasing directions.
% Check the number of arguments............................................
% Validate number of input arguments.
narginchk(4,4);
% Validate number of output arguments.
nargoutchk(1,1);
% Generate Matrix D and G..................................................
% Calculate size and ValidMask.
[Ny, Nx] = size(sx);
ValidMask = isfinite(sx) & isfinite(sy);
% Expand sy and y.
sy = [sy;NaN(1,Nx)];
y = [y ;NaN(1,Nx)];
% Compose matrices Dx and Dy.
ee = ones(Ny*Nx,1);
Dx = spdiags([-ee,ee],[0,Ny],Ny*(Nx-1),Ny*Nx);
Dy = spdiags([-ee,ee],[0,1],Ny*Nx,Ny*Nx);
% Compose matrices Gx and Gy.
Gx = (sx(:,1:end-1)+sx(:,2:end)).*(x(:,2:end)-x(:,1:end-1))/2;
Gy = (sy(1:end-1,:)+sy(2:end,:)).*(y(2:end,:)-y(1:end-1,:))/2;
% Compose D.
D = [Dx(isfinite(Gx),:); Dy(isfinite(Gy),:)];
clear Dx Dy;
% Compose matrix SpGx.
spGx = ComposeSpGx(x,sx,ValidMask,Nx,Ny);
% Compose matrix SpGy.
spGy = ComposeSpGy(y,sy,ValidMask,Nx,Ny);
clear sx sy x y;
% Replace with spline values, if available.
Gy(end,:)=[];
Gx(isfinite(spGx)) = spGx(isfinite(spGx));
Gy(isfinite(spGy)) = spGy(isfinite(spGy));
% Compose G.
G = [Gx(isfinite(Gx)); Gy(isfinite(Gy))];
clear Gx Gy;
% Solve "Warning: Rank deficient" for complete data by assuming Z(Ind)=0.
Ind = find(D(1,:)==-1,1);
D(:,Ind) = [];
Z = D\G;
Z = [Z(1:Ind-1);0;Z(Ind:end)];
% Reconstructed result.
z_sli2 = reshape(Z,Ny,Nx);
z_sli2(~ValidMask)= nan;
end
%% Subfunctions.
% Compose matrix spGx
function SpGx = ComposeSpGx(x,sx,ValidMask,Nx,Ny)
SpGx = NaN(Ny,Nx-1);
for ny = 1:Ny
xl = x(ny,:)';
vl = sx(ny,:)';
% Check the number of sections.
ml = ValidMask(ny,:)';
[Ns, Indices] = CheckSection(ml, Nx);
% Spline fitting section by section.
gs = cell(Ns,1);
for ns = 1:Ns
idx = Indices{ns};
xx = xl(idx);
vv = vl(idx);
if length(xx)>1
pp = spline(xx,vv); % "not-a-knot end condition"
c = pp.coefs;
switch(size(c,2))
case 4 % 4 points for piecewise cubic spline fitting.
dx = diff(xx);
if sign(mean(dx))==1
gs{ns} = dx.*(c(:,4) + dx.*(c(:,3)./2 + dx.*(c(:,2)./3 + dx.*c(:,1)./4)));
else
dx = -flipud(dx);
gs{ns} = dx.*(c(:,4) + dx.*(c(:,3)./2 + dx.*(c(:,2)./3 + dx.*c(:,1)./4)));
gs{ns} = -flipud(gs{ns});
end
case 3 % 3 points for 2nd order polynominal fitting.
% Here we do not use the polynomials.
% We are going to use the Southwell expression instead
gs{ns} = diff(xx)*NaN;
case 2 % 2 points for 1st order polynominal fitting.
% Here we do not use the polynomials.
% We are going to use the Southwell expression instead
gs{ns} = diff(xx)*NaN;
case 1
% Logically impossible.
error('Only one point for fitting in x direction!');
otherwise
% Logically impossible.
error('Unexpected number of points for fitting in x direction!');
end
end
end
sg = cat(1,gs{:});
Valid = ml(1:end-1) & ml(1+1:end);
pt = 1;
for nx = 1 : Nx-1
if Valid(nx) == 1
SpGx(ny, nx) = sg(pt);
pt = pt + 1;
end
end
end
end
% Compose matrix spGy
function SpGy = ComposeSpGy(y,sy,ValidMask,Nx,Ny)
SpGy = NaN(Ny-1,Nx);
for nx = 1:Nx
yl = y(:,nx);
vl = sy(:,nx);
% Check the number of sections.
ml = ValidMask(:,nx);
[Ns, Indices] = CheckSection(ml, Ny);
% Spline fitting section by section.
gs = cell(Ns,1);
for ns = 1:Ns
idx = Indices{ns};
yy = yl(idx);
vv = vl(idx);
if length(yy)>1
pp = spline(yy,vv); % "not-a-knot end condition"
c = pp.coefs;
switch(size(c,2))
case 4 % 4 points for piecewise cubic spline fitting.
dy = diff(yy);
if sign(mean(dy))==1
gs{ns} = dy.*(c(:,4) + dy.*(c(:,3)./2 + dy.*(c(:,2)./3 + dy.*c(:,1)./4)));
else
dy = -flipud(diff(yy));
gs{ns} = dy.*(c(:,4) + dy.*(c(:,3)./2 + dy.*(c(:,2)./3 + dy.*c(:,1)./4)));
gs{ns} = -flipud(gs{ns});
end
case 3 % 3 points
% Here we do not use the polynomials.
% We are going to use the Southwell expression instead
gs{ns} = diff(yy)*NaN;
case 2 % 2 points
% Here we do not use the polynomials.
% We are going to use the Southwell expression instead
gs{ns} = diff(yy)*NaN;
case 1
% Logically impossible.
error('Only one point for fitting in y direction!');
otherwise
% Logically impossible.
error('Unexpected number of points for fitting in y direction!');
end
end
end
sg = cat(1,gs{:});
Valid = ml(1:end-1) & ml(1+1:end);
pt = 1;
for ny = 1 : Ny-1
if Valid(ny) == 1
SpGy(ny, nx) = sg(pt);
pt = pt + 1;
end
end
end
end
% Check Sections.
function [Ns, Indices] = CheckSection(ml, N)
if all(ml)==true
Ns = 1;
Indices{Ns} = 1:N;
else
Indices = cell(N,1);
first = nan;
last = nan;
Ns = 0;
for n = 1:N
% Find the first.
if n==1
if ml(n)==true
first = n;
end
else
if ml(n)==true && ml(n-1)==false
first = n;
end
end
% Find the last.
if n==N
if ml(n)==true
last = n;
end
else
if ml(n)==true && ml(n+1)==false
last = n;
end
end
% Sum up the total number of sections and compose the Indices.
if isfinite(first) && isfinite(last)
Ns = Ns + 1;
Indices{Ns} = first:last;
first = nan;
last = nan;
end
end
end
end