interp1 1D interpolation in nearest, linear or spline mode

## Syntax

              yp = interp1(y, xp)
              yp = interp1(x, y, xp)
              yp = interp1(.., xp, method)
              yp = interp1(.., xp, method, extrapolation)

## Arguments

x
  vector of at least 2 real numbers: Abscissas of known interpolation nodes, without duplicates. By default,
  - if `y` is a vector: `x=1:length(y)`.
  - if `y` is a matrix or an hypermatrix: `x=1:size(y,1)`.

y
  vector, matrix or hypermatrix of real or complex numbers: values at known interpolation nodes, at the corresponding `x` abscissas.
  - if `y` is a vector, `x` and `y` must have the same length.
  - if `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`.

xp
  scalar, vector, matrix or hypermatrix or decimal numbers: abscissas of points whose values `yp` must be computed according to data of interpolation nodes.

yp
  vector, matrix, or hypermatrix of numbers: interpolated `y` values at the given `xp`.
  - if `y` is a vector: `yp` has the size of `xp`.

  - if `y` is a matrix or an hypermatrix:
    - if `xp` is a scalar or a vector: `size(yp)` is `[length(xp) size(y)(2:$)]`
    - if `xp` is a matrix or an hypermatrix: `size(yp)` is `[size(xp) size(y)(2:$)]`

method
  string defining the interpolation method. Possible values and processing are:

  "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)

extrapolation
  string or number defining the yp(j) components for xp(j) values outside the [x(1)=min(x),x($)=max(x)] interval. We suppose here-below that `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.

## Description

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 interpolations

They are performed through the `linear_interpn(..)` function, with the corresponding `"edgevalue"→"C0"`, `"linear"→"natural"`, `"periodic"→"periodic"` extrapolation option.

### spline interpolations

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")`.

## Examples

    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");

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"); gcf().axes_size = [600 400];

## See also

- interp
- splin
- linear_interpn

## History

6.1.1

- For complex `y` values, `imag(y)` is no longer ignored: `real(y)` and `imag(y)` parts are separately interpolated.
- `"periodic"` extrapolation added for the linear and spline interpolations.
- `"edgevalue"` extrapolation added for all nearest, linear and spline interpolations.
- `"linear"` extrapolation added for the spline interpolation.
- When `xp` is an hypermatrix and `size(xp,1)==1`, `size(yp)` is now always `[size(xp) size(y)(2,$)` instead of `[size(xp,2:$), size(y)(2,$)`.