6702
|
1 ## Copyright (C) 2007 David Bateman |
|
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. |
6702
|
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/>. |
6702
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{vi} =} interpn (@var{x1}, @var{x2}, @dots{}, @var{v}, @var{y1}, @var{y2}, @dots{}) |
|
21 ## @deftypefnx {Function File} {@var{vi} =} interpn (@var{v}, @var{y1}, @var{y2}, @dots{}) |
|
22 ## @deftypefnx {Function File} {@var{vi} =} interpn (@var{v}, @var{m}) |
|
23 ## @deftypefnx {Function File} {@var{vi} =} interpn (@var{v}) |
|
24 ## @deftypefnx {Function File} {@var{vi} =} interpn (@dots{}, @var{method}) |
|
25 ## @deftypefnx {Function File} {@var{vi} =} interpn (@dots{}, @var{method}, @var{extrapval}) |
|
26 ## |
|
27 ## Perform @var{n}-dimensional interpolation, where @var{n} is at least two. |
|
28 ## Each element of then @var{n}-dimensional array @var{v} represents a value |
|
29 ## at a location given by the parameters @var{x1}, @var{x2}, @dots{}, @var{xn}. |
|
30 ## The parameters @var{x1}, @var{x2}, @dots{}, @var{xn} are either |
|
31 ## @var{n}-dimensional arrays of the same size as the array @var{v} in |
|
32 ## the 'ndgrid' format or vectors. The parameters @var{y1}, etc respect a |
|
33 ## similar format to @var{x1}, etc, and they represent the points at which |
|
34 ## the array @var{vi} is interpolated. |
|
35 ## |
7001
|
36 ## If @var{x1}, @dots{}, @var{xn} are omitted, they are assumed to be |
6702
|
37 ## @code{x1 = 1 : size (@var{v}, 1)}, etc. If @var{m} is specified, then |
7001
|
38 ## the interpolation adds a point half way between each of the interpolation |
6702
|
39 ## points. This process is performed @var{m} times. If only @var{v} is |
|
40 ## specified, then @var{m} is assumed to be @code{1}. |
|
41 ## |
|
42 ## Method is one of: |
|
43 ## |
|
44 ## @table @asis |
|
45 ## @item 'nearest' |
|
46 ## Return the nearest neighbour. |
|
47 ## @item 'linear' |
|
48 ## Linear interpolation from nearest neighbours. |
|
49 ## @item 'cubic' |
|
50 ## Cubic interpolation from four nearest neighbours (not implemented yet). |
|
51 ## @item 'spline' |
|
52 ## Cubic spline interpolation--smooth first and second derivatives |
|
53 ## throughout the curve. |
|
54 ## @end table |
|
55 ## |
|
56 ## The default method is 'linear'. |
|
57 ## |
|
58 ## If @var{extrap} is the string 'extrap', then extrapolate values beyond |
|
59 ## the endpoints. If @var{extrap} is a number, replace values beyond the |
6742
|
60 ## endpoints with that number. If @var{extrap} is missing, assume NA. |
6702
|
61 ## @seealso{interp1, interp2, spline, ndgrid} |
|
62 ## @end deftypefn |
|
63 |
|
64 function vi = interpn (varargin) |
|
65 |
|
66 method = "linear"; |
6742
|
67 extrapval = NA; |
6702
|
68 nargs = nargin; |
|
69 |
|
70 if (nargin < 1) |
|
71 print_usage (); |
|
72 endif |
|
73 |
7208
|
74 if (ischar (varargin{end})) |
|
75 method = varargin{end}; |
6702
|
76 nargs = nargs - 1; |
7208
|
77 elseif (ischar (varargin{end - 1})) |
|
78 if (! isnumeric (varargin{end}) || ! isscalar (varargin{end})) |
6702
|
79 error ("extrapal is expected to be a numeric scalar"); |
|
80 endif |
7208
|
81 method = varargin{end - 1}; |
6702
|
82 nargs = nargs - 2; |
|
83 endif |
|
84 |
|
85 if (nargs < 3) |
7208
|
86 v = varargin{1}; |
6702
|
87 m = 1; |
|
88 if (nargs == 2) |
7208
|
89 m = varargin{2}; |
6702
|
90 if (! isnumeric (m) || ! isscalar (m) || floor (m) != m) |
|
91 error ("m is expected to be a integer scalar"); |
|
92 endif |
|
93 endif |
|
94 sz = size (v); |
|
95 nd = ndims (v); |
|
96 x = cell (1, nd); |
|
97 y = cell (1, nd); |
|
98 for i = 1 : nd; |
|
99 x{i} = 1 : sz(i); |
|
100 y{i} = 1 : (1 / (2 ^ m)) : sz(i); |
|
101 endfor |
7208
|
102 elseif (! isvector (varargin{1}) && nargs == (ndims (varargin{1}) + 1)) |
|
103 v = varargin{1}; |
6702
|
104 sz = size (v); |
|
105 nd = ndims (v); |
|
106 x = cell (1, nd); |
|
107 y = varargin (2 : nargs); |
|
108 for i = 1 : nd; |
|
109 x{i} = 1 : sz(i); |
|
110 endfor |
|
111 elseif (rem (nargs, 2) == 1 && nargs == |
7208
|
112 (2 * ndims (varargin{ceil (nargs / 2)})) + 1) |
6702
|
113 nv = ceil (nargs / 2); |
7208
|
114 v = varargin{nv}; |
6702
|
115 sz = size (v); |
|
116 nd = ndims (v); |
|
117 x = varargin (1 : (nv - 1)); |
|
118 y = varargin ((nv + 1) : nargs); |
|
119 else |
|
120 error ("wrong number or incorrectly formatted input arguments"); |
|
121 endif |
|
122 |
|
123 if (any (! cellfun (@isvector, x))) |
|
124 for i = 2 : nd |
|
125 if (! size_equal (x{1}, x{i}) || ! size_equal (x{i}, v)) |
|
126 error ("dimensional mismatch"); |
|
127 endif |
|
128 idx (1 : nd) = {1}; |
|
129 idx (i) = ":"; |
6721
|
130 x{i} = x{i}(idx{:})(:); |
6702
|
131 endfor |
|
132 idx (1 : nd) = {1}; |
|
133 idx (1) = ":"; |
6721
|
134 x{1} = x{1}(idx{:})(:); |
6702
|
135 endif |
|
136 |
|
137 if (strcmp (method, "linear") || strcmp (method, "nearest")) |
|
138 if (all (cellfun (@isvector, y))) |
|
139 [y{:}] = ndgrid (y{:}); |
|
140 endif |
6721
|
141 elseif (any (! cellfun (@isvector, y))) |
6702
|
142 for i = 1 : nd |
|
143 idx (1 : nd) = {1}; |
|
144 idx (i) = ":"; |
6721
|
145 y{i} = y{i}(idx{:})(:).'; |
6702
|
146 endfor |
|
147 endif |
|
148 |
|
149 method = tolower (method); |
|
150 if (strcmp (method, "linear")) |
|
151 vi = __lin_interpn__ (x{:}, v, y{:}); |
6742
|
152 vi (isna (vi)) = extrapval; |
6702
|
153 elseif (strcmp (method, "nearest")) |
|
154 yshape = size (y{1}); |
|
155 yidx = cell (1, nd); |
|
156 for i = 1 : nd |
|
157 y{i} = y{i}(:); |
|
158 yidx{i} = lookup (x{i}(2:end-1), y{i}) + 1; |
|
159 endfor |
|
160 idx = cell (1,nd); |
|
161 for i = 1 : nd |
7208
|
162 idx{i} = yidx{i} + (y{i} - x{i}(yidx{i}).' > x{i}(yidx{i} + 1).' - y{i}); |
6702
|
163 endfor |
|
164 vi = v (sub2ind (sz, idx{:})); |
|
165 idx = zeros (prod(yshape),1); |
|
166 for i = 1 : nd |
|
167 idx |= y{i} < min (x{i}(:)) | y{i} > max (x{i}(:)); |
|
168 endfor |
|
169 vi(idx) = extrapval; |
|
170 vi = reshape (vi, yshape); |
6721
|
171 elseif (strcmp (method, "spline")) |
6702
|
172 vi = __splinen__ (x, v, y, extrapval, "interpn"); |
|
173 elseif (strcmp (method, "cubic")) |
|
174 error ("cubic interpolation not yet implemented"); |
|
175 else |
|
176 error ("unrecognized interpolation method"); |
|
177 endif |
|
178 |
|
179 endfunction |
|
180 |
|
181 %!demo |
|
182 %! A=[13,-1,12;5,4,3;1,6,2]; |
|
183 %! x=[0,1,4]; y=[10,11,12]; |
|
184 %! xi=linspace(min(x),max(x),17); |
|
185 %! yi=linspace(min(y),max(y),26)'; |
|
186 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"linear").'); |
|
187 %! [x,y] = meshgrid(x,y); |
|
188 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
|
189 |
|
190 %!demo |
|
191 %! A=[13,-1,12;5,4,3;1,6,2]; |
|
192 %! x=[0,1,4]; y=[10,11,12]; |
|
193 %! xi=linspace(min(x),max(x),17); |
|
194 %! yi=linspace(min(y),max(y),26)'; |
|
195 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"nearest").'); |
|
196 %! [x,y] = meshgrid(x,y); |
|
197 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
|
198 |
|
199 %!#demo |
|
200 %! A=[13,-1,12;5,4,3;1,6,2]; |
|
201 %! x=[0,1,2]; y=[10,11,12]; |
|
202 %! xi=linspace(min(x),max(x),17); |
|
203 %! yi=linspace(min(y),max(y),26)'; |
|
204 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"cubic").'); |
|
205 %! [x,y] = meshgrid(x,y); |
|
206 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
|
207 |
|
208 %!demo |
|
209 %! A=[13,-1,12;5,4,3;1,6,2]; |
|
210 %! x=[0,1,2]; y=[10,11,12]; |
|
211 %! xi=linspace(min(x),max(x),17); |
|
212 %! yi=linspace(min(y),max(y),26)'; |
|
213 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"spline").'); |
|
214 %! [x,y] = meshgrid(x,y); |
|
215 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
|
216 |
6721
|
217 |
|
218 %!demo |
|
219 %! x = y = z = -1:1; |
|
220 %! f = @(x,y,z) x.^2 - y - z.^2; |
|
221 %! [xx, yy, zz] = meshgrid (x, y, z); |
|
222 %! v = f (xx,yy,zz); |
|
223 %! xi = yi = zi = -1:0.1:1; |
|
224 %! [xxi, yyi, zzi] = ndgrid (xi, yi, zi); |
|
225 %! vi = interpn(x, y, z, v, xxi, yyi, zzi, 'spline'); |
|
226 %! mesh (yi, zi, squeeze (vi(1,:,:))); |
|
227 |