5837
|
1 ## Copyright (C) 2005 Hoxide Ma |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5837
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5837
|
18 |
|
19 ## -*- texinfo -*- |
6702
|
20 ## @deftypefn {Function File} {@var{zi}=} bicubic (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}, @var{extrapval}) |
5837
|
21 ## |
6653
|
22 ## Return a matrix @var{zi} corresponding to the bicubic |
5838
|
23 ## interpolations at @var{xi} and @var{yi} of the data supplied |
6702
|
24 ## as @var{x}, @var{y} and @var{z}. Points outside the grid are set |
|
25 ## to @var{extrapval} |
5837
|
26 ## |
6702
|
27 ## See @url{http://wiki.woodpecker.org.cn/moin/Octave/Bicubic} |
|
28 ## for further information. |
5838
|
29 ## @seealso{interp2} |
5837
|
30 ## @end deftypefn |
|
31 |
|
32 ## Bicubic interpolation method. |
|
33 ## Author: Hoxide Ma <hoxide_dirac@yahoo.com.cn> |
|
34 |
6702
|
35 function F = bicubic (X, Y, Z, XI, YI, extrapval, spline_alpha) |
5837
|
36 |
6702
|
37 if (nargin < 1 || nargin > 7) |
5837
|
38 print_usage (); |
|
39 endif |
|
40 |
6702
|
41 if (nargin == 7 && isscalar(spline_alpha)) |
5837
|
42 a = spline_alpha |
|
43 else |
|
44 a = 0.5; |
|
45 endif |
|
46 |
6702
|
47 if (nargin < 6) |
|
48 extrapval = NaN; |
|
49 endif |
|
50 |
5838
|
51 if (nargin <= 2) |
|
52 ## bicubic (Z) or bicubic (Z, 2) |
5837
|
53 if (nargin == 1) |
|
54 n = 1; |
|
55 else |
|
56 n = Y; |
|
57 endif |
5838
|
58 Z = X; |
5837
|
59 X = []; |
5838
|
60 [rz, cz] = size (Z); |
|
61 s = linspace (1, cz, (cz-1)*pow2(n)+1); |
|
62 t = linspace (1, rz, (rz-1)*pow2(n)+1); |
5837
|
63 elseif (nargin == 3) |
5838
|
64 if (! isvector (X) || ! isvector (Y)) |
5837
|
65 error ("XI and YI must be vector"); |
|
66 endif |
|
67 s = Y; |
|
68 t = Z; |
|
69 Z = X; |
5838
|
70 [rz, cz] = size (Z); |
5837
|
71 elseif (nargin == 5 || nargin == 6) |
|
72 [rz, cz] = size (Z) ; |
|
73 if (isvector (X) && isvector (Y)) |
|
74 if (rz != length (Y) || cz != length (X)) |
|
75 error ("length of X and Y must match the size of Z"); |
|
76 endif |
6157
|
77 elseif (size_equal (X, Y) && size_equal (X, Z)) |
5837
|
78 X = X(1,:); |
|
79 Y = Y(:,1); |
|
80 else |
|
81 error ("X, Y and Z must be martrices of same size"); |
|
82 endif |
|
83 |
|
84 ## mark values outside the lookup table |
|
85 xfirst_ind = find (XI < X(1)); |
|
86 xlast_ind = find (XI > X(cz)); |
|
87 yfirst_ind = find (YI < Y(1)); |
|
88 ylast_ind = find (YI > Y(rz)); |
|
89 ## set value outside the table preliminary to min max index |
|
90 XI(xfirst_ind) = X(1); |
|
91 XI(xlast_ind) = X(cz); |
|
92 YI(yfirst_ind) = Y(1); |
|
93 YI(ylast_ind) = Y(rz); |
|
94 |
|
95 |
5838
|
96 X = reshape (X, 1, cz); |
|
97 X(cz) *= 1 + sign (X(cz))*eps; |
5837
|
98 if (X(cz) == 0) |
|
99 X(cz) = eps; |
|
100 endif; |
5838
|
101 XI = reshape (XI, 1, length (XI)); |
|
102 [m, i] = sort ([X, XI]); |
|
103 o = cumsum (i <= cz); |
|
104 xidx = o(find (i > cz)); |
5837
|
105 |
5838
|
106 Y = reshape (Y, rz, 1); |
|
107 Y(rz) *= 1 + sign (Y(rz))*eps; |
5837
|
108 if (Y(rz) == 0) |
|
109 Y(rz) = eps; |
|
110 endif; |
5838
|
111 YI = reshape (YI, length (YI), 1); |
|
112 [m, i] = sort ([Y; YI]); |
|
113 o = cumsum (i <= rz); |
5837
|
114 yidx = o([find( i> rz)]); |
|
115 |
|
116 ## set s and t used follow codes |
|
117 s = xidx + ((XI .- X(xidx))./(X(xidx+1) .- X(xidx))); |
|
118 t = yidx + ((YI - Y(yidx))./(Y(yidx+1) - Y(yidx))); |
|
119 else |
|
120 print_usage (); |
|
121 endif |
|
122 |
|
123 if (rz < 3 || cz < 3) |
|
124 error ("Z at least a 3 by 3 matrices"); |
|
125 endif |
|
126 |
5838
|
127 inds = floor (s); |
|
128 d = find (s == cz); |
|
129 s = s - floor (s); |
5837
|
130 inds(d) = cz-1; |
|
131 s(d) = 1.0; |
|
132 |
|
133 d = []; |
5838
|
134 indt = floor (t); |
|
135 d = find (t == rz); |
|
136 t = t - floor (t); |
5837
|
137 indt(d) = rz-1; |
|
138 t(d) = 1.0; |
|
139 d = []; |
|
140 |
5838
|
141 p = zeros (size (Z) + 2); |
5837
|
142 p(2:rz+1,2:cz+1) = Z; |
|
143 p(1,:) = (6*(1-a))*p(2,:) -3*p(3,:) + (6*a-2)*p(4,:); |
|
144 p(rz+2,:) = (6*(1-a))*p(rz+1,:) -3*p(rz,:) + (6*a-2)*p(rz-1,:); |
|
145 p(:,1) = (6*(1-a))*p(:,2) -3*p(:,3) + (6*a-2)*p(:,4); |
|
146 p(:,cz+2) = (6*(1-a))*p(:,cz+1) -3*p(:,cz) + (6*a-2)*p(:,cz-1); |
|
147 |
|
148 ## calculte the C1(t) C2(t) C3(t) C4(t) and C1(s) C2(s) C3(s) C4(s) |
|
149 t2= t.*t; |
|
150 t3= t2.*t; |
|
151 |
|
152 ct0= -a .* t3 + (2 * a) .* t2 - a .* t ; # -a G0 |
|
153 ct1 = (2-a) .* t3 + (-3+a) .* t2 + 1 ; # F0 - a G1 |
|
154 ct2 = (a-2) .* t3 + (-2 *a + 3) .* t2 + a .* t ; # F1 + a G0 |
|
155 ct3 = a .* t3 - a .* t2; # a G1 |
|
156 t = [];t2=[]; t3=[]; |
|
157 |
|
158 s2= s.*s; |
|
159 s3= s2.*s; |
|
160 |
|
161 cs0= -a .* s3 + (2 * a) .* s2 - a .*s ; # -a G0 |
|
162 cs1 = (2-a) .* s3 + (-3 + a) .* s2 + 1 ; # F0 - a G1 |
|
163 cs2 = (a-2) .* s3 + (-2 *a + 3) .* s2 + a .*s ; # F1 + a G0 |
|
164 cs3 = a .* s3 - a .* s2; # a G1 |
|
165 s=[] ; s2 = []; s3 = []; |
|
166 |
|
167 cs0 = cs0([1,1,1,1],:); |
|
168 cs1 = cs1([1,1,1,1],:); |
|
169 cs2 = cs2([1,1,1,1],:); |
|
170 cs3 = cs3([1,1,1,1],:); |
|
171 |
5838
|
172 lent = length (ct0); |
|
173 lens = length (cs0); |
|
174 F = zeros (lent, lens); |
5837
|
175 |
|
176 for i = 1:lent |
|
177 it = indt(i); |
|
178 int = [it, it+1, it+2, it+3]; |
|
179 F(i,:) = [ct0(i),ct1(i),ct2(i),ct3(i)] * ... |
5838
|
180 (p(int,inds) .* cs0 + p(int,inds+1) .* cs1 + ... |
|
181 p(int,inds+2) .* cs2 + p(int,inds+3) .* cs3); |
5837
|
182 endfor |
|
183 |
6702
|
184 ## set points outside the table to extrapval |
5837
|
185 if (! (isempty (xfirst_ind) && isempty (xlast_ind))) |
6702
|
186 F(:, [xfirst_ind, xlast_ind]) = extrapval; |
5837
|
187 endif |
|
188 if (! (isempty (yfirst_ind) && isempty (ylast_ind))) |
6702
|
189 F([yfirst_ind; ylast_ind], :) = extrapval; |
5837
|
190 endif |
|
191 |
|
192 endfunction |
|
193 |
|
194 %!demo |
|
195 %! A=[13,-1,12;5,4,3;1,6,2]; |
|
196 %! x=[0,1,4]+10; y=[-10,-9,-8]; |
|
197 %! xi=linspace(min(x),max(x),17); |
6702
|
198 %! yi=linspace(min(y),max(y),26)'; |
5837
|
199 %! mesh(xi,yi,bicubic(x,y,A,xi,yi)); |
|
200 %! [x,y] = meshgrid(x,y); |
|
201 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |