2.13.1.4 Interpolation

Interpolation is one of the more common mathematical functions performed on data, and Origin supports interpolation in two ways: (1) interpolation of single values and datasets through range notation and (2) interpolation of entire curves by X-Functions.


Interpolation at Given Index Values

The following example show how to perform interpolation at given index values to find interpolated y values.

// Using datasets
dataset d1={10,20,30,40,50,60,70,80,90,100};
dataset d2={3,7,2,6,8};
dataset d3;
d3=d1(d2);    // Perform interpolation of d1 at index values from d2
type $(d3); //d3={30,70,20,60,80};

// Using ranges
newbook;
wks.ncols = 3;
range r1 = 1;  // Column 1 in active worksheet
r1 = {10,20,30,40,50,60,70,80,90,100};
range r2 = 2;  // Column 2 in active worksheet to put index values;
r2 = {3,7,2,6,8};
range r3 = 3;  // Column 3 in active worksheet;
r3 = r1(r2); 

// Using columns
col(3)=col(1)(col(2));

Finding Interpolated Y at Given X Values

Using XY Range

An XY Range (subrange specified by X values is available) once declared can be used as a function. The argument to this function can be a scalar - which returns a scalar - or a vector - which returns a vector. In either case, the X dataset should be increasing or decreasing. For example:

newbook;
wks.ncols = 4;
col(1) = data(1,0,-.05);
col(2) = gauss(col(1),0,.5,.2,100);
range rxy = (1,2);
rxy(.67)=;
range newx = 3; // Use column as X column data
newx = {0, 0.3333, 0.6667, 1.0}; // Create our new X data
range newy = 4; // This is the empty column we will interpolate into
newy = rxy(newx);

You can then use such range variables as a function with the following form:

XYRangeVariable(RangeVariableOrScalar[,connect[,param]])

where connect is one of the following options:

line
straight line connection
spline
spline connection
bspline
b-spline connection

and param is smoothing parameter, which applies only to bspline connection method. If param=-1, then a simple bspline is used, which will give same result as bspline line connection type in line plots. If 'param >=0, the NAG nag_1d_spline_function is used.

Notes: When using XY range interpolation, you should guarantee there are no duplicated x values if you specify spline or bspline as the connection method. Instead, you can use interpolation X-Functions.

From Worksheet Data

The following examples show how to perform interpolation using range as function, with data from a worksheet as the argument.

Example1: The following code illustrates the usage of the various smoothing parameters for bspline:

col(1)=data(1,9);              // Fill column 1 with row data
col(2)=normal(9);              // Fill column 2 with random values
col(3)=data(1,9,0.01);         // Fill Col(3) with desired X values
wks.col3.type = 4;             
range bb=(1,2);                // Declare range using cols 1,2;
// Compute interpolated values using different parameter settings
loop(i, 4, 10) {
   wcol(i)=bb(col(3), bspline, $(i*0.1));
}

Example2: With an XY range, new Y values can be obtained at any X value using code such as:

// Generate some data
newbook;
wcol(1)={1, 2, 3, 4};
wcol(2)={2, 3, 5, 6};
// Define XYrange
range rr =(1,2);
// Find Y value by linear interpolation at a specified X value.
rr(1.23) = ;    // ANS: rr(1.23)=2.23
// Find Y value by linear interpolation for an array of X values.
wcol(3)={1.5, 2.5, 3.5};
range rNewX = col(3);
// Add new column to hold the calculated Y values
wks.addcol();
wcol(4) = rr(rNewX);

Example3: To find X values given Y values, simply reverse the arguments in the examples above. In the case of finding X given Y, the Y dataset should be increasing or decreasing.

// Generate some data
newbook;
wcol(1)={1, 2, 3, 4};
wcol(2)={2, 3, 5, 6};
// Define XYrange
range rr =(2,1); //swapping the X and Y
// Find X value by linear interpolation at a specified Y value.
rr(2.23) = ;    // ANS: rr(2.23)=1.23;
// Add new column to hold the calculated X values
wks.addcol();
range rNewX = wcol(3);
// Find X value by linear interpolation for an array of Y values:
wcol(4)={2.5, 3.5, 5.5};
range rNewY = wcol(4);
rNewX = rr(rNewY);

From Graph

You can also use range interpolation when a graph page is active.

Example 1: Interpolate an array of values.

// Define range on active plot:
range rg = %C; 
// Interpolate for a scalar value using the line connection style:
rg(3.54)=;
// Interpolate for an array of values:
// Give the location of the new X values:
range newX = [Book2]1!1;
// Give the location where the new Y values (output) should go:
range newY = [Book2]1!2;
// Compute the new Y values:
newY = rg(newX);

Example 2: Specify the interpolation method.

