Engee documentation

interp1

Page in progress.

One-dimensional data interpolation (search table).

Library

EngeeDSP

Syntax

Function call

  • vq = interp1(x,v,xq) — returns the interpolated values of a one-dimensional function at the specified query points. The default function is interp1 uses linear interpolation. Vector x contains the sample points, and v — relevant values . Vector xq contains the coordinates of the query points.

    If you have multiple datasets received from the same points, you can transfer v as an array. Each column of the array v It contains its own set of one-dimensional sample values.

  • vq = interp1(x,v,xq,method) — defines an alternative interpolation method: "linear", "nearest", "next", "previous", "pchip", "cubic", "v5cubic", "makima" or "spline". The default method — "linear".

  • vq = interp1(x,v,xq,method,extrapolation) — defines a strategy for evaluating points that lie outside the definition area x. Set for the argument extrapolation meaning "extrap" to use the algorithm method for extrapolation. Alternatively, you can specify a scalar value, in which case the function interp1 returns this value for all points outside the definition area. x.

  • vq = interp1(v,xq) — returns the interpolated values and assumes a set of coordinates of the default sampling points. The default dots represent a sequence of numbers from 1 before n, where n depends on the form v:

    • if v — vector, the default points have the form 1:length(v);

    • if v — an array with default points in the form 1:size(v,1).

    Use this syntax if you are not interested in absolute distances between points.

  • vq = interp1(v,xq,method) — defines any of the alternative interpolation methods and uses the default sampling points.

Arguments

Input arguments

# x — sample points

+ vector

Details

Sample points specified as a row vector or column vector of real numbers. Values in x they should be different. Length x must meet one of the following requirements:

  • if v — vector, then length(x) must be equal to length(v);

  • if v — an array, then length(x) must be equal to size(v,1).

Типы данных

Float32, Float64

# v — sample values

+ vector | the matrix | array

Details

Sample values specified as a vector, matrix, or array of real or complex numbers. If v — a matrix or an array, then each column contains a separate set of one-dimensional values.

If v contains complex numbers, then the function interp1 interpolates the real and imaginary parts separately.

Типы данных

Float32, Float64

Support for complex numbers

Yes

# xq — query points

+ scalar | vector | the matrix | array

Details

Query points specified as a scalar, vector, matrix, or array of real numbers.

Типы данных

Float32, Float64

# method — interpolation method

+ "linear" (by default) | "nearest" | "next" | "previous" | "pchip" | "cubic" | "v5cubic" | "makima" | "spline"

Details

The interpolation method specified as one of the options in this table.

Method Description Continuity Comments

"linear"

Linear interpolation. The interpolated value at the query point is based on linear interpolation of values at neighboring grid points in each corresponding dimension. This is the default interpolation method.

  • Requires at least 2 dots

  • It requires more memory and computing time than nearest neighbor interpolation.

"nearest"

Nearest neighbor interpolation. The interpolated value at the query point is the value at the nearest point in the sampling grid.

Intermittent

  • Requires at least 2 points

  • Low memory requirements

  • Fastest calculation time

"next"

Interpolation to the next neighbor. The interpolated value at the query point is the value at the next point in the selection grid.

Intermittent

  • Requires at least 2 dots

  • The same memory requirements and computing time as for "nearest"

"previous"

Interpolation by the previous neighbor. The interpolated value at the query point is the value at the previous point in the selection grid.

Intermittent

  • Requires at least 2 points

  • The same memory requirements and computing time as for "nearest"

"pchip"

Piecewise cubic shape-preserving interpolation. The interpolated value at the query point is based on piecewise cubic interpolation of values at neighboring grid points while preserving the shape.

  • Requires at least 4 points

  • It requires more memory and computing time than "linear"

"cubic"

Cubic convolution used in Engee.

  • Requires at least 3 points

  • The points should be evenly distributed

  • For unevenly distributed data, this method uses interpolation. "spline"

  • The memory requirements and calculation time are similar "pchip"

"v5cubic"

The same as "cubic".

"makima"

Akim’s modified interpolation by cubic Hermite polynomials. The interpolated value at the query point is based on a piecewise linear function of polynomials of degree no higher than the third. Akim’s formula has been modified to prevent emissions.

  • Requires at least 2 dots

  • Creates less undulation than "spline" But it doesn’t smooth out as aggressively as "pchip"

  • Calculations require more resources than "pchip", but usually less than "spline"

  • The memory requirements are similar "spline"

