22
22
## @deftypefnx {Function File} {@var{yi} =} interp1 (@var{y}, @var{xi})
23
23
## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{method})
24
24
## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{extrap})
25
## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, "left")
26
## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, "right")
25
27
## @deftypefnx {Function File} {@var{pp} =} interp1 (@dots{}, "pp")
27
## One-dimensional interpolation. Interpolates to determine the value of
28
## @var{yi} at the points, @var{xi}. If not specified, @var{x} is taken
29
## to be the indices of @var{y}. If @var{y} is a matrix or an N-dimensional
30
## array, the interpolation is performed on each column of @var{y}.
29
## One-dimensional interpolation.
31
## Interpolate input data to determine the value of @var{yi} at the points
32
## @var{xi}. If not specified, @var{x} is taken to be the indices of @var{y}.
33
## If @var{y} is a matrix or an N-dimensional array, the interpolation is
34
## performed on each column of @var{y}.
32
36
## Method is one of:
35
39
## @item @qcode{"nearest"}
36
## Return the nearest neighbor.
40
## Return the nearest neighbor
38
42
## @item @qcode{"linear"}
39
43
## Linear interpolation from nearest neighbors
49
53
## throughout the curve
52
## Appending '*' to the start of the above method forces @code{interp1}
56
## Adding '*' to the start of any method above forces @code{interp1}
53
57
## to assume that @var{x} is uniformly spaced, and only @code{@var{x}(1)}
54
58
## and @code{@var{x}(2)} are referenced. This is usually faster,
55
59
## and is never slower. The default method is @qcode{"linear"}.
57
61
## If @var{extrap} is the string @qcode{"extrap"}, then extrapolate values
58
## beyond the endpoints. If @var{extrap} is a number, replace values beyond
59
## the endpoints with that number. If @var{extrap} is missing, assume NA.
62
## beyond the endpoints using the current @var{method}. If @var{extrap} is a
63
## number, then replace values beyond the endpoints with that number. When
64
## unspecified, @var{extrap} defaults to NA.
61
## If the string argument @qcode{"pp"} is specified, then @var{xi} should not be
62
## supplied and @code{interp1} returns the piecewise polynomial that
63
## can later be used with @code{ppval} to evaluate the interpolation.
66
## If the string argument @qcode{"pp"} is specified, then @var{xi} should not
67
## be supplied and @code{interp1} returns a piecewise polynomial object. This
68
## object can later be used with @code{ppval} to evaluate the interpolation.
64
69
## There is an equivalence, such that @code{ppval (interp1 (@var{x},
65
70
## @var{y}, @var{method}, @qcode{"pp"}), @var{xi}) == interp1 (@var{x}, @var{y},
66
71
## @var{xi}, @var{method}, @qcode{"extrap"})}.
302
316
yi = shiftdim (yi, 1);
306
321
error ("interp1: invalid method '%s'", method);
310
if (! ischar (extrap))
311
## determine which values are out of range and set them to extrap,
312
## unless extrap == "extrap".
313
minx = min (x(1), x(nx));
314
maxx = max (x(1), x(nx));
325
if (! ispp && ! ischar (extrap))
326
## determine which values are out of range and set them to extrap,
327
## unless extrap == "extrap".
328
minx = min (x(1), x(nx));
329
maxx = max (x(1), x(nx));
316
outliers = xi < minx | ! (xi <= maxx); # this catches even NaNs
317
if (size_equal (outliers, yi))
318
yi(outliers) = extrap;
319
yi = reshape (yi, szx);
320
elseif (!isvector (yi))
321
yi(outliers, :) = extrap;
323
yi(outliers.') = extrap;
331
outliers = xi < minx | ! (xi <= maxx); # this even catches NaNs
332
if (size_equal (outliers, yi))
333
yi(outliers) = extrap;
334
yi = reshape (yi, szx);
335
elseif (! isvector (yi))
336
yi(outliers, :) = extrap;
338
yi(outliers.') = extrap;
388
402
%! h = plot (x, interp1 (x1, y1, x), 'b', x1, y1, 'sb');
390
404
%! g = plot (x, interp1 (x2, y2, x), 'r', x2, y2, '*r');
392
%! legend ([h(1), g(1)], {'left-continous', 'right-continuous'}, ...
393
%! 'location', 'east')
405
%! axis ([0.5 3.5 -0.5 1.5])
406
%! legend ([h(1), g(1)], {'left-continuous', 'right-continuous'}, ...
407
%! 'location', 'northwest')
395
409
%! %--------------------------------------------------------
396
%! % red curve is left-continuos and blue is right-continuous at x = 2
410
%! % red curve is left-continuous and blue is right-continuous at x = 2
398
412
##FIXME: add test for n-d arguments here
597
%!# test linear extrapolation
611
## test extrapolation (linear)
598
612
%!assert (interp1 ([1:5],[3:2:11],[0,6],"linear","extrap"), [1, 13], eps)
599
613
%!assert (interp1 (xp, yp, [-1, max(xp)+1],"linear",5), [5, 5])
615
## Basic sanity checks
601
616
%!assert (interp1 (1:2,1:2,1.4,"nearest"), 1)
602
617
%!assert (interp1 (1:2,1:2,1.4,"linear"), 1.4)
603
618
%!assert (interp1 (1:4,1:4,1.4,"cubic"), 1.4)
604
%!assert (interp1 (1:2,1:2,1.1, "spline"), 1.1)
619
%!assert (interp1 (1:2,1:2,1.1,"spline"), 1.1)
605
620
%!assert (interp1 (1:3,1:3,1.4,"spline"), 1.4)
607
622
%!assert (interp1 (1:2:4,1:2:4,1.4,"*nearest"), 1)
613
628
%!assert (interp1 ([3,2,1],[3,2,2],2.5), 2.5)
615
%!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])
616
630
%!assert (interp1 ([4,4,3,2,0],[0,1,4,2,1],[1.5,4,4.5], "linear"), [1.75,1,NA])
617
631
%!assert (interp1 (0:4, 2.5), 1.5)
633
## Left and Right discontinuities
634
%!assert (interp1 ([1,2,2,3,4],[0,1,4,2,1],[-1,1.5,2,2.5,3.5], "linear", "extrap", "right"), [-8,2,4,3,1.5])
635
%!assert (interp1 ([1,2,2,3,4],[0,1,4,2,1],[-1,1.5,2,2.5,3.5], "linear", "extrap", "left"), [-2,0.5,1,1.5,1.5])
637
%% Test input validation
619
638
%!error interp1 ()
620
%!error interp1 (1,1,1, "linear")
621
%!error interp1 (1,1,1, "*nearest")
622
%!error interp1 (1,1,1, "*linear")
623
%!error interp1 (1:2,1:2,1, "bogus")
639
%!error interp1 (1,2,3,4,5,6,7)
640
%!error <table too short> interp1 (1,1,1, "linear")
641
%!error <table too short> interp1 (1,1,1, "*nearest")
642
%!error <table too short> interp1 (1,1,1, "*linear")
643
%!warning <multiple discontinuities> interp1 ([1 1 1 2], [1 2 3 4], 1);
644
%!error <discontinuities not supported> interp1 ([1 1],[1 2],1, "pchip")
645
%!error <discontinuities not supported> interp1 ([1 1],[1 2],1, "cubic")
646
%!error <discontinuities not supported> interp1 ([1 1],[1 2],1, "spline")
647
%!error <invalid method> interp1 (1:2,1:2,1, "bogus")