Interpolation
*************

One-dimensional Interpolation
=============================

Octave supports several methods for one-dimensional interpolation, most of which are described in this section.  Polynomial Interpolation and Interpolation on Scattered Data describe additional methods.

 -- YI =: interp1 (X, Y, XI)
 -- YI =: interp1 (Y, XI)
 -- YI =: interp1 (..., METHOD)
 -- YI =: interp1 (..., EXTRAP)
 -- YI =: interp1 (..., "left")
 -- YI =: interp1 (..., "right")
 -- PP =: interp1 (..., "pp")

     One-dimensional interpolation.

     Interpolate input data to determine the value of YI at the points XI.  If not specified, X is taken to be the indices of Y (1:length (Y)).  If Y is a matrix or an N-dimensional array, the interpolation is performed on each column of Y.

     The interpolation METHOD is one of:

     "nearest"
          Return the nearest neighbor.

     "previous"
          Return the previous neighbor.

     "next"
          Return the next neighbor.

     "linear" (default)
          Linear interpolation from nearest neighbors.

     "pchip"
          Piecewise cubic Hermite interpolating polynomial---shape-preserving interpolation with smooth first derivative.

     "cubic"
          Cubic interpolation (same as "pchip").

     "spline"
          Cubic spline interpolation---smooth first and second derivatives throughout the curve.

     Adding '*' to the start of any method above forces interp1 to assume that X is uniformly spaced, and only X(1) and X(2) are referenced.  This is usually faster, and is never slower.  The default method is "linear".

     If EXTRAP is the string "extrap", then extrapolate values beyond the endpoints using the current METHOD.  If EXTRAP is a number, then replace values beyond the endpoints with that number.  When unspecified, EXTRAP defaults to NA.

     If the string argument "pp" is specified, then XI should not be supplied and interp1 returns a piecewise polynomial object.  This object can later be used with ppval to evaluate the interpolation. There is an equivalence, such that ppval (interp1 (X, Y, METHOD, "pp"), XI) == interp1 (X, Y, XI, METHOD, "extrap").

     Duplicate points in X specify a discontinuous interpolant.  There may be at most 2 consecutive points with the same value. If X is increasing, the default discontinuous interpolant is right-continuous.  If X is decreasing, the default discontinuous interpolant is left-continuous. The continuity condition of the interpolant may be specified by using the options "left" or "right" to select a left-continuous or right-continuous interpolant, respectively. Discontinuous interpolation is only allowed for "nearest" and "linear" methods; in all other cases, the X-values must be unique.

     An example of the use of interp1 is

          xf = [0:0.05:10];
          yf = sin (2*pi*xf/5);
          xp = [0:10];
          yp = sin (2*pi*xp/5);
          lin = interp1 (xp, yp, xf);
          near = interp1 (xp, yp, xf, "nearest");
          pch = interp1 (xp, yp, xf, "pchip");
          spl = interp1 (xp, yp, xf, "spline");
          plot (xf,yf,"r", xf,near,"g", xf,lin,"b", xf,pch,"c", xf,spl,"m",
                xp,yp,"r*");
          legend ("original", "nearest", "linear", "pchip", "spline");

     pchip, spline, interpft, interp2, interp3, interpn

There are some important differences between the various interpolation methods.  The "spline" method enforces that both the first and second derivatives of the interpolated values have a continuous derivative, whereas the other methods do not.  This means that the results of the "spline" method are generally smoother.  If the function to be interpolated is in fact smooth, then "spline" will give excellent results.  However, if the function to be evaluated is in some manner discontinuous, then "pchip" interpolation might give better results.

This can be demonstrated by the code

     t = -2:2;
     dt = 1;
     ti =-2:0.025:2;
     dti = 0.025;
     y = sign (t);
     ys = interp1 (t,y,ti,"spline");
     yp = interp1 (t,y,ti,"pchip");
     ddys = diff (diff (ys)./dti) ./ dti;
     ddyp = diff (diff (yp)./dti) ./ dti;
     figure (1);
     plot (ti,ys,"r-", ti,yp,"g-");
     legend ("spline", "pchip", 4);
     figure (2);
     plot (ti,ddys,"r+", ti,ddyp,"g*");
     legend ("spline", "pchip");

Fourier interpolation, is a resampling technique where a signal is converted to the frequency domain, padded with zeros and then reconverted to the time domain.

 -- Y =: interpft (X, N)
 -- Y =: interpft (X, N, DIM)

     Fourier interpolation.

     If X is a vector then X is resampled with N points.  The data in X is assumed to be equispaced.  If X is a matrix or an N-dimensional array, the interpolation is performed on each column of X.

     If DIM is specified, then interpolate along the dimension DIM.

     interpft assumes that the interpolated function is periodic, and so assumptions are made about the endpoints of the interpolation. interp1