"spline"

Spline interpolation using "not-a-knot" conditions. The interpolated value at the query point is based on cubic interpolation of values at neighboring grid points in each corresponding dimension.

  • At least 4 points are required, and if there are 2 or 3 points, linear or quadratic interpolation is applied, respectively.

  • It requires more memory and computing time than "pchip"

# extrapolation — extrapolation strategy

+ "extrap" | scalar

Details

The extrapolation strategy, defined as "extrap" or a real scalar value.

  • Specify "extrap" if you want the function to interp1 calculated points outside the definition area using the same method as for interpolation.

  • Specify a scalar value if you want the function to interp1 returned a certain constant value for points outside the definition area.

The default behavior depends on the input arguments:

  • If the interpolation methods are specified "pchip", "spline" or "makima", the default behavior is — "extrap".

  • All other interpolation methods return by default NaN for query points outside the definition area.

Типы данных

Char, String, Float32, Float64

Output arguments

# vq — interpolated values

+ scalar | vector | the matrix | array

Details

Interpolated values returned as a scalar, vector, matrix, or array. Size vq depends on the form v and xq.

Form v Form xq Size vq Example

Vector

Vector

size(xq)

If size(v) = [1 100] and size(xq) = [1 500] Then size(vq) = [1 500].

Vector

A matrix or an N-dimensional array

size(xq)

If size(v) = [1 100] and size(xq) = [50 30] Then size(vq) = [50 30].

A matrix or an N-dimensional array

Vector

[length(xq) size(v,2),…,size(v,n)]

If size(v) = [100 3] and size(xq) = [1 500] Then size(vq) = [500 3].

A matrix or an N-dimensional array

A matrix or an N-dimensional array

[size(xq,1),…,size(xq,n),… size(v,2),…,size(v,m)]

If size(v) = [4 5 6] and size(xq) = [2 3 7] Then size(vq) = [2 3 7 5 6].

Examples

Interpolation of a roughly sampled sinusoidal function

Details

Let’s define the sampling points x and the corresponding sample values v.

x = 0:π/4:2π
v = sin.(x)

Let’s define the query points for a more accurate selection in the range x.

xq = 0:π/16:2π

We interpolate the function at the query points and plot the result graph.

import EngeeDSP.Functions: interp1

vq1 = interp1(x, v, xq)

plot(xq, vq1, linestyle = :dot)
scatter!(x, v,
         markershape = :circle,
         xlims = (0, 2π),
         title = "(Default) Linear Interpolation")

interp1 1

Now let’s evaluate v at the same points, using the method "spline".

vq2 = interp1(x, v, xq, "spline")

plot(xq, vq2, linestyle = :dot)
scatter!(x, v,
         markershape = :circle,
         xlims = (0, 2π),
         title = "Spline Interpolation")

interp1 2

Interpolation without specifying points

Details

Let’s define a set of function values.

v = [0, 1.41, 2, 1.41, 0, -1.41, -2, -1.41, 0]

Let’s define a set of query points that fall within the interval between the default points., 1:9. In this case, the default points are — 1:9 because v contains 9 values.

xq = 1.5:8.5

Let’s evaluate v in points xq.

import EngeeDSP.Functions: interp1

vq = interp1(v, xq)

Let’s plot the result graph.

plot(1:9, v,
     seriestype = :scatter,
     markershape = :circle,
     label = "v")
plot!(xq, vq,
      seriestype = :scatter,
      markershape = :diamond,
      label = "vq")

interp1 3

Interpolation of complex values

Details

Let’s define a set of sample points.

x = 1:10

Define the values of the function at the sample points.

v = 5*x + x.^2*1im

Let’s define the query points for a more accurate selection in the range x.

xq = 1:0.25:10

Interpolating v at the request points.

import EngeeDSP.Functions: interp1

vq = interp1(x, v, xq)

We will represent the real part of the result in red, and the imaginary part in blue.

plot(x, real(v),
     seriestype = :scatter,
     color = :red,
     markershape = :diamond)
