annotate scripts/general/interp1.m @ 9754:4219e5cf773d

improve interp1 and pchip
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 22 Oct 2009 10:12:54 +0200
parents e9dc2ed2ec0f
children 9a1c4fe44af8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
1 ## Copyright (C) 2000, 2006, 2007, 2008, 2009 Paul Kienzle
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
2 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
4 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## your option) any later version.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
9 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
14 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
18
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{yi} =} interp1 (@var{x}, @var{y}, @var{xi})
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{method})
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
22 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{extrap})
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
23 ## @deftypefnx {Function File} {@var{pp} =} interp1 (@dots{}, 'pp')
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
24 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
25 ## One-dimensional interpolation. Interpolate @var{y}, defined at the
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
26 ## points @var{x}, at the points @var{xi}. The sample points @var{x}
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
27 ## must be strictly monotonic. If @var{y} is an array, treat the columns
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6742
diff changeset
28 ## of @var{y} separately.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
29 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
30 ## Method is one of:
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
31 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
32 ## @table @asis
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
33 ## @item 'nearest'
9070
e9dc2ed2ec0f Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
34 ## Return the nearest neighbor.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
35 ## @item 'linear'
9070
e9dc2ed2ec0f Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
36 ## Linear interpolation from nearest neighbors
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
37 ## @item 'pchip'
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
38 ## Piece-wise cubic hermite interpolating polynomial
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
39 ## @item 'cubic'
9070
e9dc2ed2ec0f Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
40 ## Cubic interpolation from four nearest neighbors
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
41 ## @item 'spline'
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
42 ## Cubic spline interpolation--smooth first and second derivatives
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
43 ## throughout the curve
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
44 ## @end table
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
45 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
46 ## Appending '*' to the start of the above method forces @code{interp1}
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
47 ## to assume that @var{x} is uniformly spaced, and only @code{@var{x}
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
48 ## (1)} and @code{@var{x} (2)} are referenced. This is usually faster,
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
49 ## and is never slower. The default method is 'linear'.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
50 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
51 ## If @var{extrap} is the string 'extrap', then extrapolate values beyond
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
52 ## the endpoints. If @var{extrap} is a number, replace values beyond the
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
53 ## endpoints with that number. If @var{extrap} is missing, assume NA.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
54 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
55 ## If the string argument 'pp' is specified, then @var{xi} should not be
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
56 ## supplied and @code{interp1} returns the piece-wise polynomial that
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
57 ## can later be used with @code{ppval} to evaluate the interpolation.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
58 ## There is an equivalence, such that @code{ppval (interp1 (@var{x},
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
59 ## @var{y}, @var{method}, 'pp'), @var{xi}) == interp1 (@var{x}, @var{y},
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
60 ## @var{xi}, @var{method}, 'extrap')}.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
61 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
62 ## An example of the use of @code{interp1} is
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
63 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
64 ## @example
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
65 ## @group
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
66 ## xf = [0:0.05:10];
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
67 ## yf = sin (2*pi*xf/5);
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
68 ## xp = [0:10];
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
69 ## yp = sin (2*pi*xp/5);
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
70 ## lin = interp1 (xp, yp, xf);
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
71 ## spl = interp1 (xp, yp, xf, "spline");
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
72 ## cub = interp1 (xp, yp, xf, "cubic");
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
73 ## near = interp1 (xp, yp, xf, "nearest");
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
74 ## plot (xf, yf, "r", xf, lin, "g", xf, spl, "b",
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
75 ## xf, cub, "c", xf, near, "m", xp, yp, "r*");
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7671
diff changeset
76 ## legend ("original", "linear", "spline", "cubic", "nearest")
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
77 ## @end group
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
78 ## @end example
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
79 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
80 ## @seealso{interpft}
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
81 ## @end deftypefn
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
82
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
83 ## Author: Paul Kienzle
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
84 ## Date: 2000-03-25
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
85 ## added 'nearest' as suggested by Kai Habel
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
86 ## 2000-07-17 Paul Kienzle
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
87 ## added '*' methods and matrix y
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
88 ## check for proper table lengths
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
89 ## 2002-01-23 Paul Kienzle
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
90 ## fixed extrapolation
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
91
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
92 function yi = interp1 (x, y, varargin)
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
93
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
94 if (nargin < 3 || nargin > 6)
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
95 print_usage ();
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
96 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
97
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
98 method = "linear";
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
99 extrap = NA;
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
100 xi = [];
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
101 pp = false;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
102 firstnumeric = true;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
103
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
104 if (nargin > 2)
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
105 for i = 1:length (varargin)
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
106 arg = varargin{i};
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
107 if (ischar (arg))
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
108 arg = tolower (arg);
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
109 if (strcmp ("extrap", arg))
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
110 extrap = "extrap";
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
111 elseif (strcmp ("pp", arg))
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
112 pp = true;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
113 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
114 method = arg;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
115 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
116 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
117 if (firstnumeric)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
118 xi = arg;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
119 firstnumeric = false;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
120 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
121 extrap = arg;
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
122 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
123 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
124 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
125 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
126
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
127 ## reshape matrices for convenience
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
128 x = x(:);
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
129 nx = rows (x);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
130 if (isvector (y))
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
131 y = y(:);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
132 endif
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
133 szy = size (y);
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
134 y = y(:,:);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
135 [ny, nc] = size (y);
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
136 szx = size (xi);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
137 xi = xi(:);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
138
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
139 ## determine sizes
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
140 if (nx < 2 || ny < 2)
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
141 error ("interp1: table too short");
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
142 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
143
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
144 ## check whether x is sorted; sort if not.
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
145 if (! issorted (x))
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
146 [x, p] = sort (x);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
147 y = y(p,:);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
148 endif
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
149
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
150 starmethod = method(1) == "*";
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
151
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
152 if (starmethod)
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
153 dx = x(2) - x(1);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
154 else
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
155 if (any (x(1:nx-1) == x(2:nx)))
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
156 error ("interp1: table must be strictly monotonic");
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
157 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
158 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
159
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
160 ## Proceed with interpolating by all methods.
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
161
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
162 switch (method)
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
163 case "nearest"
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
164 if (pp)
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
165 yi = mkpp ([x(1); (x(1:end-1)+x(2:end))/2; x(end)], y, szy(2:end));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
166 else
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
167 idx = lookup (0.5*(x(1:nx-1)+x(2:nx)), xi) + 1;
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
168 yi = y(idx,:);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
169 endif
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
170 case "*nearest"
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
171 if (pp)
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
172 yi = mkpp ([x(1); x(1)+[0.5:(ny-1)]'*dx; x(nx)], y, szy(2:end));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
173 else
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
174 idx = max (1, min (ny, floor((xi-x(1))/dx+1.5)));
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
175 yi = y(idx,:);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
176 endif
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
177 case "linear"
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
178 dy = diff (y);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
179 dx = diff (x);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
180 if (pp)
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
181 yi = mkpp (x, [dy./dx, y(1:end-1)], szy(2:end));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
182 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
183 ## find the interval containing the test point
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
184 idx = lookup (x, xi, "lr");
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
185 ## use the endpoints of the interval to define a line
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
186 s = (xi - x(idx))./dx(idx);
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
187 yi = bsxfun (@times, s, dy(idx,:)) + y(idx,:);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
188 endif
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
189 case "*linear"
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
190 dy = diff (y);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
191 if (pp)
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
192 yi = mkpp (x(1) + [0:ny-1]*dx, [dy./dx, y(1:end-1)], szy(2:end));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
193 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
194 ## find the interval containing the test point
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
195 t = (xi - x(1))/dx + 1;
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
196 idx = max (1, min (ny - 1, floor (t)));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
197
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
198 ## use the endpoints of the interval to define a line
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
199 s = t - idx;
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
200 yi = bsxfun (@times, s, dy(idx,:)) + y(idx,:);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
201 endif
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
202 case {"pchip", "*pchip"}
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
203 if (nx == 2 || starmethod)
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
204 x = linspace (x(1), x(nx), ny);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
205 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
206 ## Note that pchip's arguments are transposed relative to interp1
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
207 if (pp)
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
208 yi = pchip (x.', y.');
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
209 yi.d = szy(2:end);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
210 else
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
211 yi = pchip (x.', y.', xi.').';
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
212 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
213
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
214 case {"cubic", "*cubic"}
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
215 if (nx < 4 || ny < 4)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
216 error ("interp1: table too short");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
217 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
218
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
219 ## FIXME Is there a better way to treat pp return and *cubic
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
220 if (starmethod && ! pp)
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
221 ## From: Miloje Makivic
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
222 ## http://www.npac.syr.edu/projects/nasa/MILOJE/final/node36.html
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
223 t = (xi - x(1))/dx + 1;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
224 idx = max (min (floor (t), ny-2), 2);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
225 t = t - idx;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
226 t2 = t.*t;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
227 tp = 1 - 0.5*t;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
228 a = (1 - t2).*tp;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
229 b = (t2 + t).*tp;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
230 c = (t2 - t).*tp/3;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
231 d = (t2 - 1).*t/6;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
232 J = ones (1, nc);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
233
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
234 yi = a(:,J) .* y(idx,:) + b(:,J) .* y(idx+1,:) ...
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
235 + c(:,J) .* y(idx-1,:) + d(:,J) .* y(idx+2,:);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
236 else
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
237 if (starmethod)
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
238 x = linspace (x(1), x(nx), ny).';
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
239 nx = ny;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
240 endif
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
241
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
242 idx = lookup (x(2:nx-1), xi, "lr");
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
243
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
244 ## Construct cubic equations for each interval using divided
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
245 ## differences (computation of c and d don't use divided differences
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
246 ## but instead solve 2 equations for 2 unknowns). Perhaps
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
247 ## reformulating this as a lagrange polynomial would be more efficient.
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
248 i = 1:nx-3;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
249 J = ones (1, nc);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
250 dx = diff (x);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
251 dx2 = x(i+1).^2 - x(i).^2;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
252 dx3 = x(i+1).^3 - x(i).^3;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
253 a = diff (y, 3)./dx(i,J).^3/6;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
254 b = (diff (y(1:nx-1,:), 2)./dx(i,J).^2 - 6*a.*x(i+1,J))/2;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
255 c = (diff (y(1:nx-2,:), 1) - a.*dx3(:,J) - b.*dx2(:,J))./dx(i,J);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
256 d = y(i,:) - ((a.*x(i,J) + b).*x(i,J) + c).*x(i,J);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
257
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
258 if (pp)
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
259 xs = [x(1);x(3:nx-2)];
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
260 yi = mkpp ([x(1);x(3:nx-2);x(nx)],
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
261 [a(:), (b(:) + 3.*xs(:,J).*a(:)), ...
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
262 (c(:) + 2.*xs(:,J).*b(:) + 3.*xs(:,J)(:).^2.*a(:)), ...
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
263 (d(:) + xs(:,J).*c(:) + xs(:,J).^2.*b(:) + ...
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
264 xs(:,J).^3.*a(:))], szy(2:end));
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
265 else
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
266 yi = ((a(idx,:).*xi(:,J) + b(idx,:)).*xi(:,J) ...
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
267 + c(idx,:)).*xi(:,J) + d(idx,:);
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
268 endif
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
269 endif
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
270 case {"spline", "*spline"}
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
271 if (nx == 2 || starmethod)
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
272 x = linspace(x(1), x(nx), ny);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
273 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
274 ## Note that spline's arguments are transposed relative to interp1
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
275 if (pp)
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
276 yi = spline (x.', y.');
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
277 yi.d = szy(2:end);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
278 else
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
279 yi = spline (x.', y.', xi.').';
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
280 endif
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
281 otherwise
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
282 error ("interp1: invalid method '%s'", method);
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
283 endswitch
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
284
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
285 if (! pp)
9754
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
286 if (! ischar (extrap))
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
287 ## determine which values are out of range and set them to extrap,
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
288 ## unless extrap == "extrap".
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
289 minx = min (x(1), x(nx));
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
290 maxx = max (x(1), x(nx));
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
291
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
292 outliers = xi < minx | ! (xi <= maxx); # this catches even NaNs
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
293 yi(outliers, :) = extrap;
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
294 endif
4219e5cf773d improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents: 9070
diff changeset
295
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
296 if (! isvector (y) && length (szx) == 2 && (szx(1) == 1 || szx(2) == 1))
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
297 if (szx(1) == 1)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
298 yi = reshape (yi, [szx(2), szy(2:end)]);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
299 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
300 yi = reshape (yi, [szx(1), szy(2:end)]);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
301 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
302 else
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
303 yi = reshape (yi, [szx, szy(2:end)]);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
304 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
305 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
306
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
307 endfunction
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
308
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
309 %!demo
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
310 %! xf=0:0.05:10; yf = sin(2*pi*xf/5);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
311 %! xp=0:10; yp = sin(2*pi*xp/5);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
312 %! lin=interp1(xp,yp,xf,"linear");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
313 %! spl=interp1(xp,yp,xf,"spline");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
314 %! cub=interp1(xp,yp,xf,"pchip");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
315 %! near=interp1(xp,yp,xf,"nearest");
6702
b2391d403ed2 [project @ 2007-06-12 21:39:26 by dbateman]
dbateman
parents: 6374
diff changeset
316 %! plot(xf,yf,"r",xf,near,"g",xf,lin,"b",xf,cub,"c",xf,spl,"m",xp,yp,"r*");
b2391d403ed2 [project @ 2007-06-12 21:39:26 by dbateman]
dbateman
parents: 6374
diff changeset
317 %! legend ("original","nearest","linear","pchip","spline")
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
318 %! %--------------------------------------------------------
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
319 %! % confirm that interpolated function matches the original
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
320
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
321 %!demo
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
322 %! xf=0:0.05:10; yf = sin(2*pi*xf/5);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
323 %! xp=0:10; yp = sin(2*pi*xp/5);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
324 %! lin=interp1(xp,yp,xf,"*linear");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
325 %! spl=interp1(xp,yp,xf,"*spline");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
326 %! cub=interp1(xp,yp,xf,"*cubic");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
327 %! near=interp1(xp,yp,xf,"*nearest");
6702
b2391d403ed2 [project @ 2007-06-12 21:39:26 by dbateman]
dbateman
parents: 6374
diff changeset
328 %! plot(xf,yf,"r",xf,near,"g",xf,lin,"b",xf,cub,"c",xf,spl,"m",xp,yp,"r*");
b2391d403ed2 [project @ 2007-06-12 21:39:26 by dbateman]
dbateman
parents: 6374
diff changeset
329 %! legend ("*original","*nearest","*linear","*cubic","*spline")
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
330 %! %--------------------------------------------------------
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
331 %! % confirm that interpolated function matches the original
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
332
6721
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
333 %!demo
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
334 %! t = 0 : 0.3 : pi; dt = t(2)-t(1);
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
335 %! n = length (t); k = 100; dti = dt*n/k;
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
336 %! ti = t(1) + [0 : k-1]*dti;
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
337 %! y = sin (4*t + 0.3) .* cos (3*t - 0.1);
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
338 %! ddyc = diff(diff(interp1(t,y,ti,'cubic'))./dti)./dti;
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
339 %! ddys = diff(diff(interp1(t,y,ti,'spline'))./dti)./dti;
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
340 %! ddyp = diff(diff(interp1(t,y,ti,'pchip'))./dti)./dti;
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
341 %! plot (ti(2:end-1), ddyc,'g+',ti(2:end-1),ddys,'b*', ...
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
342 %! ti(2:end-1),ddyp,'c^');
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
343 %! legend('cubic','spline','pchip');
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
344 %! title("Second derivative of interpolated 'sin (4*t + 0.3) .* cos (3*t - 0.1)'");
01036667884a [project @ 2007-06-14 06:56:41 by dbateman]
dbateman
parents: 6702
diff changeset
345
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
346 ## For each type of interpolated test, confirm that the interpolated
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
347 ## value at the knots match the values at the knots. Points away
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
348 ## from the knots are requested, but only 'nearest' and 'linear'
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
349 ## confirm they are the correct values.
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
350
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
351 %!shared xp, yp, xi, style
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
352 %! xp=0:2:10; yp = sin(2*pi*xp/5);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
353 %! xi = [-1, 0, 2.2, 4, 6.6, 10, 11];
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
354
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
355
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
356 ## The following BLOCK/ENDBLOCK section is repeated for each style
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
357 ## nearest, linear, cubic, spline, pchip
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
358 ## The test for ppval of cubic has looser tolerance, but otherwise
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
359 ## the tests are identical.
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
360 ## Note that the block checks style and *style; if you add more tests
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
361 ## before to add them to both sections of each block. One test,
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
362 ## style vs. *style, occurs only in the first section.
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
363 ## There is an ENDBLOCKTEST after the final block
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
364 %!test style = "nearest";
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
365 ## BLOCK
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
366 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
367 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
368 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
369 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
370 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
371 %!assert (isempty(interp1(xp',yp',[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
372 %!assert (isempty(interp1(xp,yp,[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
373 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
374 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
375 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
376 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
377 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
378 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
379 %!error interp1(1,1,1, style);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
380 %!assert (interp1(xp,[yp',yp'],xi,style),
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
381 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
382 %!test style=['*',style];
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
383 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
384 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
385 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
386 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
387 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
388 %!assert (isempty(interp1(xp',yp',[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
389 %!assert (isempty(interp1(xp,yp,[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
390 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
391 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
392 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
393 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
394 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
395 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
396 %!error interp1(1,1,1, style);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
397 ## ENDBLOCK
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
398 %!test style='linear';
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
399 ## BLOCK
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
400 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
401 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
402 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
403 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
404 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
405 %!assert (isempty(interp1(xp',yp',[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
406 %!assert (isempty(interp1(xp,yp,[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
407 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
408 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
409 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
410 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
411 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
412 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
413 %!error interp1(1,1,1, style);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
414 %!assert (interp1(xp,[yp',yp'],xi,style),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
415 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
416 %!test style=['*',style];
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
417 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
418 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
419 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
420 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
421 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
422 %!assert (isempty(interp1(xp',yp',[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
423 %!assert (isempty(interp1(xp,yp,[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
424 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
425 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
426 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
427 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
428 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
429 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
430 %!error interp1(1,1,1, style);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
431 ## ENDBLOCK
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
432 %!test style='cubic';
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
433 ## BLOCK
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
434 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
435 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
436 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
437 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
438 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
439 %!assert (isempty(interp1(xp',yp',[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
440 %!assert (isempty(interp1(xp,yp,[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
441 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
442 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
443 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
444 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
445 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
446 %! interp1(xp,yp,xi,style,"extrap"),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
447 %!error interp1(1,1,1, style);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
448 %!assert (interp1(xp,[yp',yp'],xi,style),
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
449 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
450 %!test style=['*',style];
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
451 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
452 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
453 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
454 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
455 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
456 %!assert (isempty(interp1(xp',yp',[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
457 %!assert (isempty(interp1(xp,yp,[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
458 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
459 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
460 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
461 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
462 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
463 %! interp1(xp,yp,xi,style,"extrap"),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
464 %!error interp1(1,1,1, style);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
465 ## ENDBLOCK
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
466 %!test style='pchip';
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
467 ## BLOCK
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
468 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
469 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
470 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
471 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
472 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
473 %!assert (isempty(interp1(xp',yp',[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
474 %!assert (isempty(interp1(xp,yp,[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
475 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
476 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
477 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
478 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
479 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
480 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
481 %!error interp1(1,1,1, style);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
482 %!assert (interp1(xp,[yp',yp'],xi,style),
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
483 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
484 %!test style=['*',style];
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
485 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
486 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
487 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
488 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
489 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
490 %!assert (isempty(interp1(xp',yp',[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
491 %!assert (isempty(interp1(xp,yp,[],style)));
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
492 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
493 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
494 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
495 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
496 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
497 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
498 %!error interp1(1,1,1, style);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
499 ## ENDBLOCK
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
500 %!test style='spline';
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
501 ## BLOCK
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
502 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
503 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
504 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
505 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
506 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
507 %!assert (isempty(interp1(xp',yp',[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
508 %!assert (isempty(interp1(xp,yp,[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
509 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
510 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
511 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
512 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
513 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
514 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
515 %!error interp1(1,1,1, style);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
516 %!assert (interp1(xp,[yp',yp'],xi,style),
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
517 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
518 %!test style=['*',style];
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
519 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]);
6374
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
520 %!assert (interp1(xp,yp,xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
521 %!assert (interp1(xp,yp,xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
522 %!assert (interp1(xp',yp',xp',style), yp', 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
523 %!assert (interp1(xp',yp',xp,style), yp, 100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
524 %!assert (isempty(interp1(xp',yp',[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
525 %!assert (isempty(interp1(xp,yp,[],style)));
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
526 %!assert (interp1(xp,[yp',yp'],xi(:),style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
527 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
528 %!assert (interp1(xp,yp,xi,style),...
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
529 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
530 %!assert (ppval(interp1(xp,yp,style,"pp"),xi),
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
531 %! interp1(xp,yp,xi,style,"extrap"),10*eps);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
532 %!error interp1(1,1,1, style);
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
533 ## ENDBLOCK
419017274c1e [project @ 2007-03-01 15:57:50 by jwe]
jwe
parents: 6366
diff changeset
534 ## ENDBLOCKTEST
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
535
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
536 %!# test linear extrapolation
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
537 %!assert (interp1([1:5],[3:2:11],[0,6],"linear","extrap"), [1, 13], eps);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
538 %!assert (interp1(xp, yp, [-1, max(xp)+1],"linear",5), [5, 5]);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
539
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
540 %!error interp1
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
541 %!error interp1(1:2,1:2,1,"bogus")
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
542
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
543 %!assert (interp1(1:2,1:2,1.4,"nearest"),1);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
544 %!error interp1(1,1,1, "linear");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
545 %!assert (interp1(1:2,1:2,1.4,"linear"),1.4);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
546 %!error interp1(1:3,1:3,1, "cubic");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
547 %!assert (interp1(1:4,1:4,1.4,"cubic"),1.4);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
548 %!error interp1(1:2,1:2,1, "spline");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
549 %!assert (interp1(1:3,1:3,1.4,"spline"),1.4);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
550
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
551 %!error interp1(1,1,1, "*nearest");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
552 %!assert (interp1(1:2:4,1:2:4,1.4,"*nearest"),1);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
553 %!error interp1(1,1,1, "*linear");
6742
ebf96cc00ee9 [project @ 2007-06-18 16:27:52 by jwe]
jwe
parents: 6721
diff changeset
554 %!assert (interp1(1:2:4,1:2:4,[0,1,1.4,3,4],"*linear"),[NA,1,1.4,3,NA]);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
555 %!error interp1(1:3,1:3,1, "*cubic");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
556 %!assert (interp1(1:2:8,1:2:8,1.4,"*cubic"),1.4);
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
557 %!error interp1(1:2,1:2,1, "*spline");
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
558 %!assert (interp1(1:2:6,1:2:6,1.4,"*spline"),1.4);
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
559
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
560 %!assert (interp1([3,2,1],[3,2,2],2.5),2.5)