// Define range on specific plot:
range -wx rWx = 2;     // Use X of 2nd plot in active layer
range -w rWy = 2;      // Use Y of 2nd plot in active layer
range rr = (rWx,rWy);  // Construct an XY range from two ranges
// Give the location where the new X values (output) should go:
range newX = [Book2]1!1;
newX = {5,15,25};
range newY1 = [Book2]1!2; // Range for new Y
range newY2 = [Book2]1!3; // Range for new Y
// Find new Y values by linear interpolation for an array of X values:
newY1 = rr(newX);
// Find new Y values by bspline interpolation for an array of X values:
newY2 = rr(newX,bspline);

Using Arbitrary Dataset

For two arbitrary datasets with the same length, where both are increasing or decreasing, Origin allows you to interpolate from one dataset to the other at a given value. The datasets can be a range variable, dataset variable, or column. The form to perform such interpolation is:

dataset1(value, dataset2)

which will perform interpolation on the group of XY data constructed by dataset2 and dataset1, and it will return the so-called Y (dataset1) value at the given so-called X (dataset2) value. For example:

// Using datasets
dataset ds1 = {1, 2, 3, 4};
dataset ds2 = {2, 3, 5, 6};
// Return interpolated value in ds2 where X in ds1 is 1.23
ds2(1.23, ds1) = ;  // Return 2.23
// Return interpolated value in ds1 where X in ds2 is 5.28
ds1(5.28, ds2) = ;  // Return 3.28

// Using ranges
newbook;
wks.ncols = 3;
range r1 = 2;  // Column 2 in active worksheet
r1 = {1, 2, 3, 4};
range r2 = 3;  // Column 3 in active worksheet;
r2 = {2, 3, 5, 6};
r2(1.23, r1) = ;
r1(5.28, r2) = ;

// Using columns
col(3)(1.23, col(2)) = ;
col(2)(5.28, col(3)) = ;

Creating Interpolated Curves

X-Functions for Interpolation of Curves

Origin provides three X-Functions for interpolating XY data and creating a new output XY data pair:

Name Brief Description
interp1xy

Perform interpolation of XY data and generate output at uniformly spaced X

interp1

Perform interpolation of XY data and generate output at a given set of X values

interp1trace

Perform interpolation of XY data that is not monotonic in X

Using Existing X Dataset

The following example shows how to use an existing X dataset to find interpolated Y values:

// Create a new workbook with specific column designations
newbook sheet:=0;
newsheet cols:=4 xy:="XYXY"; 
// Import a sample data file
fname$ = system.path.program$ + "Samples\Mathematics\Interpolation.dat";
impasc;

// Interpolate the data in col(1) and col(2) with the X values in col(3)
range rResult=col(4);
interp1 ix:=col(3) iy:=(col(1), col(2)) method:=linear ox:=rResult;

//Plot the original data and the result
plotxy iy:=col(2) plot:=202 color:=1;
plotxy iy:=rResult plot:=202 color:=2 size:=5 ogl:=1;

Uniformly Spaced X Output

The following example performs interpolation by generating uniformly spaced X output:

//Create a new workbook and import a data file
fname$ = system.path.program$ + "Samples\Mathematics\Sine Curve.dat";
newbook;
impasc;

//Interpolate the data in column 2
interp1xy iy:=col(2) method:=bspline npts:=50; 
range rResult = col(3);

//Plot the original data and the result
plotxy iy:=col(2) plot:=202 color:=1;
plotxy iy:=rResult plot:=202 color:=2 size:=5 ogl:=1;

Interpolating Non-Monotonic Data

The following example performs trace interpolation on data where X is not monotonic:

//Create a new workbook and import the data file
fname$ = system.path.program$ + "Samples\Mathematics\circle.dat";
newbook;
impasc;

//Interpolate the circular data in column 2 with trace interpolation
interp1trace iy:=Col(2) method:=bspline; 
range rResult= col(4);

//Plot the original data and the result
plotxy iy:=col(2) plot:=202 color:=1;
plotxy iy:=rResult plot:=202 color:=2 size:=1 ogl:=1;

Note that the interpolation X-Functions can also be used for extrapolating Y values outside of the X range of the input data.

Matrix Interpolation

The minterp2 X-Function can be used to perform interpolation/extrapolation of matrices.

// Create a new matrixbook and import sample data;
newbook mat:=1;
filepath$ = "Samples\Matrix Conversion and Gridding\Direct.dat";
string fname$=system.path.program$ + filepath$;
impasc;
// Interpolate to a matrix with 10 times the x and y size of the original
range rin = 1; // point to matrix with input data;
int nx, ny;
nx = rin.ncols * 10;
ny = rin.nrows * 10;
minterp2 method:=bicubic cols:=nx rows:=ny ;

OriginPro also offers the interp3 X-Function which can be used to perform interpolation on 4-dimensional scatter data.

Origin 2022 makes it easier to get 2D interpolation Z values from a named range

range mm=1!1;
mm[1,1]=;   //to get element by index
mm(1.5, 1.7)=;    // return interpolated value, matrix XY mapping
mm(1.5, 1.7, spline)=; //3rd argument supports same items as XF minterp2