7785
|
1 ## Copyright (C) 2008 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 3 of the License, or (at |
|
8 ## your option) 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, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{yi} =} interp1q (@var{x}, @var{y}, @var{xi}) |
|
21 ## One-dimensional linear interpolation without error checking. |
|
22 ## Interpolates @var{y}, defined at the points @var{x}, at the points |
|
23 ## @var{xi}. The sample points @var{x} must be a strictly monotonically |
|
24 ## increasing column vector. If @var{y} is an array, treat the columns |
|
25 ## of @var{y} separately. If @var{y} is a vector, it must be a column |
|
26 ## vector of the same length as @var{x}. |
|
27 ## |
|
28 ## Values of @var{xi} beyond the endpoints of the interpolation result |
|
29 ## in NA being returned. |
|
30 ## |
|
31 ## Note that the error checking is only a significant portion of the |
|
32 ## execution time of this @code{interp1} if the size of the input arguments |
|
33 ## is relatively small. Therefore, the benefit of using @code{interp1q} |
|
34 ## is relatively small. |
|
35 ## @seealso{interp1} |
|
36 ## @end deftypefn |
|
37 |
|
38 function yi = interp1q (x, y, xi) |
|
39 x = x(:); |
|
40 nx = size (x, 1); |
|
41 szy = size (y); |
|
42 ny = szy (1); |
|
43 nc = prod (szy (2 : end)); |
|
44 y = reshape (y, ny, nc); |
|
45 szx = size (xi); |
|
46 xi = xi (:); |
|
47 range = find (xi >= x (1) & xi <= x (nx)); |
|
48 yi = NA (size(xi, 1), size (y, 2)); |
|
49 xi = xi (range); |
|
50 dy = y (2 : ny, :) - y (1 : ny - 1, :); |
|
51 dx = x (2 : nx) - x (1 : nx - 1); |
|
52 idx = lookup (x, xi, "lr"); |
|
53 s = (xi - x (idx)) ./ dx (idx); |
|
54 yi (range, :) = s (:, ones (1, nc)) .* dy (idx, :) + y (idx, :); |
|
55 if (length (szx) == 2 && any (szx == 1)) |
|
56 yi = reshape (yi, [max (szx), szy (2 : end)]); |
|
57 else |
|
58 yi = reshape (yi, [szx, szy(2:end)]); |
|
59 endif |
|
60 endfunction |
|
61 |
|
62 %!shared xp, yp, xi |
|
63 %! xp=[0:2:10].'; yp = sin(2*pi*xp/5); |
|
64 %! xi = [-1; 0; 2.2; 4; 6.6; 10; 11]; |
|
65 %!assert (interp1(xp, yp, [min(xp)-1; max(xp)+1]), [NA; NA]); |
|
66 %!assert (interp1(xp,yp,xp), yp, 100*eps); |
|
67 %!assert (isempty(interp1(xp,yp,[]))); |
|
68 %!assert (interp1(xp,[yp,yp],xi), [interp1(xp,yp,xi),interp1(xp,yp,xi)]); |
|
69 %!assert (interp1(xp,yp,[xi,xi]), [interp1(xp,yp,xi),interp1(xp,yp,xi)]); |
|
70 %!assert (interp1(xp,[yp,yp],[xi,xi]), [interp1(xp,yp,xi),interp1(xp,yp,xi)]); |