Interpolation in MATLAB
Mathematics | ![]() ![]() |
One-Dimensional Interpolation
There are two kinds of one-dimensional interpolation in MATLAB:
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 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 byxi
. This is the default method for theinterp1
function. - Cubic spline interpolation (
method = 'spline'
). This method fits a different cubic function between each pair of existing data points, and uses thespline
function to perform cubic spline interpolation at the data points. - Cubic interpolation (
method = 'pchip'
or'cubic'
). These methods are identical. They use thepchip
function to perform piecewise cubic Hermite interpolation within the vectorsx
andy
. 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 non-uniform 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 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
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.
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 xi
and determined by interpolation within vectors x
and Y
. The vector x
specifies the points at which the data Y
is given. If Y
is a matrix, then the interpolation is performed for each column of Y
and yi
is length(xi)
-by-size(Y,2)
.
yi = interp1(Y,xi)
assumes that x = 1:N
, where N
is the length of Y
for vector Y
, or size(Y,1)
for matrix Y
.
interpolates using alternative methods:yi = interp1(x,Y,xi,
method
)
'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 the 'nearest'
, 'linear'
, and 'v5cubic'
methods, interp1(x,Y,xi,method)
returns NaN
for any element of xi
that is outside the interval spanned by x
. For all other 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 extrapval
for out of range values. NaN
and 0
are often used for extrapval
.
The interp1
command interpolates between data points. It finds values at intermediate points, of a one-dimensional function that underlies the data. This function is shown below, along with the relationship between vectors
x
, Y
, xi
, and yi
.
Interpolation is the same operation as table lookup. Described in table lookup terms, the table is [x,Y]
and interp1
looks up the elements of xi
in x
, and, based upon their locations, returns values yi
interpolated within the elements of Y
.
Note interp1q is quicker than interp1 on non-uniformly spaced data because it does no input checking. For interp1q to work properly, x must be a monotonically increasing column vector and Y must be a column vector or matrix with length(X) rows. Type help interp1q at the command line for more 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 are two vectors representing the census years from 1900 to 1990 and the corresponding United States population in millions of people.
t = 1900:10:1990;
p = [75.995 91.972 105.711 123.203 131.669...
150.697 179.323 203.212 226.505 249.633];
The expression interp1(t,p,1975)
interpolates within the census data to estimate the population in 1975. The result is
ans =
214.8585
Now interpolate within the data at every year from 1900 to 2000, 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 are stored in a single table. If a portion of the census data is stored in a single 5-by-2 table,
tab =
1950 150.697
1960 179.323
1970 203.212
1980 226.505
1990 249.633
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 'nearest'
and 'linear'
methods have straightforward implementations.
For the 'spline'
method, interp1
calls a function spline
that 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 Toolbox.
For the 'pchip'
and 'cubic'
methods, interp1
calls a function pchip
that performs piecewise cubic interpolation within the vectors 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 Practical Guide to Splines, Springer-Verlag, 1978.
Interpolation in MATLAB的更多相关文章
- superresolution_v_2.0 Application超分辨率程序文档
SUPERRESOLUTION GRAPHICAL USER INTERFACE DOCUMENTATION Contents 1.- How to use this application. 2.- ...
- 数字图像处理实验(4):PROJECT 02-04 [Multiple Uses],Zooming and Shrinking Images by Bilinear Interpolation 标签: 图像处理MATLAB
实验要求: Zooming and Shrinking Images by Bilinear Interpolation Objective To manipulate another techniq ...
- MATLAB曲面插值及交叉验证
在离散数据的基础上补插连续函数,使得这条连续曲线通过全部给定的离散数据点.插值是离散函数逼近的重要方法,利用它可通过函数在有限个点处的取值状况,估算出函数在其他点处的近似值.曲面插值是对三维数据进行离 ...
- paper 121 :matlab中imresize函数
转自:http://www.cnblogs.com/rong86/p/3558344.html matlab中函数imresize简介: 函数功能:该函数用于对图像做缩放处理. 调用格式: B = i ...
- Matlab 进阶学习记录
最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal = proposal_config('image_means', ...
- matlab中imresize
matlab中函数imresize简介: 函数功能:该函数用于对图像做缩放处理. 调用格式: B = imresize(A, m) 返回的图像B的长宽是图像A的长宽的m倍,即缩放图像. m大于1, 则 ...
- matlab 2012 vs2010混合编程
电脑配置: 操作系统:window 8.1 Matlab 2012a安装路径:D:\Program Files\MATLAB\R2012a VS2010 : OpenCV 2.4.3:D:\Progr ...
- 非刚性图像配准 matlab简单示例 demons算法
2011-05-25 17:21 非刚性图像配准 matlab简单示例 demons算法, % Clean clc; clear all; close all; % Compile the mex f ...
- MATLAB中的函数的归总
字符串操作函数 1. 函数eval可以用来执行用字符串表示的表达式 2. 函数deblank可以去掉字符串末尾的所有空格 3. 函数findstr可以用来在长 ...
随机推荐
- sql返回两个日期之间的日期_函数实现
-- Description:返回两段日期之间的所有日期 <Description,,>-- ============================================ ...
- Linux系统重要快捷键& Shell 常用通配符
[Tab]:使用Tab键来进行命令补全: [Ctrl+c]:强行终止当前程序: [Ctrl+d]:键盘输入结束或退出终端: [Ctrl+s]:暂定当前程序,暂停后按下任意键恢复运行: [Ctrl+z] ...
- 推荐记录片系列:Ultimate Factories系列和MegaStructures系列
-_- 我刚刚看完记录片 <终极工厂:M1主战坦克> (Ultimate Factories: M-1 Tank) (2006), 决定推荐几个系列的记录片. →_→ 1993年后出厂的M ...
- window下安装pip工具,再利用pip安装工具来安装其他的python包
1.在安装pip前,请确认你window系统中已经安装好了python,和easy_install工具,如果系统安装成功,easy_install在目录C:\Python27\Scripts 下面,如 ...
- dos2unix
dos2unix 实际上就是把文本文件里面的^M删除,我一般懒得用这个命令,vi里面用一个命令(:%s/^m//g)删除即可. 用法举例: dos2unix a.txt b.txt 要把一批文件都do ...
- JAVA vo pojo javabean dto区别
JavaBean 是一种JAVA语言写成的可重用组件.为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器.JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性 ...
- leetcode Online Judge 150题 解答分析之一 Reverse Words in a String
问题 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- sql语句的匹配
like 的通配符有两种 %(百分号):代表零个.一个或者多个字符. _(下划线):代表一个数字或者字符. 1. name以"李"开头 where name like '李%' 2 ...
- C++ 四种类型转换
在写代码中经常会有很多的隐式类型转换或显式类型转换. 对于隐式的类型转换主要是放生在赋值的时候,讲变量赋值给不同类型的变量的时候就会发生类型转换,如果是宽化转换(即从占字节少的类型向占字节多的类型转换 ...
- php生成html文件的多种方法介绍
我经常会在网上看到有人问怎么将整个动态的网站静态化,其实实现的方法很简单. 代码如下 复制代码 <?php//在你的开始处加入 ob_start(); ob_start(); //以下是你的代 ...