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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {@var{vi} =} interp3 (@var{x}, @var{y},@var{z}, @var{v}, @var{xi}, @var{yi}, @var{zi}) |
|
22 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@var{v}, @var{xi}, @var{yi}, @var{zi}) |
|
23 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@var{v}, @var{m}) |
|
24 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@var{v}) |
|
25 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@dots{}, @var{method}) |
|
26 ## @deftypefnx {Function File} {@var{vi} =} interp3 (@dots{}, @var{method}, @var{extrapval}) |
|
27 ## |
|
28 ## Perform 3-dimensional interpolation. Each element of then 3-dimensional |
|
29 ## array @var{v} represents a value at a location given by the parameters |
|
30 ## @var{x}, @var{y}, and @var{z}. The parameters @var{x}, @var{x}, and |
|
31 ## @var{z} are either 3-dimensional arrays of the same size as the array |
|
32 ## @var{v} in the 'meshgrid' format or vectors. The parameters @var{xi}, etc |
|
33 ## respect a similar format to @var{x}, etc, and they represent the points |
|
34 ## at which the array @var{vi} is interpolated. |
|
35 ## |
|
36 ## If @var{x}, @var{y}, @var{z} are ommitted, they are assumed to be |
|
37 ## @code{x = 1 : size (@var{v}, 2)}, @code{y = 1 : size (@var{v}, 1)} and |
|
38 ## @code{z = 1 : size (@var{v}, 3)}. If @var{m} is specified, then |
|
39 ## the interpolation adds a point half way between each of the interplation |
|
40 ## points. This process is performed @var{m} times. If only @var{v} is |
|
41 ## specified, then @var{m} is assumed to be @code{1}. |
|
42 ## |
|
43 ## Method is one of: |
|
44 ## |
|
45 ## @table @asis |
|
46 ## @item 'nearest' |
|
47 ## Return the nearest neighbour. |
|
48 ## @item 'linear' |
|
49 ## Linear interpolation from nearest neighbours. |
|
50 ## @item 'cubic' |
|
51 ## Cubic interpolation from four nearest neighbours (not implemented yet). |
|
52 ## @item 'spline' |
|
53 ## Cubic spline interpolation--smooth first and second derivatives |
|
54 ## throughout the curve. |
|
55 ## @end table |
|
56 ## |
|
57 ## The default method is 'linear'. |
|
58 ## |
|
59 ## If @var{extrap} is the string 'extrap', then extrapolate values beyond |
|
60 ## the endpoints. If @var{extrap} is a number, replace values beyond the |
|
61 ## endpoints with that number. If @var{extrap} is missing, assume NaN. |
|
62 ## @seealso{interp1, interp2, spline, meshgrid} |
|
63 ## @end deftypefn |
|
64 |
|
65 function vi = interp3 (varargin) |
|
66 method = "linear"; |
|
67 extrapval = NaN; |
|
68 nargs = nargin; |
|
69 |
|
70 if (nargin < 1) |
|
71 print_usage (); |
|
72 endif |
|
73 |
|
74 if (ischar (varargin {end})) |
|
75 method = varargin {end}; |
|
76 nargs = nargs - 1; |
|
77 elseif (ischar (varargin {end - 1})) |
6722
|
78 if (! isnumeric (varargin {end}) || ! isscalar (varargin {end})) |
6702
|
79 error ("extrapal is expected to be a numeric scalar"); |
|
80 endif |
6722
|
81 extrapval = varargin {end}; |
6702
|
82 method = varargin {end - 1}; |
|
83 nargs = nargs - 2; |
|
84 endif |
|
85 |
|
86 if (nargs < 3 || (nargs == 4 && ! isvector (varargin {1}) && |
|
87 nargs == (ndims (varargin {1}) + 1))) |
|
88 if (ndims (v) != 3) |
|
89 error ("expect 3-dimensional array of values"); |
|
90 endif |
6722
|
91 x = varargin (2:4); |
|
92 if (any (! cellfun (@isvector, x))) |
|
93 for i = 2 : 3 |
|
94 if (! size_equal (x{1}, x{i}) || ! size_equal (x{i}, v)) |
|
95 error ("dimensional mismatch"); |
|
96 endif |
|
97 x{i} = permute (x{i}, [2, 1, 3]); |
|
98 endfor |
|
99 x{1} = permute (x{1}, [2, 1, 3]); |
|
100 endif |
|
101 v = permute (v, [2, 1, 3]); |
|
102 vi = ipermute (interpn (v, x{:}, method, extrapval), [2, 1, 3]); |
6702
|
103 elseif (nargs == 7 && nargs == (2 * ndims (varargin {ceil (nargs / 2)})) + 1) |
|
104 v = varargin {4}; |
|
105 if (ndims (v) != 3) |
|
106 error ("expect 3-dimensional array of values"); |
|
107 endif |
6722
|
108 x = varargin (1:3); |
|
109 if (any (! cellfun (@isvector, x))) |
|
110 for i = 2 : 3 |
|
111 if (! size_equal (x{1}, x{i}) || ! size_equal (x{i}, v)) |
|
112 error ("dimensional mismatch"); |
|
113 endif |
|
114 x{i} = permute (x{i}, [2, 1, 3]); |
|
115 endfor |
|
116 x{1} = permute (x{1}, [2, 1, 3]); |
|
117 endif |
|
118 y = varargin (5:7); |
|
119 if (any (! cellfun (@isvector, y))) |
|
120 for i = 2 : 3 |
|
121 if (! size_equal (y{1}, y{i})) |
|
122 error ("dimensional mismatch"); |
|
123 endif |
|
124 y{i} = permute (y{i}, [2, 1, 3]); |
|
125 endfor |
|
126 y{1} = permute (y{1}, [2, 1, 3]); |
|
127 endif |
|
128 v = permute (v, [2, 1, 3]); |
|
129 vi = ipermute (interpn (x{:}, v, y{:}, method, extrapval), [2,1,3]); |
6702
|
130 else |
|
131 error ("wrong number or incorrectly formatted input arguments"); |
|
132 endif |
|
133 endfunction |
|
134 |
|
135 %!test |
|
136 %! x = y = z = -1:1; |
|
137 %! f = @(x,y,z) x.^2 - y - z.^2; |
|
138 %! [xx, yy, zz] = meshgrid (x, y, z); |
|
139 %! v = f (xx,yy,zz); |
|
140 %! xi = yi = zi = -1:0.5:1; |
|
141 %! [xxi, yyi, zzi] = meshgrid (xi, yi, zi); |
|
142 %! vi = interp3(x, y, z, v, xxi, yyi, zzi); |
|
143 %! [xxi, yyi, zzi] = ndgrid (xi, yi, zi); |
|
144 %! vi2 = interpn(x, y, z, v, xxi, yyi, zzi); |
|
145 %! assert (vi, vi2); |