How to get multiple interpolated Y values from many X’s

Have you ever wanted to bunch up all Y values corresponding to multiple X values from multiple datasets (plots) at one time using interpolation (or extrapolation) technique like in the following sample graph? One way to achieve such task is to apply the “Vertical Cursor” gadget multiple times as discussed in the forum post, http://www.originlab.com/forum/topic.asp?TOPIC_ID=20330 .

Although this is a handy interactive tool, if you have many specific X values to interpolate, you can try the following methods of interpolation applied on the worksheet data.

This method is only applicable when your desired X positions are equally spaced in a range. You can run this from “Analysis> Mathematics> Interpolate/Extrapolate” menu, then you enter the X Minimum and Maximum, as well as the Number of Points (after turning OFF the Auto check boxes). For example, you can enter: X Minimum=0, X Maximum=10, and Number of Points=11 , to produce Y values at 0, 1, 2, … , 10.

CASE-2: Using the “Interpolate/Extrapolate Y from X” menu

This method is useful when the number of source datasets are not many, but they may exist in various workbooks or worksheets. As a preparation of the procedure, first make a worksheet which has a column containing your desired X values to interpolate. For example, in the screenshot below, a new sheet “Results” is created, and you enter the X values to interpolate into the first column, A(X). (You only need this X column at this moment, so you may delete the empty B(Y).)

Now you can run the interpolation by “Analysis> Mathematics> Interpolate/Extrapolate Y from X” menu for each of your source Y dataset. You can specify the column for “X Values to Interpolate”. Note that the column part of the “Result of interpolation” has been set to “New;” in the specific worksheet ([Book1]Results in this sample) so that every time you run this menu command, the interpolated result is automatically added as a new column in the Results worksheet. Also, the recalculation mode is set to Auto so that it recalculates automatically when the X values to interpolate in Results worksheet are updated.  Repeat this step for all Y datasets.

CASE-3: Using a Script

If you have lots of source Y datasets to be interpolated, the method in CASE-2 takes tedious steps to interpolate all Y datasets. But, if your source datasets are arranged in a single worksheet with a regular pattern of designations like XYY…Y or XYXY…XY, you can accomplish all tasks in a single step with a help of simple script. For example, the following sample worksheet contains ten(10) Y datasets, and the X values to interpolate are stored in the first column in the second sheet(=Sheet2):

Try to run the following simple script (using “interp1” x-function).:

```range rix=2!col(1); //X column to interpolate
range riy=2!col(2); //as we only need X column in this sheet
if(riy.getSize()==0) delete riy;  //if the 2nd column is empty, delete it.
page.active=1; //Sheet# of data sheet
doc -e DY {
cn=colnum(%C); //next Y dataset
interp1 -r 1 ix:=rix iy:=1!\$(cn) ox:=2!<new> method:=0;
}```

You get the whole result in a single run!

View all posts by Hideo Fujii →

4 Comments on “How to get multiple interpolated Y values from many X’s”

1. Anzar Ali says:

Dear Sir,
I have a worksheet with a regular pattern XYXY……XY. I want to interpolate the whole worksheet like X(0 to 90000 in 901 steps). And finally I want to have a worksheet like XYYYYYY…..Y. Please help me to write the script so that I can do it in a single click.

1. Hideo Fujii says:

Hi Anzar Ali,

Maybe you can write similar to the following using interp1xy for the first XY pair, and interp1 for the rest of XY pairs:
```//////////////////////////////////// nc=wks.ncols; //number of columns of XYXY interp1xy iy:=Col(2) npts:=100 xmin:=0 xmax:=1 oy:=(,); //interp to produce new XY xpos=wks.ncols-1; //X position for(ii=4; ii<=nc; ii=ii+2) { //Go through Ys from 2nd Y at col(4) to the end interp1 ix:=wcol(xpos) iy:=wcol(ii) ox:=; //Add interpolated Y using X from interp1xy result } ////////////////////////////////////```

Did it work?

–Hideo Fujii
OriginLab

2. purpleblitz says:

Hi, please could you give more detail on how the script is written and executed? Thanks! 🙂

1. Hideo Fujii says:

Hi purpleblitz,
You can copy-and-paste the script code in this article into the Script window (“Window: Script Window” menu to open), highlighted all pasted lines, then press ENTER key to run the block of lines. Also, you can save this code in a script file (a text file which has .OGS extension), and later run it by “run.section” command. Please refer to the following explanation:
http://www.originlab.com/doc/LabTalk/guide/Running-Scripts
Hope this helps.
–Hideo Fujii, Originlab