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} =} interp3 (@var{x}, @var{y},@var{z}, @var{v}, @var{xi}, @var{yi}, @var{zi}) |
|
21 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@var{v}, @var{xi}, @var{yi}, @var{zi}) |
|
22 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@var{v}, @var{m}) |
|
23 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@var{v}) |
|
24 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@dots{}, @var{method}) |
|
25 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@dots{}, @var{method}, @var{extrapval}) |
|
26 ## |
|
27 ## Perform 3-dimensional interpolation. Each element of then 3-dimensional |
|
28 ## array @var{v} represents a value at a location given by the parameters |
|
29 ## @var{x}, @var{y}, and @var{z}. The parameters @var{x}, @var{x}, and |
|
30 ## @var{z} are either 3-dimensional arrays of the same size as the array |
|
31 ## @var{v} in the 'meshgrid' format or vectors. The parameters @var{xi}, etc |
|
32 ## respect a similar format to @var{x}, etc, and they represent the points |
|
33 ## at which the array @var{vi} is interpolated. |
|
34 ## |
7001
|
35 ## If @var{x}, @var{y}, @var{z} are omitted, they are assumed to be |
6702
|
36 ## @code{x = 1 : size (@var{v}, 2)}, @code{y = 1 : size (@var{v}, 1)} and |
|
37 ## @code{z = 1 : size (@var{v}, 3)}. 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, meshgrid} |
|
62 ## @end deftypefn |
|
63 |
|
64 function vi = interp3 (varargin) |
|
65 method = "linear"; |
6742
|
66 extrapval = NA; |
6702
|
67 nargs = nargin; |
|
68 |
|
69 if (nargin < 1) |
|
70 print_usage (); |
|
71 endif |
|
72 |
|
73 if (ischar (varargin {end})) |
|
74 method = varargin {end}; |
|
75 nargs = nargs - 1; |
|
76 elseif (ischar (varargin {end - 1})) |
6722
|
77 if (! isnumeric (varargin {end}) || ! isscalar (varargin {end})) |
6702
|
78 error ("extrapal is expected to be a numeric scalar"); |
|
79 endif |
6722
|
80 extrapval = varargin {end}; |
6702
|
81 method = varargin {end - 1}; |
|
82 nargs = nargs - 2; |
|
83 endif |
|
84 |
|
85 if (nargs < 3 || (nargs == 4 && ! isvector (varargin {1}) && |
|
86 nargs == (ndims (varargin {1}) + 1))) |
|
87 if (ndims (v) != 3) |
|
88 error ("expect 3-dimensional array of values"); |
|
89 endif |
6722
|
90 x = varargin (2:4); |
|
91 if (any (! cellfun (@isvector, x))) |
|
92 for i = 2 : 3 |
|
93 if (! size_equal (x{1}, x{i}) || ! size_equal (x{i}, v)) |
|
94 error ("dimensional mismatch"); |
|
95 endif |
|
96 x{i} = permute (x{i}, [2, 1, 3]); |
|
97 endfor |
|
98 x{1} = permute (x{1}, [2, 1, 3]); |
|
99 endif |
|
100 v = permute (v, [2, 1, 3]); |
|
101 vi = ipermute (interpn (v, x{:}, method, extrapval), [2, 1, 3]); |
6702
|
102 elseif (nargs == 7 && nargs == (2 * ndims (varargin {ceil (nargs / 2)})) + 1) |
|
103 v = varargin {4}; |
|
104 if (ndims (v) != 3) |
|
105 error ("expect 3-dimensional array of values"); |
|
106 endif |
6722
|
107 x = varargin (1:3); |
|
108 if (any (! cellfun (@isvector, x))) |
|
109 for i = 2 : 3 |
|
110 if (! size_equal (x{1}, x{i}) || ! size_equal (x{i}, v)) |
|
111 error ("dimensional mismatch"); |
|
112 endif |
|
113 x{i} = permute (x{i}, [2, 1, 3]); |
|
114 endfor |
|
115 x{1} = permute (x{1}, [2, 1, 3]); |
|
116 endif |
|
117 y = varargin (5:7); |
|
118 if (any (! cellfun (@isvector, y))) |
|
119 for i = 2 : 3 |
|
120 if (! size_equal (y{1}, y{i})) |
|
121 error ("dimensional mismatch"); |
|
122 endif |
|
123 y{i} = permute (y{i}, [2, 1, 3]); |
|
124 endfor |
|
125 y{1} = permute (y{1}, [2, 1, 3]); |
|
126 endif |
|
127 v = permute (v, [2, 1, 3]); |
|
128 vi = ipermute (interpn (x{:}, v, y{:}, method, extrapval), [2,1,3]); |
6702
|
129 else |
|
130 error ("wrong number or incorrectly formatted input arguments"); |
|
131 endif |
|
132 endfunction |
|
133 |
|
134 %!test |
|
135 %! x = y = z = -1:1; |
|
136 %! f = @(x,y,z) x.^2 - y - z.^2; |
|
137 %! [xx, yy, zz] = meshgrid (x, y, z); |
|
138 %! v = f (xx,yy,zz); |
|
139 %! xi = yi = zi = -1:0.5:1; |
|
140 %! [xxi, yyi, zzi] = meshgrid (xi, yi, zi); |
|
141 %! vi = interp3(x, y, z, v, xxi, yyi, zzi); |
|
142 %! [xxi, yyi, zzi] = ndgrid (xi, yi, zi); |
|
143 %! vi2 = interpn(x, y, z, v, xxi, yyi, zzi); |
|
144 %! assert (vi, vi2); |