plot!(xq, real(vq), color = :red)
plot!(x, imag(v),
     seriestype = :scatter,
     color = :blue,
     markershape = :diamond)
plot!(xq, imag(vq), color = :blue)

interp1 4

Extrapolation using two different methods

Details

Let’s define the sampling points x and the corresponding sample values v.

x = [1, 2, 3, 4, 5]
v = [12, 16, 31, 10, 6]

Specify the request points xq which go beyond the scope of definition x.

xq = [0, 0.5, 1.5, 5.5, 6]

Let’s evaluate v at the point xq using the method "pchip".

import EngeeDSP.Functions: interp1

vq1 = interp1(x, v, xq, "pchip")
5-element Vector{Float64}:
 19.36842105263158
 13.631578947368421
 13.210526315789474
  7.48
 12.559999999999999

Next, let’s evaluate v at the point xq using the method "linear".

vq2 = interp1(x, v, xq, "linear")
5-element Vector{Float64}:
 NaN
 NaN
  14.0
 NaN
 NaN

Now we use the method "linear" with the option "extrap".

vq3 = interp1(x, v, xq, "linear", "extrap")
5-element Vector{Float64}:
  8.0
 10.0
 14.0
  4.0
  2.0

"pchip" extrapolates by default, and "linear" - no.

Assigning a constant value to all queries outside the scope of x definition

Details

Let’s define the sampling points x and the corresponding sample values v.

x = [-3, -2, -1, 0, 1, 2, 3]
v = 3*x.^2

Specify the request points xq which go beyond the scope of definition x.

xq = [-4, -2.5, -0.5, 0.5, 2.5, 4]

Now let’s evaluate v in points xq using the method "pchip", and assign to any value outside the scope of definition x meaning 27.

import EngeeDSP.Functions: interp1

vq = interp1(x, v, xq, "pchip", 27)
6-element Vector{Float64}:
 27.0
 18.65625
  0.9375
  0.9375
 18.65625
 27.0

Interpolating multiple data sets in one go

Details

Let’s define the sampling points.

x = (-5:5)

Let’s choose three different parabolic functions at the points defined in x.

v1 = x.^2
v2 = 2*x.^2 .+ 2
v3 = 3*x.^2 .+ 4

Create a matrix v, the columns of which are vectors v1, v2 and v3.

v = [v1 v2 v3]

Define a set of query points xq for more accurate sampling in the range x.

xq = -5:0.1:5

Let’s evaluate all three functions in points xq and let’s plot the results.

import EngeeDSP.Functions: interp1

vq = interp1(x, v, xq, "pchip")

plot(x, v, seriestype = :scatter, markershape = :circle)
plot!(xq, vq, xticks = (-5:5))

interp1 5

The circles on the graph indicate v, and the solid lines denote vq.

Additional Info

Akim and spline interpolation

Details

Akim’s algorithm for one-dimensional interpolation, described in [1] and [2], performs cubic interpolation to obtain piecewise polynomials with continuous first-order derivatives ( ). The algorithm preserves the slope and avoids undulation on flat areas. A flat section occurs when there are three or more consecutive collinear points that the algorithm connects with a straight line. To ensure that the area between two data points is flat, insert an additional data point between them.

When two flat areas with different slopes intersect, a modification of Akim’s original algorithm gives more weight to the side whose slope is closer to zero. This modification gives priority to the side closer to the horizontal, which is more intuitive and avoids overshooting. (Akim’s original algorithm gives equal weights to the points on both sides, thereby evenly distributing the undulation.)

The Spline algorithm, on the other hand, performs cubic interpolation to obtain piecewise polynomials with continuous second-order derivatives ( The result is comparable to conventional polynomial interpolation, but less susceptible to strong fluctuations between data points for high degrees. However, this method may be subject to outliers and fluctuations between data points.

Compared to the spline algorithm, Akim’s algorithm creates fewer wave-like oscillations and is better suited to handle rapid changes between flat areas.

Literature

  1. Akima, Hiroshi. «A new method of interpolation and smooth curve fitting based on local procedures.» Journal of the ACM (JACM), 17.4, 1970, pp. 589–602

  2. Akima, Hiroshi. «A method of bivariate interpolation and smooth surface fitting based on local procedures.» Communications of the ACM, 17.1, 1974, pp. 18–20.