interp1

interp1 returns interpolated data of one-dimensional function.
  • interp1(x,v,xq)
    • x: vector with sample points
    • v: evaluation of x
    • xq: coordenates of querying points
  • interp1(x,v,xq,opt,ext)
  • opt defines the interpolation method:
    • linear (default): linear interpolation from nearest neighbour
    • previous: retuns the previous neighbour
    • next: returns the next neighbour
    • nearest: returns the nearest neighbour
    • pchip: returns interpolated data using the Piecewise Cubic Hermite Interpolating Polynomial
    • spline: returns interpolated data using cubic spline interpolation
    ext defines the extrapolation method
    • 'extrap': uses the interpolation method for extrapolation
    • X: if X is a scalar, the value for all points outside the domain of x will be X
    • In case of using linear interpolation, the following extrapolation methods are available:
    • flat (default)
    • linear
    • flatright
    • flatleft
  • interp1(v,xq): if the vector x is not defined, it will set by default x = 1:length(v)

Examples

>> x = [1 2 3]
y = [4 8 13]
xq = 0.5:0.5:3.5

% linear interpolation by default and flat extrapolation

yq1 = interp1(x,y,xq)# 4 4 6 8 10.5 13 13 >> op = struct('show', false, 'xAxisTicks', 3, 'yAxisTicks', 4) overlay({scatter(x,y,op), plot(xq,yq1,op)})

% linear interpolation and linear extrapolation

>> yq2 = interp1(x,y,xq,'linear', 'linear')# 2 4 6 8 10.5 13 15.5 >> overlay({scatter(x,y,op), plot(xq,yq2,op)})

% previous interpolation and NaN's for points out of x

>> yq3 = interp1(x,y,xq,'previous')# NaN 4 4 8 8 13 NaN >> overlay({scatter(x,y,op), plot(xq,yq3,op)})

% previous interpolation and extrapolation

>> yq4 = interp1(x,y,xq,'previous', 'extrap')# NaN 4 4 8 8 13 13 >> overlay({scatter(x,y,op), plot(xq,yq4,op)})

% next interpolation and NaN's for points out of x

>> yq5 = interp1(x,y,xq,'next')# NaN 4 8 8 13 13 NaN >> overlay({scatter(x,y,op), plot(xq,yq5,op)})

% nearest interpolation and NaN's for points out of x

>> yq6 = interp1(x,y,xq,'nearest')# NaN 4 8 8 13 13 NaN >> overlay({scatter(x,y,op), plot(xq,yq6,op)})

% pchip interpolation

>> yq7 = interp1(x,y,xq,'pchip')# 2.39583 4 5.88194 8 10.3681 13 15.8542 >> overlay({scatter(x,y,op), plot(xq,yq7,op)})

% pchip interpolation and 0's for points out of x

>> yq8 = interp1(x,y,xq,'pchip', 0)# 0 4 5.88194 8 10.3681 13 0 >> overlay({scatter(x,y,op), plot(xq,yq8,op)})

% spline interpolation and extrapolation

>> yq9 = interp1(x,y,xq,'spline')# 2.375 4 5.875 8 10.375 13 15.875 >> overlay({scatter(x,y,op), plot(xq,yq9,op)})

x = 0:pi/4:2*pi
v = cos(x)
xq = 0:pi/8:2*pi
vq1 = interp1(x,v,xq) 

% linear interpolation

op1 = struct('show', false, 'lineStroke', 'orange') vq2 = interp1(x,v,xq,'pchip')

% pchip interpolation

op2 = struct('show', false, 'lineStroke', 'gray', 'dashArray', '1 1') op3 = struct('show', false) options.legendLabels = {'linear', 'pchip', 'data'} overlay({plot(xq,vq1,op1), plot(xq,vq2,op2), scatter(x,v,op3)},options)

See also

interp2 | spline | pchip