| MATLAB® | ![]() |
| On this page… |
|---|
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:
Nearest neighbor interpolation (method = 'nearest'). This method sets the value of an interpolated point to the value of the nearest existing data point.
Linear interpolation (method = 'linear'). This method fits a different linear function between each pair of existing data points, and returns the value of the relevant function at the points specified by xi. This is the default method for the interp1 function.
Cubic spline interpolation (method = 'spline'). This method fits a different cubic function between each pair of existing data points, and uses the spline function to perform cubic spline interpolation at the data points.
Cubic interpolation (method = 'pchip' or 'cubic'). These methods are identical. They use the pchip function to perform piecewise cubic Hermite interpolation within the vectors x and y. These methods preserve monotonicity and the shape of the data.
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:
Nearest neighbor interpolation is the fastest method. However, it provides the worst results in terms of smoothness.
Linear interpolation uses more memory than the nearest neighbor method, and requires slightly more execution time. Unlike nearest neighbor interpolation its results are continuous, but the slope changes at the vertex points.
Cubic spline interpolation has the longest relative execution time, although it requires less memory than cubic interpolation. It produces the smoothest results of all the interpolation methods. You may obtain unexpected results, however, if your input data is nonuniform and some points are much closer together than others.
Cubic interpolation requires more memory and execution time than either the nearest neighbor or linear methods. However, both the interpolated data and its derivative are continuous.
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.
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:
Nearest neighbor interpolation (method = 'nearest'). This method fits a piecewise constant surface through the data values. The value of an interpolated point is the value of the nearest point.
Bilinear interpolation (method = 'linear'). This method fits a bilinear surface through existing data points. The value of an interpolated point is a combination of the values of the four closest points. This method is piecewise bilinear, and is faster and less memory-intensive than bicubic interpolation.
Bicubic interpolation (method = 'cubic'). This method fits a bicubic surface through existing data points. The value of an interpolated point is a combination of the values of the sixteen closest points. This method is piecewise bicubic, and produces a much smoother surface than bilinear interpolation. This can be a key advantage for applications like image processing. Use bicubic interpolation when the interpolated data and its derivative must be continuous.
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'.
This example compares two-dimensional interpolation methods on a 7-by-7 matrix of data:
Generate the peaks function at low resolution:
[x,y] = meshgrid(-3:1:3); z = peaks(x,y); surf(x,y,z)

Generate a finer mesh for interpolation:
[xi,yi] = meshgrid(-3:0.25:3);
Interpolate using nearest neighbor interpolation:
zi1 = interp2(x,y,z,xi,yi,'nearest');
Interpolate using bilinear interpolation:
zi2 = interp2(x,y,z,xi,yi,'bilinear');
Interpolate using bicubic interpolation:
zi3 = interp2(x,y,z,xi,yi,'bicubic');
Compare the surface plots for the different interpolation methods.

Compare the contour plots for the different interpolation methods.

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.
The following table lists functions that operate specifically on multidimensional data.
Interpolation Functions for Multidimensional Data
Function | Description |
|---|---|
Three-dimensional data interpolation | |
Multidimensional data interpolation | |
Multidimensional data gridding (elmat directory) |
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:
X, Y, and Z matrices specify the points for which values of V are given.
A matrix V contains values corresponding to the points in X, Y, and Z.
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:
Nearest neighbor interpolation (method = 'nearest'). This method chooses the value of the nearest point.
Trilinear interpolation (method = 'linear'). This method uses piecewise linear interpolation based on the values of the nearest eight points.
Tricubic interpolation (method = 'cubic'). This method uses piecewise cubic interpolation based on the values of the nearest sixty-four points.
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'.
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:
Nearest neighbor interpolation (method = 'nearest'). This method chooses the value of the nearest point.
Linear interpolation (method = 'linear'). This method uses piecewise linear interpolation based on the values of the nearest two points in each dimension.
Cubic interpolation (method = 'cubic'). This method uses piecewise cubic interpolation based on the values of the nearest four points in each dimension.
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'.
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
![]()
for
,
,
and
. 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])

![]() | Function Summary | Scattered Data | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |