1D interpolation in nearest, linear or spline mode (2024)


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');clfh = plot(xx, [yy1 yy2 yy3], x, y, '.')h(1).mark_size = 8;title "Interpolation of a poorly sampled sin() function" fontsize 3legend(['nearest','linear','spline','nodes'], "in_lower_left");

1D interpolation in nearest, linear or spline mode (1)

See also

  • interp — cubic spline evaluation function
  • splin — cubic spline interpolation
  • linear_interpn — n dimensional linear interpolation

History

VersionDescription
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,$).
Report an issue
<< interp Interpolation interp2d >>
1D interpolation in nearest, linear or spline mode (2024)

FAQs

What is the difference between linear interpolation and spline interpolation? ›

Linear interpolation uses a linear function for each of intervals [xk,xk+1]. Spline interpolation uses low-degree polynomials in each of the intervals, and chooses the polynomial pieces such that they fit smoothly together. The resulting function is called a spline.

What is the difference between linear and nearest interpolation? ›

The nearest neighbor algorithm interpolates on the basis of a single point. The linear interpolation algorithm interpolates on the basis of the two nearest points.

When to use spline interpolation? ›

This method is best for generating gently varying surfaces such as elevation, water table heights, or pollution concentrations. The basic form of the minimum curvature Spline interpolation imposes the following two conditions on the interpolant: The surface must pass exactly through the data points.

What type of interpolation is spline? ›

The most common types of spline interpolation used are linear, quadratic, and cubic.

When not to use linear interpolation? ›

Linear interpolation is often not accurate for non-linear data. If the points in the data set change by a large amount, linear interpolation may not give a good estimate.

Which interpolation method is the best and why? ›

In terms of the ability to fit your data and produce a smooth surface, the Multiquadric method is considered by many to be the best. All of the Radial Basis Function methods are exact interpolators, so they attempt to honor your data.

Which interpolation formula is better? ›

If linear interpolation formula is concerned then it can be used to find the new value from the two given points. If we compare it to Lagrange's interpolation formula, the “n” set of numbers is needed. Thereafter Lagrange's method is to be used to find the new value.

What is better than linear interpolation? ›

Accuracy. If a C0 function is insufficient, for example if the process that has produced the data points is known to be smoother than C0, it is common to replace linear interpolation with spline interpolation or, in some cases, polynomial interpolation.

How to know which interpolation to use? ›

Linear Interpolation: This method requires a straight line or curve between two points. It is used when there is an exact relationship between the values, but no data points are available. Nearest Neighbor Interpolation: This method uses the closest known value to predict the value between two known values.

What is the problem with spline interpolation? ›

The cubic splines interpolation algorithm does not work well for interpolation when the x values are large and have a large distance between them. Under these circ*mstances, cubic splines interpolation becomes very unstable making interpolations incorrect by many orders of magnitude.

Why is spline better? ›

In interpolating problems, spline interpolation is often preferred to polynomial interpolation because it yields similar results, even when using low degree polynomials, while avoiding Runge's phenomenon for higher degrees.

What is the difference between spline and IDW interpolation? ›

Unlike IDW, spline can estimate values above and below the min and max values of your sample points. Thus it is good for estimating high and low values not already represented in your data. For visualizations of Spline interpolation, see Jochen Albrecht's Spline 3D Concepts Lecture.

When to use splines? ›

Splines can be seen as non-parametric interpolation or fitting tools. So, the ideal application would be a case where you don't have a model to describe the variable but need to either interpolate it or produce a smooth version of the data. Splines are often used in conjunction with other methods.

How do you calculate spline interpolation? ›

In cubic spline interpolation (as shown in the following figure), the interpolating function is a set of piecewise cubic functions. Specifically, we assume that the points (xi,yi) and (xi+1,yi+1) are joined by a cubic polynomial Si(x)=aix3+bix2+cix+di that is valid for xi≤x≤xi+1 for i=1,…,n−1.

How to differentiate between interpolation spline and approximation spline? ›

A curve that actually passes through each control point is called an interpolating curve; a curve that passes near to the control points but not necessarily through them is called an approximating curve.

What is the difference between linear quadratic and cubic spline interpolation? ›

The linear spline function has derivatives that are constant on each subinterval. The quadratic spline gives derivatives that are not smooth at the data points. The cubic spline provides smooth derivatives that are very close to the actual derivative of the Runge function that was used to generate the data!

What is the difference between linear interpolation and circular interpolation? ›

Linear interpolation: linear paths are calculated from a given starting and ending position. Circular interpolation: a circular path is calculated with the help of given coordinates (usually center and radius / diameter).

What are the two main types of interpolation approach? ›

There are several formal kinds of interpolation, including linear interpolation, polynomial interpolation, and piecewise constant interpolation.

References

Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6018

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.