Next: , Previous: Polynomial Manipulations, Up: Top


30 Interpolation

Octave provides the following functions for interpolation.

— Function File: yi = interp1 (x, y, xi)
— Function File: yi = interp1 (..., method)
— Function File: yi = interp1 (..., extrap)
— Function File: pp = interp1 (..., 'pp')

One-dimensional interpolation. Interpolate y, defined at the points x, at the points xi. The sample points x must be strictly monotonic. If y is an array, treat the columns of y seperately.

Method is one of:

'nearest'
Return the nearest neighbour.
'linear'
Linear interpolation from nearest neighbours
'pchip'
Piece-wise cubic hermite interpolating polynomial
'cubic'
Cubic interpolation from four nearest neighbours
'spline'
Cubic spline interpolation–smooth first and second derivatives throughout the curve

Appending '*' to the start of the above method 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. If extrap is a number, replace values beyond the endpoints with that number. If extrap is missing, assume NaN.

If the string argument 'pp' is specified, then xi should not be supplied and interp1 returns the piece-wise polynomial that 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').

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);
             spl=interp1(xp,yp,xf,'spline');
             cub=interp1(xp,yp,xf,'cubic');
             near=interp1(xp,yp,xf,'nearest');
             plot(xf,yf,';original;',xf,lin,';linear;',xf,spl,';spline;',...
                  xf,cub,';cubic;',xf,near,';nearest;',xp,yp,'*;;');
     
     
     
See also: interpft.

— Function File: zi= interp2 (x, y, z, xi, yi)
— Function File: zi= interp2 (Z, xi, yi)
— Function File: zi= interp2 (Z, n)
— Function File: zi= interp2 (..., method)
— Function File: zi= interp2 (..., method, extrapval)

Two-dimensional interpolation. x, y and z describe a surface function. If x and y are vectors their length must correspondent to the size of z. x and Yy must be monotonic. If they are matrices they must have the meshgrid format.

interp2 (x, y, Z, xi, yi, ...)
Returns a matrix corresponding to the points described by the matrices XI, YI.

If the last argument is a string, the interpolation method can be specified. The method can be 'linear', 'nearest' or 'cubic'. If it is omitted 'linear' interpolation is assumed.

interp2 (z, xi, yi)
Assumes x = 1:rows (z) and y = 1:columns (z)
interp2 (z, n)
Interleaves the Matrix z n-times. If n is ommited a value of n = 1 is assumed.

The variable method defines the method to use for the interpolation. It can take one of the values

'nearest'
Return the nearest neighbor.
'linear'
Linear interpolation from nearest neighbors.
'pchip'
Piece-wise cubic hermite interpolating polynomial (not implemented yet).
'cubic'
Cubic interpolation from four nearest neighbors.
'spline'
Cubic spline interpolation–smooth first and second derivatives throughout the curve (not implemented yet).

If a scalar value extrapval is defined as the final value, then values outside the mesh as set to this value. Note that in this case method must be defined as well. If extrapval is not defined then NaN is assumed.

     
     
See also: interp1.

— Function File: interpft (x, n)
— Function File: 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 an array, then operate along each column of the array seperately. If dim is specified, then interpolate along the dimension dim.

interpft assumes that the interpolated function is periodic, and so assumption are made about the end points of the inetrpolation.

     
     
See also: interp1.

— Loadable Function: vi = interpn (x1, x2, ..., xn, v, y1, y2, ..., yn)

Perform n-dimensional interpolation. Each element of then n-dimensional 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 are all n-dimensional arrays of the same size and represent the points at which the array vi is interpolated.

This function only performs linear interpolation.

     
     
See also: interp1, interp2, ndgrid.

— Function File: zi= bicubic (x, y, z, xi, yi)

Return a matrix zi corresponding to the bicubic interpolations at xi and yi of the data supplied as x, y and z.

For further information please see bicubic.pdf available at http://wiki.woodpecker.org.cn/moin/Octave/Bicubic

     
     
See also: interp2.

— Function File: pp = spline (x, y)
— Function File: yi = spline (x, y, xi)

Returns the cubic spline interpolation of y at the point x. Called with two arguments the piece-wise polynomial pp that may later be used with ppval to evaluate the polynomial at specific points.

The variable x must be a vector of length n, and y can be either a vector or array. In the case where y is a vector, it can 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 first derivative of the cubic spline at the end-points.

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 then reshaped internally to a matrix where to leading dimension is given by s1 * s2 * ... * sk and each row this matrix is then treated seperately. Note that this is exactly the opposite treatment than interp1 and is done for compatiability.

Called with a third input argument, spline evaluates the piece-wise spline at the points xi. There is an equivalence between ppval (spline (x, y), xi) and spline (x, y, xi).

     
     
See also: ppval, mkpp, unmkpp.

— Function File: idx = lookup (table, y)

Lookup values in a sorted table. Usually used as a prelude to interpolation.

If table is strictly increasing and idx = lookup (table, y), then table(idx(i)) <= y(i) < table(idx(i+1)) for all y(i) within the table. If y(i) is before the table, then idx(i) is 0. If y(i) is after the table then idx(i) is table(n).

If the table is strictly decreasing, then the tests are reversed. There are no guarantees for tables which are non-monotonic or are not strictly monotonic.

To get an index value which lies within an interval of the table, use:

          idx = lookup (table(2:length(table)-1), y) + 1
     

This expression puts values before the table into the first interval, and values after the table into the last interval.