one-dimensional interpolation
There is kinds of one-dimensional interpolation in MATLAB:
- Polynomial interpolation
- fft-based interpolation
Polynomial interpolation
The function interp1
performs one-dimensional interpolation, an important operation for data analysis and curve fitting. This function uses polynomial techniques, fitting the supplied data with polynomial functions between data points and Eval Uating the appropriate function at the desired interpolation points. Its more general form is
y
is a vector containing the values of a function, and are x
a vector of the same length containing the points for which The values in was y
given. is xi
a vector containing the points at which to Interpolate. was an method
optional strin G Specifying an interpolation method:
- Nearest neighbor interpolation (
method = ‘nearest‘
). This method sets the value of a 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 Relev Ant function at the points specified by xi
. The default method for the interp1
function.
- Cubic spline interpolation (
method = ‘spline‘
). This method fits a different cubic function between all pair of existing data points, and uses the spline
function to perf ORM Cubic spline interpolation at the data points.
- Cubic interpolation (
method = ‘pchip‘
or ‘cubic‘
). These methods is 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
was outside the interval spanned x
by, the specified interpolation method are used for Extrapola tion. Alternatively, yi = interp1(x,Y,xi,method,extrapval)
replaces extrapolated values with extrapval
. Are NaN
often used for extrapval
.
All methods work with nonuniformly spaced data.
Speed, Memory, and smoothness considerations
When choosing a interpolation method, keep in mind, some require more memory or longer computation time than other S. However, 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 is continuous, but the slope changes at the vertex points.
- Cubic spline interpolation has the longest relative execution time, although it r Equires less memory than cubic interpolation. It produces the smoothest results of all the interpolation methods. Obtain unexpected results, however, if your input data is non-uniform and some points are much closer together tha n 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 is continuous.
The relative performance of each method holds true even for interpolation of two-dimensional or multidimensional data. For a graphical comparison of interpolation methods, see the section comparing interpolation methods.
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
x
is a vector containing the values of a periodic function, sampled at equally spaced points. is the number of n
equally spaced points to return.
MATLAB Function Reference |
|
INTERP1
One-dimensional data interpolation (table lookup)
Syntax
Yi = Interp1 (x,y,xi) Yi = interp1 (y,xi) Yi = interp1 (x,y,xi,method) Yi = Interp1 (x,y,xi,method, ' extrap ') Yi = Interp1 (x,y,xi , Method,extrapval)
Description
yi = interp1(x,Y,xi)
Returns vector yi
containing elements corresponding to the elements of and xi
determined by interpolation within VEC Tors x
and Y
. The vector x
specifies the points at which, the data is Y
given. If Y
is a matrix, then the interpolation are performed for each column of and is Y
yi
length(xi)
-by- size(Y,2)
.
yi = interp1(Y,xi)
Assumes x = 1:N
that, where is N
Y
the length Y
of a for vector, or for size(Y,1)
matrix Y
.
yi = interp1(x,Y,xi,
method
)
Interpolates using alternative methods:
‘nearest‘ |
Nearest neighbor interpolation |
‘linear‘ |
Linear interpolation (default) |
‘spline‘ |
Cubic spline interpolation |
‘pchip‘ |
piecewise Cubic Hermite Interpolation |
‘cubic‘ |
(Same as ' pchip‘ ) |
‘v5cubic‘ |
Cubic interpolation used in MATLAB 5 |
For ‘nearest‘
the, ‘linear‘
, and ‘v5cubic‘
methods, returns for any element of which is interp1(x,Y,xi,method)
outside the NaN
xi
interval sp Anned by x
. For all and methods, interp1
performs extrapolation for out of range values.
yi = interp1(x,Y,xi,method,‘extrap‘)
Uses the specified method to perform extrapolation for out of range values.
yi = interp1(x,Y,xi,method,extrapval)
Returns the scalar for out of extrapval
range values. And is NaN
0
often used for extrapval
.
The interp1
command interpolates between data points. It finds values at intermediate points, for a one-dimensional function that underlies the data. This function was shown below, along with the relationship between vectors,,, and x
Y
xi
yi
.
Interpolation is the same operation as table lookup. Described in table lookup terms, the table was [x,Y]
and interp1
looks xi
x
up the elements of In, a ND, based upon their locations, returns values interpolated within the elements of yi
Y
.
Note interp1q is quicker than on interp1 non-uniformly spaced data because it does no input checking. interp1q for-to-work properly, x must is a monotonically increasing column vector and Y must be a column vector or MA Trix with length(X) rows. The Type at the command line is more help interp1q information. |
Examples
Example 1. Generate a coarse sine curve and interpolate over a finer abscissa.
x = 0:10; y = sin (x); Xi = 0:.25:10; Yi = Interp1 (x,y,xi); Plot (x, y, ' o ', xi,yi)
- With ' spline ' method:
x = 0:10;
y = sin (x);
Xi = 0:.25:10;
Yi = interp1 (x,y,xi, 'spline');
figure;plot (x, y, ' o ', xi,yi)
Example 2. Here is the vectors representing the census years from 1900 to 1990 and the corresponding of the states population in mil Lions of people.
The expression interp1(t,p,1975)
interpolates within the census data to estimate the population in 1975. The result is
Now interpolate within the data at every year from 1900 to, and plot the result.
x = 1900:1:2000; y = Interp1 (t,p,x, ' spline '); Plot (t,p, ' O ', x, y)
Sometimes it is more convenient to think of interpolation in table lookup terms, where the data was stored in a s Ingle table. If A portion of the census data is stored with a single 5-by-2 table,
Then the population in 1975, obtained by table lookup within the matrix tab
, is
p = INTERP1 (tab (:, 1), Tab (:, 2), 1975) P = 214.8585
Algorithm
The interp1
command is a MATLAB m-file. The ' and nearest‘
' linear‘
methods have straightforward implementations.
spline‘
for the "method, interp1
calls a function that spline
uses the functions ppval
, mkpp
and unmkpp
. These routines form a small suite of functions for working with piecewise polynomials. spline
uses them to perform the cubic spline interpolation. For access to more advanced features, see the spline
reference page, the M-file Help for these functions, and the Spline T Oolbox.
pchip‘
‘cubic‘
for the ' and methods, interp1
calls a function that pchip
performs piecewise cubic interpolation within the Vect ORS x
and y
. This method preserves monotonicity and the shape of the data. See the pchip
Reference page for more information.
See Also
interpft
, interp2
, interp3
, interpn
, pchip
,spline
References
[1] de Boor, C., A PracticalGuide to Splines, Springer-verlag, 1978.
Interpolation in MATLAB