1D interpolation in nearest, linear or spline mode
yp = interp1(y, xp) yp = interp1(x, y, xp) yp = interp1(.., xp, method) yp = interp1(.., xp, method, extrapolation)
y
is a vector: x=1:length(y)
.y
is a matrix or an hypermatrix:
x=1:size(y,1)
.x
abscissas.
y
is a vector, x
and
y
must have the same length.y
is a matrix or an hypermatrix, we must have
length(x)==size(y,1)
. Each column of
y
is then interpolated versus the same
x
abscissas, for the given xp
.yp
must be computed according
to data of interpolation nodes.
y
values at the given xp
.
y
is a vector: yp
has the
size of xp
.
y
is a matrix or an hypermatrix:
xp
is a scalar or a vector:
size(yp)
is [length(xp) size(y)(2:$)]
xp
is a matrix or an hypermatrix:
size(yp)
is [size(xp) size(y)(2:$)]
"linear": | linear interpolation between consecutive nodes, used by default. |
"spline": | interpolation by cubic splines |
"nearest": | for each value xp(j), yp(j) takes the value or y(i) corresponding to x(i) the nearest neighbor of xp(j) |
x
and y
have already been sorted accordingly.
"extrap": | interp1(x,y,xp, method, "extrap") is equivalent
to interp1(x,y,xp, method, method) . |
"linear": | Can be used with the "spline" (and obviously
"linear" ) interpolation methods. |
"periodic": | This extrapolation type can be used with the
"linear" or "spline"
interpolation methods. Then: if y is a
vector, y(1)==y($) is required ; otherwise
y(1,:)==y($,:) is required. |
"edgevalue": | Then yp(i)=y(1) for every
xp(i)<x(1) , and
yp(i)=y($) for every xp(i)>x($) . |
padding: | padding is a decimal or complex number
used to set yp(i)=padding for every
xp(i) ∉ [min(x),max(x)] . Example:
yi=interp1(x,y,xp,method, 0) . |
(none): | By default, the extrapolation is performed by splines when splines are used for the interpolation, and by padding with %nan when the interpolation is linear or by "nearest" node. |
Given (x,y,xp)
, this function computes the yp
components corresponding to xp by the interpolation between known
data provided by (x,y) nodes.
x
is priorly sorted in ascending order, and
y
values or per column are then sorted accordingly.
Interpolation of complex values: When
y
is complex, its real and imaginary parts are interpolated
separately, and then added to build the complex yp
.
interp1(x,y,xp,"nearest"): For any xp
at the middle of an [x(i),x(i+1)]
interval, the upper
bound x(i+1)
is considered as the nearest
x
value, and yp=y(i+1)
is assigned.
linear_interpn(..)
function,
with the corresponding "edgevalue"→"C0"
,
"linear"→"natural"
, "periodic"→"periodic"
extrapolation option.interp1(..,xp,"spline") or
interp1(..,xp,"spline","spline") or
interp1(..,xp,"spline","extrap")
use not_a_knot
edges conditions. Extrapolation is performed
by using both spline polynomials computed at the (x,y)
edges.
interp1(..,xp,"spline","edgevalue")
uses not_a_knot
edges conditions and then calls
interp(..,"C0")
to perform the actual interpolation
and extrapolation.
interp1(..,xp,"spline","periodic")
calls both splin(..)
and then interp(..)
with their "periodic"
option.
interp1(..,xp,"spline","linear")
calls splin(..,"natural")
for linear edges conditions,
and then feeds interp(..,"linear")
.
x = linspace(0, 10, 11)'; y = sin(x); xx = linspace(0,10,1000)'; yy2 = interp1(x, y, xx, 'linear'); yy1 = interp1(x, y, xx, 'nearest'); yy3 = interp1(x, y, xx, 'spline'); clf h = plot(xx, [yy1 yy2 yy3], x, y, '.') h(1).mark_size = 8; title "Interpolation of a poorly sampled sin() function" fontsize 3 legend(['nearest','linear','spline','nodes'], "in_lower_left"); | ![]() | ![]() |
Version | Description |
6.1.1 |
|