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 separately.
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 NA.
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 withppval
to evaluate the interpolation. There is an equivalence, such thatppval (interp1 (
x,
y,
method, 'pp'),
xi) == interp1 (
x,
y,
xi,
method, 'extrap')
.An example of the use of
interp1
isxf=[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,"r",xf,lin,"g",xf,spl,"b", ... xf,cub,"c",xf,near,"m",xp,yp,"r*"); legend ("original","linear","spline","cubic","nearest")See also: interpft
The following code
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,"linear"); spl=interp1(xp,yp,xf,"spline"); cub=interp1(xp,yp,xf,"pchip"); near=interp1(xp,yp,xf,"nearest"); plot(xf,yf,"r",xf,near,"g",xf,lin,"b",xf,cub,"c",xf,spl,"m",xp,yp,"r*"); legend ("original","nearest","linear","pchip","spline") %-------------------------------------------------------- % confirm that interpolated function matches the original
Produces the following figure
![]() |
The following code
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,"*linear"); spl=interp1(xp,yp,xf,"*spline"); cub=interp1(xp,yp,xf,"*cubic"); near=interp1(xp,yp,xf,"*nearest"); plot(xf,yf,"r",xf,near,"g",xf,lin,"b",xf,cub,"c",xf,spl,"m",xp,yp,"r*"); legend ("*original","*nearest","*linear","*cubic","*spline") %-------------------------------------------------------- % confirm that interpolated function matches the original
Produces the following figure
![]() |
The following code
t = 0 : 0.3 : pi; dt = t(2)-t(1); n = length (t); k = 100; dti = dt*n/k; ti = t(1) + [0 : k-1]*dti; y = sin (4*t + 0.3) .* cos (3*t - 0.1); ddyc = diff(diff(interp1(t,y,ti,'cubic'))./dti)./dti; ddys = diff(diff(interp1(t,y,ti,'spline'))./dti)./dti; ddyp = diff(diff(interp1(t,y,ti,'pchip'))./dti)./dti; plot (ti(2:end-1), ddyc,'g+',ti(2:end-1),ddys,'b*', ... ti(2:end-1),ddyp,'c^'); legend('cubic','spline','pchip'); title("Second derivative of interpolated 'sin (4*t + 0.3) .* cos (3*t - 0.1)'");
Produces the following figure
![]() |