Gridded Data

One-Dimensional Data

Polynomial Interpolation

The function interp1 performs one-dimensional interpolation. This function uses polynomial techniques, fitting the supplied data with polynomial functions between data points and evaluating the appropriate function at the desired interpolation points. Its most general form is

yi = interp1(x,y,xi,method)

y is a vector containing the values of a function, and x is a vector of the same length containing the points for which the values in y are given. xi is a vector containing the points at which to interpolate. method is an optional string specifying an interpolation method:

If any element of xi is outside the interval spanned by x, the specified interpolation method is used for extrapolation. Alternatively, yi = interp1(x,Y,xi,method,extrapval) replaces extrapolated values with extrapval. NaN is often used for extrapval.

All methods work with nonuniformly spaced data.

Speed, Memory, and Smoothness Considerations.   When choosing an interpolation method, keep in mind that some require more memory or longer computation time than others. However, you may need to trade off these resources to achieve the desired smoothness in the result:

FFT-Based Interpolation

The function interpft performs one-dimensional interpolation using an FFT-based method. This method calculates the Fourier transform of a vector that contains the values of a periodic function. It then calculates the inverse Fourier transform using more points. Its form is

y = interpft(x,n)

x is a vector containing the values of a periodic function, sampled at equally spaced points. n is the number of equally spaced points to return.

Two-Dimensional Data

Two-Dimensional Methods

The function interp2 performs two-dimensional interpolation, an important operation for image processing and data visualization. Its most general form is

ZI = interp2(X,Y,Z,XI,YI,method)

Z is a rectangular array containing the values of a two-dimensional function, and X and Y are arrays of the same size containing the points for which the values in Z are given. XI and YI are matrices containing the points at which to interpolate the data. method is an optional string specifying an interpolation method.

There are three different interpolation methods for two-dimensional data:

All of these methods require that X and Y be monotonic, that is, either always increasing or always decreasing from point to point. You should prepare these matrices using the meshgrid function, or else be sure that the "pattern" of the points emulates the output of meshgrid. In addition, each method automatically maps the input to an equally spaced domain before interpolating. If X and Y are already equally spaced, you can speed execution time by prepending an asterisk to the method string, for example, '*cubic'.

A Comparison of Methods

This example compares two-dimensional interpolation methods on a 7-by-7 matrix of data:

  1. Generate the peaks function at low resolution:

    [x,y] = meshgrid(-3:1:3);
    z = peaks(x,y);
    surf(x,y,z)

    three-dimensional surface plot

  2. Generate a finer mesh for interpolation:

    [xi,yi] = meshgrid(-3:0.25:3);
    
  3. Interpolate using nearest neighbor interpolation:

    zi1 = interp2(x,y,z,xi,yi,'nearest');
    
  4. Interpolate using bilinear interpolation:

    zi2 = interp2(x,y,z,xi,yi,'bilinear');
    
  5. Interpolate using bicubic interpolation:

    zi3 = interp2(x,y,z,xi,yi,'bicubic');
    
  6. Compare the surface plots for the different interpolation methods.

    nearest, bilinear, and bicubic surface plots

  7. Compare the contour plots for the different interpolation methods.

    nearest, bilinear, and bicubic contour plots

Notice that the bicubic method, in particular, produces smoother contours. This is not always the primary concern, however. For some applications, such as medical image processing, a method like nearest neighbor may be preferred because it doesn't generate any "new" data values.

Multidimensional Data

Function Summary

The following table lists functions that operate specifically on multidimensional data.

Interpolation Functions for Multidimensional Data

Function

Description

interp3

Three-dimensional data interpolation

interpn

Multidimensional data interpolation

ndgrid

Multidimensional data gridding (elmat directory)

Interpolation of Three-Dimensional Data

The function interp3 performs three-dimensional interpolation, finding interpolated values between points of a three-dimensional set of samples V. You must specify a set of known data points:

The most general form for interp3 is

VI = interp3(X,Y,Z,V,XI,YI,ZI,method)

XI, YI, and ZI are the points at which interp3 interpolates values of V. For out-of-range values, interp3 returns NaN.

There are three different interpolation methods for three-dimensional data:

All of these methods require that X, Y, and Z be monotonic, that is, either always increasing or always decreasing in a particular direction. In addition, you should prepare these matrices using the meshgrid function, or else be sure that the "pattern" of the points emulates the output of meshgrid.

Each method automatically maps the input to an equally spaced domain before interpolating. If x is already equally spaced, you can speed execution time by prepending an asterisk to the method string, for example, '*cubic'.

Interpolation of Higher Dimensional Data

The function interpn performs multidimensional interpolation, finding interpolated values between points of a multidimensional set of samples V. The most general form for interpn is

VI = interpn(X1,X2,X3...,V,Y1,Y2,Y3,...,method)

1, 2, 3, ... are matrices that specify the points for which values of V are given. V is a matrix that contains the values corresponding to these points. 1, 2, 3, ... are the points for which interpn returns interpolated values of V. For out-of-range values, interpn returns NaN.

Y1, Y2, Y3, ... must be either arrays of the same size, or vectors. If they are vectors of different sizes, interpn passes them to ndgrid and then uses the resulting arrays.

There are three different interpolation methods for multidimensional data:

All of these methods require that X1, X2,X3 be monotonic. In addition, you should prepare these matrices using the ndgrid function, or else be sure that the "pattern" of the points emulates the output of ndgrid.

Each method automatically maps the input to an equally spaced domain before interpolating. If X is already equally spaced, you can speed execution time by prepending an asterisk to the method string; for example, '*cubic'.

Multidimensional Data Gridding

The ndgrid function generates arrays of data for multidimensional function evaluation and interpolation. ndgrid transforms the domain specified by a series of input vectors into a series of output arrays. The ith dimension of these output arrays are copies of the elements of input vector xi.

The syntax for ndgrid is

[X1,X2,X3,...] = ndgrid(x1,x2,x3,...)

For example, assume that you want to evaluate a function of three variables over a given range. Consider the function

z = x sub 2 times e to the power ( minus x sub 1 squared minus x sub 2 squared minus x sub 3 squared

for minus 2 pi less than or equal x sub 1 less than or equal 0, 2 pi less than or equal x sub 2 less than or equal 4 pi, and 0 less than or equal x sub 3 less than or equal pi. To evaluate and plot this function,

x1 = -2:0.2:2;
x2 = -2:0.25:2;
x3 = -2:0.16:2;
[X1,X2,X3] = ndgrid(x1,x2,x3);
z = X2.*exp(-X1.^2 -X2.^2 -X3.^2);
slice(X2,X1,X3,z,[-1.2 0.8 2],2,[-2 0.2]) 

slice plot of the function

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS