comparison scripts/general/interp1.m @ 10820:c44c786f87ba

interp1.m: When absent set X equal to the inices of Y.
author Ben Abbott <bpabbott@mac.com>
date Mon, 26 Jul 2010 07:41:23 -0400
parents be55736a0783
children 693e22af08ae
comparison
equal deleted inserted replaced
10819:f3c984d45dcb 10820:c44c786f87ba
16 ## You should have received a copy of the GNU General Public License 16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see 17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>. 18 ## <http://www.gnu.org/licenses/>.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {@var{yi} =} interp1 (@var{x}, @var{y}, @var{xi}) 21 ## @deftypefn {Function File} {@var{yi} =} interp1 (@var{x}, @var{y}, @var{xi})
22 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@var{y}, @var{xi})
22 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{method}) 23 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{method})
23 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{extrap}) 24 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{extrap})
24 ## @deftypefnx {Function File} {@var{pp} =} interp1 (@dots{}, 'pp') 25 ## @deftypefnx {Function File} {@var{pp} =} interp1 (@dots{}, 'pp')
25 ## 26 ##
26 ## One-dimensional interpolation. Interpolate @var{y}, defined at the 27 ## One-dimensional interpolation. Interpolate @var{y}, defined at the
27 ## points @var{x}, at the points @var{xi}. The sample points @var{x} 28 ## points @var{x}, at the points @var{xi}. The sample points @var{x}
28 ## must be monotonic. If @var{y} is an array, treat the columns 29 ## must be monotonic. If not specified, @var{x} is taken to be the
30 ## indices of @var{y}. If @var{y} is an array, treat the columns
29 ## of @var{y} separately. 31 ## of @var{y} separately.
30 ## 32 ##
31 ## Method is one of: 33 ## Method is one of:
32 ## 34 ##
33 ## @table @asis 35 ## @table @asis
34 ## @item 'nearest' 36 ## @item 'nearest'
35 ## Return the nearest neighbor. 37 ## Return the nearest neighbor.
36 ## @item 'linear' 38 ## @item 'linear'
37 ## Linear interpolation from nearest neighbors 39 ## Linear interpolation from nearest neighbors
38 ## @item 'pchip' 40 ## @item 'pchip'
39 ## Piece-wise cubic Hermite interpolating polynomial 41 ## Piece-wise cubic hermite interpolating polynomial
40 ## @item 'cubic' 42 ## @item 'cubic'
41 ## Cubic interpolation from four nearest neighbors 43 ## Cubic interpolation from four nearest neighbors
42 ## @item 'spline' 44 ## @item 'spline'
43 ## Cubic spline interpolation--smooth first and second derivatives 45 ## Cubic spline interpolation--smooth first and second derivatives
44 ## throughout the curve 46 ## throughout the curve
97 ## 2002-01-23 Paul Kienzle 99 ## 2002-01-23 Paul Kienzle
98 ## fixed extrapolation 100 ## fixed extrapolation
99 101
100 function yi = interp1 (x, y, varargin) 102 function yi = interp1 (x, y, varargin)
101 103
102 if (nargin < 3 || nargin > 6) 104 if (nargin < 2 || nargin > 6)
103 print_usage (); 105 print_usage ();
104 endif 106 endif
105 107
106 method = "linear"; 108 method = "linear";
107 extrap = NA; 109 extrap = NA;
128 else 130 else
129 extrap = arg; 131 extrap = arg;
130 endif 132 endif
131 endif 133 endif
132 endfor 134 endfor
135 endif
136
137 if (isempty (xi) && firstnumeric && ! pp)
138 xi = y;
139 y = x;
140 x = 1:numel(y);
133 endif 141 endif
134 142
135 ## reshape matrices for convenience 143 ## reshape matrices for convenience
136 x = x(:); 144 x = x(:);
137 nx = rows (x); 145 nx = rows (x);
599 607
600 %!assert (interp1([3,2,1],[3,2,2],2.5),2.5) 608 %!assert (interp1([3,2,1],[3,2,2],2.5),2.5)
601 609
602 %!assert (interp1 ([1,2,2,3,4],[0,1,4,2,1],[-1,1.5,2,2.5,3.5], "linear", "extrap"), [-2,0.5,4,3,1.5]) 610 %!assert (interp1 ([1,2,2,3,4],[0,1,4,2,1],[-1,1.5,2,2.5,3.5], "linear", "extrap"), [-2,0.5,4,3,1.5])
603 %!assert (interp1 ([4,4,3,2,0],[0,1,4,2,1],[1.5,4,4.5], "linear"), [0,1,NA]) 611 %!assert (interp1 ([4,4,3,2,0],[0,1,4,2,1],[1.5,4,4.5], "linear"), [0,1,NA])
612 %!assert (interp1 (0:4, 2.5), 1.5)