There are two significant limitations on Fourier interpolation.  First, the function signal is assumed to be periodic, and so non-periodic signals will be poorly represented at the edges.  Second, both the signal and its interpolation are required to be sampled at equispaced points.  An example of the use of interpft is

     t = 0 : 0.3 : pi; dt = t(2)-t(1);
     n = length (t); k = 100;
     ti = t(1) + [0 : k-1]*dt*n/k;
     y = sin (4*t + 0.3) .* cos (3*t - 0.1);
     yp = sin (4*ti + 0.3) .* cos (3*ti - 0.1);
     plot (ti, yp, "g", ti, interp1 (t, y, ti, "spline"), "b", ...
           ti, interpft (y, k), "c", t, y, "r+");
     legend ("sin(4t+0.3)cos(3t-0.1)", "spline", "interpft", "data");

which demonstrates the poor behavior of Fourier interpolation for non-periodic functions.

In addition, the support functions spline and lookup that underlie the interp1 function can be called directly.

 -- PP =: spline (X, Y)
 -- YI =: spline (X, Y, XI)
     Return the cubic spline interpolant of points X and Y.

     When called with two arguments, return the piecewise polynomial PP that may be used with ppval to evaluate the polynomial at specific points.

     When called with a third input argument, spline evaluates the spline at the points XI.  The third calling form spline (X, Y, XI) is equivalent to ppval (spline (X, Y), XI).

     The variable X must be a vector of length N.

     Y can be either a vector or array.  If Y is a vector it must have a length of either N or N + 2.  If the length of Y is N, then the "not-a-knot" end condition is used. If the length of Y is N + 2, then the first and last values of the vector Y are the values of the first derivative of the cubic spline at the endpoints.

     If Y is an array, then the size of Y must have the form [S1, S2, ..., SK, N] or [S1, S2, ..., SK, N + 2]. The array is reshaped internally to a matrix where the leading dimension is given by S1 * S2 * ... * SK and each row of this matrix is then treated separately.  Note that this is exactly the opposite of interp1 but is done for MATLAB compatibility.

     pchip, ppval, mkpp, unmkpp

Multi-dimensional Interpolation
===============================

