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

Is spline interpolation better than linear interpolation? ›

Linear interpolation gives the highest average difference, while spline interpolation gives a somewhat lower difference than polynomials. We also notice that a polynomial of degree 4 has a larger average error than a polynomial of degree 3.

What is the formula for 1D linear interpolation? ›

Performs 1D linear interpolation of 'xi' points using 'x' and 'y', resulting in 'yi', following the formula yi = y1 + (y2-y1)/(x2-x1)*(xi-x1).

What is the easiest method for solving interpolation? ›

One of the simplest methods is linear interpolation (sometimes known as lerp). Consider the above example of estimating f(2.5). Since 2.5 is midway between 2 and 3, it is reasonable to take f(2.5) midway between f(2) = 0.9093 and f(3) = 0.1411, which yields 0.5252.

What is linear spline interpolation? ›

Linear spline interpolation still uses data only from the two consecutive data points, and data from other points is not used at all. Also, at the interior points of the data, the slope of the spline changes abruptly, which implies that the first derivative is “artificially” not continuous at these points.

Which interpolation is most accurate? ›

Radial Basis Function interpolation is a diverse group of data interpolation methods. 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.

Which interpolation formula is better? ›

Average of the maximum percentage error for the functions in Newton's interpolating polynomial and Lagrange's interpolating polynomial are 765.3107 and 898.9139 respectively. This indicates that the Newton's interpolating polynomial is approximately 1.174574 times better than the Lagrange's interpolating polynomial.

Which produces smoother interpolation? ›

Which produces smoother interpolants? Explanation: Polynomial interpolation and spline interpolation produces smoother interpolants.

Is linear interpolation easy? ›

Linear interpolation has been used since antiquity for filling the gaps in tables. Suppose that one has a table listing the population of some country in 1970, 1980, 1990 and 2000, and that one wanted to estimate the population in 1994. Linear interpolation is an easy way to do this.

What are the 3 formulas for 1D motion? ›

The equations are as follows:
  • v = u + at. Here, v = final velocity. u = initial velocity. ...
  • S = ut + (½)at 2 In this equation, S = displacement. u = initial velocity. ...
  • v 2 = u 2 + 2aS. v = final velocity. u = initial velocity. ...
  • S n = u + (a/2)(2n – 1) S n = displacement in the n th second of motion.
  • a = (S 2 – S 1 )/n 2
Jul 31, 2023

What is the fastest interpolation method? ›

The Nearest Point interpolation method is the fastest of all the interpolation methods when used with point data (fig. 19). If used with line or polygon data it can be slower than the Nearest interpolation especially if many of the object vertices lie outside the grid.

What is the simplest method of interpolation? ›

One of the simplest methods, linear interpolation, requires knowledge of two points and the constant rate of change between them. With this information, you may interpolate values anywhere between those two points.

What are the disadvantages of interpolation? ›

It is important to notice that the IDW interpolation method also has some disadvantages: the quality of the interpolation result can decrease, if the distribution of sample data points is uneven. Furthermore, maximum and minimum values in the interpolated surface can only occur at sample data points.

Why is spline interpolation better? ›

Spline interpolation avoids the problem of Runge's phenomenon, which occurs when the interpolating uses high degree polynomials. This can only be achieved if polynomials of degree 3 or higher are used. The classical approach uses polynomials of degree 3, which is the case of cubic splines.

What is the spline formula? ›

Linear splines

The linear spline represents a set of line segments between the two adjacent data points (Vk,Ik) and (Vk+1,Ik+1). The equations for each line segment can be immediately found in a simple form: Ik(V) = Ik + ( Ik+1 - Ik) ( V - Vk ) / (Vk+1 - Vk), where V = [Vk,Vk+1] and k = 0,1,...,(n-1).

What are the disadvantages of spline interpolation? ›

When the sample points are close together and have extreme differences in value, Spline interpolation doesn't work as well. This is because Spline uses slope calculations (change over distance) to figure out the shape of the flexible rubber sheet.

What are the advantages of spline interpolation? ›

Spline interpolation is preferred over polynomial interpolation because the interpolation error can be made small even when using low degree polynomials for the spline. Spline interpolation avoids the problem of Runge's phenomenon, which occurs when the interpolating uses high degree polynomials.

Which spatial interpolation method is best? ›

Spatial Interpolation is the same general function, except the predicted values are influenced by the geographic proximity - or distance - to known values. Two of the most popular spatial interpolation methods are Inverse Distance Weighting (IDW) and Kriging.

What is the best image interpolation method? ›

BICUBIC INTERPOLATION

Since these are at various distances from the unknown pixel, closer pixels are given a higher weighting in the calculation. Bicubic produces noticeably sharper images than the previous two methods, and is perhaps the ideal combination of processing time and output quality.

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.

References

Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5799

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.