5.8 Mathematical Analysis with LabTalk
Summary
Many mathematical analysis tools in Origin is accessible from LabTalk script, for example, differentiation, integration, interpolation, averaging multiple curves .etc. Most of these analysis has one or more associated X-Functions, and many commands, objects or arithmetic operators work as an implement to these X-Functions.
When carrying out mathematical analysis, it is sometimes very useful to create a user-interactive dialog to get user input and use the input value(s) for further calculation.
What You Will Learn
This tutorial will show you how to:
- subtract a line from source data
- create a user-interactive dialog (getn) to get input from user and make use of the input values
- use interpolation to compute the Y value for given X
- normalize a data column
- calculate average for multiple curves
Steps
- Import the data file Waterfall.dat under <Origin EXE folder>\Samples\Graphing folder with the associated import filter into the workbook Waterfall.
- Use a loop and the subtract_line X-Function to subtract a simple baseline of end points connection.
- Use a getn dialog to get some user input, let user decide whether to use the peak height at a reference position, or simply the maximum peak height for normalization.
- Based on the user input from the previous step, loop over all Y columns to do data normalization.
- Plot the normalized data as grouped line plots.
- Calculate the average of multiple curves with the avecurves X-Function.
- Plot the average curve results as another grouped line plot.
// Create a newbook and name it Waterfall (both long and short name) newbook name:="Waterfall" option:=lsname; // Define the file name and filter name string fname$ = system.path.program$ + "Samples\Graphing\Waterfall.dat"; string filtername$ = "waterfall.oif"; // Use the impfile x-function to import the specified file with the import filter impfile fname:=fname$ filtername:=filtername$ location:=data orng:=[Waterfall]1!;
//Create new sheet named as SubtractedData newsheet name:=SubtractedData; //Define the newly created worksheet(current active) as a range variable range sdwk = !; //Active the "Waterfall" worksheet by its name page.active$ = "Waterfall"; //Define a integral to hold the number of columns in Waterfall sheet int nc = wks.nCols; //Set the number of columns in SubtractedData sheet to be the same sdwk.nCols = nc; //Use the loop and subtract_line x-function to subtract baseline //The first and last data points are found and connected linearly //This line will be subtracted as baseline from source data //Subtracted data will be named by excitation wavelength of source data loop(ii, 2, nc) { range rx = [Waterfall]Waterfall!col(1); range ry = [Waterfall]Waterfall!col($(ii)); subtract_line iy:=[Waterfall]Waterfall!(1,$(ii)) x1:=rx[1] /* Specifies the X value of the first point*/ y1:=ry[1] /* Specifies the Y value of the first point*/ x2:=rx[$(wks.maxRows)] /* Specifies the X value of the last point*/ y2:=ry[$(wks.maxRows)] /* Specifies the Y value of the last point*/ oy:=[Waterfall]SubtractedData!(1,$(ii)); //Rename the column long names in SubtractedData sheet //Use the User Defined Parameter Wavelength in Waterfall sheet [Waterfall]SubtractedData!col($(ii))[L]$ = [Waterfall]Waterfall!col($(ii))[D1]$ + "nm"; }
//Create a dialog to get user input of reference peak position X value double xx;//give an initial value //Create getn dialog int n; getn (Use Specified Reference Peak for Normalization) n:2s (Reference Peak Position X Value) xx (If not specified, the data will be normalized based on maximum Y value);
//Create a new worksheet Normalized //And set the number of columns to be the same as previous worksheets newsheet name:=Normalized cols:=nc outname:=sname1$; //Fill in column A with the same data of column A in SubtractedData worksheet range r1 = col(1); range r2 = [Waterfall]SubtractedData!col(1); r1 = r2; if(n==0)/*If user hasn't chosen "Use Specified Reference Peak for Normalization"*/ { loop(ii, 2, nc) { //Use the rnormalize x-function to normalize the columns rnormalize -r 2 irng:=[Waterfall]SubtractedData!col($(ii)) method:=max /*Use "Divided by Maximum" as Normalization method" */ orng:=%(sname1$)col($(ii)); wks.col = ii; col($(ii))[L]$ = [Waterfall]Waterfall!col($(ii))[D1]$ + "nm"; } } if(n==1)/*If user has chosen "Use Specified Reference Peak for Normalization"*/ { loop(ii, 2, nc) { range rr=[Waterfall]SubtractedData!(1,$(ii)); //Use interpolation to compute the Y value for given X double yy = rr(xx); //Use the rnormalize x-function to normalize the columns rnormalize -r 2 irng:=[Waterfall]SubtractedData!col($(ii)) method:=specify /*Use "Divided by a specified value" as Normalization method" */ val:=yy /*Use the computed Y value from interpolation as reference value*/ orng:=%(sname1$)col($(ii)); wks.col = ii; col($(ii))[L]$ = [Waterfall]Waterfall!col($(ii))[D1]$ + "nm"; } }
Except for the range( ) syntax, several X-Functions can be used for interpolation, e.g. interp1(interpolate Y from X), interp1q(linear interpolation and extrapolation), interp1xy(1D interpolation/extrapolation on a group of XY data).etc |
//Plot the normalized data as grouped line plots, no legend plotxy iy:=(1,2:end) plot:=200 legend:=0;
win -a Waterfall; //Create new sheet named as Avecurve newsheet name:=Avecurve; //Use a loop to calculate the average for every 4 curves for(ii=2; ii<nc; ii=ii+4) { avecurves -r 2 iy:=%(sname1$)(1,$(ii):$(ii+4)) rd:=[Waterfall]Avecurve!col($(ii/2)); }
//Select the whole worksheet of Avecurve worksheet -s 0 0 0 0; //Plot the active range as grouped line plot plotxy plot:=200 legend:=0;