There are three multi-dimensional interpolation functions in Octave, with similar capabilities.  Methods using Delaunay tessellation are described in Interpolation on Scattered Data.

 -- ZI =: interp2 (X, Y, Z, XI, YI)
 -- ZI =: interp2 (Z, XI, YI)
 -- ZI =: interp2 (Z, N)
 -- ZI =: interp2 (Z)
 -- ZI =: interp2 (..., METHOD)
 -- ZI =: interp2 (..., METHOD, EXTRAP)

     Two-dimensional interpolation.

     Interpolate numeric reference data X, Y, Z to determine ZI at the coordinates XI, YI.  The reference data X, Y can be matrices, as returned by meshgrid, in which case the sizes of X, Y, and Z must be equal.  If X, Y are vectors describing a grid then length (X) == columns (Z) and length (Y) == rows (Z).  In either case the input data must be strictly monotonic.

     If called without X, Y, and just a single reference data matrix Z, the 2-D region X = 1:columns (Z), Y = 1:rows (Z) is assumed. This saves memory if the grid is regular and the distance between points is not important.

     If called with a single reference data matrix Z and a refinement value N, then perform interpolation over a grid where each original interval has been recursively subdivided N times.  This results in 2^N-1 additional points for every interval in the original grid.  If N is omitted a value of 1 is used.  As an example, the interval [0,1] with N==2 results in a refined interval with points at [0, 1/4, 1/2, 3/4, 1].

     The interpolation METHOD is one of:

     "nearest"
          Return the nearest neighbor.

     "linear" (default)
          Linear interpolation from nearest neighbors.

     "pchip"
          Piecewise cubic Hermite interpolating polynomial---shape-preserving interpolation with smooth first derivative.

     "cubic"
          Cubic interpolation using a convolution kernel function---third order method with smooth first derivative.

     "spline"
          Cubic spline interpolation---smooth first and second derivatives throughout the curve.

     EXTRAP is a scalar number.  It replaces values beyond the endpoints with EXTRAP.  Note that if EXTRAP is used, METHOD must be specified as well.  If EXTRAP is omitted and the METHOD is "spline", then the extrapolated values of the "spline" are used.  Otherwise the default EXTRAP value for any other METHOD is "NA". interp1, interp3, interpn, meshgrid

 -- VI =: interp3 (X, Y, Z, V, XI, YI, ZI)
 -- VI =: interp3 (V, XI, YI, ZI)
 -- VI =: interp3 (V, N)
 -- VI =: interp3 (V)
 -- VI =: interp3 (..., METHOD)
 -- VI =: interp3 (..., METHOD, EXTRAPVAL)

     Three-dimensional interpolation.

     Interpolate numeric reference data X, Y, Z, V to determine VI at the coordinates XI, YI, ZI.  The reference data X, Y, Z can be matrices, as returned by meshgrid, in which case the sizes of X, Y, Z, and V must be equal.  If X, Y, Z are vectors describing a cubic grid then length (X) == columns (V), length (Y) == rows (V), and length (Z) == size (V, 3).  In either case the input data must be strictly monotonic.

     If called without X, Y, Z, and just a single reference data matrix V, the 3-D region X = 1:columns (V), Y = 1:rows (V), Z = 1:size (V, 3) is assumed. This saves memory if the grid is regular and the distance between points is not important.

     If called with a single reference data matrix V and a refinement value N, then perform interpolation over a 3-D grid where each original interval has been recursively subdivided N times.  This results in 2^N-1 additional points for every interval in the original grid.  If N is omitted a value of 1 is used.  As an example, the interval [0,1] with N==2 results in a refined interval with points at [0, 1/4, 1/2, 3/4, 1].

     The interpolation METHOD is one of:

     "nearest"
          Return the nearest neighbor.

     "linear" (default)
          Linear interpolation from nearest neighbors.

     "cubic"
          Piecewise cubic Hermite interpolating polynomial---shape-preserving interpolation with smooth first derivative (not implemented yet).

     "spline"
          Cubic spline interpolation---smooth first and second derivatives throughout the curve.

     EXTRAPVAL is a scalar number.  It replaces values beyond the endpoints with EXTRAPVAL.  Note that if EXTRAPVAL is used, METHOD must be specified as well.  If EXTRAPVAL is omitted and the METHOD is "spline", then the extrapolated values of the "spline" are used.  Otherwise the default EXTRAPVAL value for any other METHOD is "NA". interp1, interp2, interpn, meshgrid

 -- VI =: interpn (X1, X2, ..., V, Y1, Y2, ...)
 -- VI =: interpn (V, Y1, Y2, ...)
 -- VI =: interpn (V, M)
 -- VI =: interpn (V)
 -- VI =: interpn (..., METHOD)
 -- VI =: interpn (..., METHOD, EXTRAPVAL)

     Perform N-dimensional interpolation, where N is at least two.

     Each element of the N-dimensional numeric array V represents a value at a location given by the parameters X1, X2, ..., XN.  The parameters X1, X2, ..., XN are either N-dimensional arrays of the same size as the array V in the "ndgrid" format or vectors.

     The parameters Y1, Y2, ..., YN represent the points at which the array VI is interpolated.  They can be vectors of the same length and orientation in which case they are interpreted as coordinates of scattered points.  If they are vectors of differing orientation or length, they are used to form a grid in "ndgrid" format.  They can also be N-dimensional arrays of equal size.

     If X1, ..., XN are omitted, they are assumed to be x1 = 1 : size (V, 1), etc.  If M is specified, then the interpolation adds a point half way between each of the interpolation points.  This process is performed M times.  If only V is specified, then M is assumed to be 1.

     The interpolation METHOD is one of:

     "nearest"
          Return the nearest neighbor.

     "linear" (default)
          Linear interpolation from nearest neighbors.

     "pchip"
          Piecewise cubic Hermite interpolating polynomial---shape-preserving interpolation with smooth first derivative (not implemented yet).

     "cubic"
          Cubic interpolation (same as "pchip" [not implemented yet]).

     "spline"
          Cubic spline interpolation---smooth first and second derivatives throughout the curve.

     The default method is "linear".

     EXTRAPVAL is a scalar number.  It replaces values beyond the endpoints with EXTRAPVAL.  Note that if EXTRAPVAL is used, METHOD must be specified as well.  If EXTRAPVAL is omitted and the METHOD is "spline", then the extrapolated values of the "spline" are used.  Otherwise the default EXTRAPVAL value for any other METHOD is NA. interp1, interp2, interp3, spline, ndgrid

A significant difference between interpn and the other two multi-dimensional interpolation functions is the fashion in which the dimensions are treated.  For interp2 and interp3, the y-axis is considered to be the columns of the matrix, whereas the x-axis corresponds to the rows of the array.  As Octave indexes arrays in column major order, the first dimension of any array is the columns, and so interpn effectively reverses the 'x' and 'y' dimensions.  Consider the example,

     x = y = z = -1:1;
     f = @(x,y,z) x.^2 - y - z.^2;
     [xx, yy, zz] = meshgrid (x, y, z);
     v = f (xx,yy,zz);
     xi = yi = zi = -1:0.1:1;
     [xxi, yyi, zzi] = meshgrid (xi, yi, zi);
     vi = interp3 (x, y, z, v, xxi, yyi, zzi, "spline");
     [xxi, yyi, zzi] = ndgrid (xi, yi, zi);
     vi2 = interpn (x, y, z, v, xxi, yyi, zzi, "spline");
     mesh (zi, yi, squeeze (vi2(1,:,:)));

where vi and vi2 are identical.  The reversal of the dimensions is treated in the meshgrid and ndgrid functions respectively.