4.5.4 Peaks AnalysisPeaks-and-Baseline
Integrate Active Curve in Graph over a Specific X Range
The following example shows how to integrate the active curve in a graph over a specific range of x values, using the integ1 X-Function.
// Purpose: This example demonstrates the following:
// 1. import a file
// 2. create a graph
// 3. integrate the curve over a specific x range
// Create new book and import a sample file
newbook;
string fname$ = system.path.program$ + "samples\curve fitting\multiple peaks.dat";
impasc;
// Plot 4th column against 1st column as a line plot
plotxy iy:=(1,4) plot:=200;
// Get row index for x values of 6.5 and 9.5, which covers the 3rd peak in the graph
int ix1=xindex(6.5,%c);
int ix2=xindex(9.5,%c);
// Integrate this range of the curve, and list the results of integration
range rr=(%c)[$(ix1): $(ix2)];
integ1 rr;
integ1.=;
Subtract Baseline and Integrate Peaks
The following example process a single file but it can be easily modify to loop through to process multiple files.
// Purpose: This example demonstrates the following:
// 1. import a file
// 2. find and subtract baseline
// 3. find peaks
// 4. integrate each peaks
// 5. put integration area of each peak to append to a summary sheet
//clean the project to start with empty space
doc -s;doc -n;{win -cd};
// Create new book to save results from analysis of multiple peaks
newbook name:="Integ Peaks" sheet:=0;
string result$ = %h;
newsheet cols:=5 xy:="NXYXY" name:="Results";
// Define ranges for all cols and give names to the columns
range rFilename=1, rPkindex=2, rArea=3, rCenter=4, rHeight=5;
rFilename[L]$ = "File Name";
rPkindex[L]$ = "Peak Index";
rArea[L]$ = "Area";
rCenter[L]$ = "Center";
rHeight[L]$ = "Height";
// filename col need to be wider
wcolwidth irng:= col(1) width:=20;
// Find data files
fname$= system.path.program$ + "Samples\Spectroscopy\Peaks with Base.dat";
newbook;// use a new book to store data, so results book can be in separate book
//impfile filtername:="ASCII.oif";
impASC;
// we will use only the 1st two col
wks.ncols = 2;
range baseline=3;// put before anchor XY so we can share data's X of col(1)
range subtracted=4;// data subtracted to find peaks and integrate
range -x anchors = (5,6);
//columns to hold peak finding results
range pcenter=7, pleft=8, pright=9;
// Find baseline anchor points to put into col(5),(6)
blauto iy:=(1,2) oy:=anchors;
// Interpolate with Cubic B-Spline to put into col(5)
interp1 ix:=1 iy:=anchors method:=spline ox:=baseline;
baseline[L]$="Baseline";
baseline[C]$="Interpolated from Found Anchor Pts";
//Subtract baseline
subtracted = col(2) - baseline;
subtracted[L]$="Subtracted";
//Find peak
pkfind subtracted ocenter:=pcenter oleft:=pleft oright:=pright;
//loop each peak to compute area
for(int ipeak = 1; ipeak <= pcenter.GetSize(); ipeak++)
{
int n1 = pleft[ipeak];
int n2 = pright[ipeak];
range rint = subtracted[$(n1):$(n2)]; // range of data to be integrated
double aa, x0, y0;
integ1 rint oy:=<optional> area:=aa x0:=xx y0:=yy;
rFilename[ipeak]$ = %(page.info.system.import.filename$);
rPkindex[ipeak] = ipeak;
rArea[ipeak] = aa;
rCenter[ipeak] = xx;
rHeight[ipeak] = yy;
}
Process Multiple DSC Files to Find and Integrate Peaks
In this example, we find all files under a sample subfolder to loop over each and process them and generate a summary sheet.
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Purpose: This example demonstrates the following:
// 1. Import files with dsc extension
// 2. Loop through each data to : find and subtract baseline;
// find peaks; integrate peaks; put integration area and other result in a separate worksheet
// 3. Sort summary sheet by indices of peaks.
//
// Note that this code is in 'OGS' style with various sections that can be independently accessed.
// Save the code as a file with OGS extension to your User Files Folder.
// Run with run.section(filename,Main)
// OR
// Type DIR *.OGS<Enter> to scan for OGS files then type filename<Enter> to run.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
[Help]
type Load one more files with dsc extension and then integrate area of each peak.;
type One argument : %1 = number of files to load, 0 to load all.;
[Main]
int nfiles = %1;
string LoadDSCogsPath$=system.path.program$ + "Samples\LabTalk Script Examples\LoadDSC.ogs";
%A=LoadDSCogsPath$;
if(!run.section(%A, Main, nfiles))
return 0;
// load success, data should be loaded into active book
string dscBook$=%H;
if(dscBook.GetLength() < 1 )
{
type "new book " + dscBook$ + " is not valid";
return 0;
}
nfiles = page.nLayers;
// Create new sheet for summary report and define ranges
newsheet cols:=8 xy:="LYYYYYYY" name:="Summary";
range rColName = 1, rIndex = 2, rCntrInd = 3, rLtInd = 4, rRtInd = 5;
range rArea = 6, rCntr = 7, rHt = 8;
rColName[L]$ = "Data Name";
rIndex[L]$ = "Peak Index";
rCntrInd[L]$ = "Peak Center Index";
rLtInd[L]$ = "Peak Left Index";
rRtInd[L]$ = "Peak Right Index";
rArea[L]$ = "Peak Area";
rCntr[L]$ = "Peak Center";
rHt[L]$ = "Peak Height";
for(int ii = 1; ii<= nfiles; ii++)
{
if(!run.section(, IntegOneData, $(ii)))
return 0;
}
page.active = $(page.nLayers);
dataset sbc = {2}; //Select indecies of peaks for the primary sort order
dataset sodr= {1}; //Ascending
wsort nestcols:=sbc order:=sodr; //Perform the sorting
return 1;// for success
[IntegOneData]
int sheet = %1;
page.active = $(sheet);
range rinput = $(sheet)!2;
string strData$ = rinput[C]$;
//alloc space for baseline and subtracted with same X of data
wks.AddCol();
int n = wks.nCols;
range baseline = $(n);
baseline[L]$="Baseline";
baseline[C]$="Interpolated from Found Anchor Pts";
wks.AddCol();
n = wks.nCols;
range subtracted = $(n);
subtracted[L]$="Subtracted";
// Find baseline anchor points
range -x anchors = (AnchorX, AnchorsY);
blauto iy:=rinput oy:=anchors;
// Interpolate with Spline
interp1 ix:=1 iy:=anchors method:=spline ox:=baseline;
//Subtract baseline
subtracted = rinput - baseline;
//columns to hold peak finding results
int nn = wks.ncols;
loop(cc, 1, 7)
{
wks.addcol();
}
//Find peak
range pindex = $(nn+1), pcenter= $(nn+2), pleft= $(nn+3), pright= $(nn+4), parea= $(nn+5), px = $(nn+6), py = $(nn+7);
pkfind subtracted ocenter:=pcenter oleft:=pleft oright:=pright;
//loop each peak to compute area
int nNumPeaks = pcenter.GetSize();
for(int ipeak = 1; ipeak <= nNumPeaks; ipeak++)
{
int n1 = pleft[ipeak];
int n2 = pright[ipeak];
range rint = subtracted[$(n1):$(n2)]; // range of data to be integrated
double aa, x0, y0;
integ1 rint area:=aa x0:=xx y0:=yy oy:=<optional>;
pindex[ipeak] = ipeak;
parea[ipeak] = aa;
px[ipeak] = xx;
py[ipeak] = yy;
// Copy results for current peak, to the summary sheet
int irow = (($(sheet)-1)*$(nNumPeaks))+$(ipeak) ;
rColName[$(irow)]$ = rinput[C]$;
rIndex[$(irow)] = ipeak;
rCntrInd[$(irow)] = pcenter[ipeak];
rLtInd[$(irow)] = pleft[ipeak];
rRtInd[$(irow)] = pright[ipeak];
rArea[$(irow)] = parea[ipeak];
rCntr[$(irow)] = px[ipeak];
rHt[$(irow)] = py[ipeak];
}
pindex[L]$ = "Peak Index";
pcenter[L]$ = "Peak Center Index";
pleft[L]$ = "Peak Left Index";
pright[L]$ = "Peak Right Index";
parea[L]$ = "Area";
px[L]$ = "Center";
py[L]$ = "Height";
return 1;
Compare Fitting Results of PA
This example shows how to compare a particular peak with its fit after running PA on a series of sample data.
(Please unzip the dialog theme from this zip file to your User Files Folder\Themes\AnalysisAndReportTable\ in advance.)
// Purpose: This example demonstrates the following:
// 1. import a series of dsc files
// 2. auto find and subtract baseline
// 3. find peaks and fit peaks with same setting loading form theme file.
// 4. Plot the fit curve of third peak to the first sheet
// 5. Append primary parameters of this peak under the graph cell.
// 6. At the end, get the comparsion sheet about the same peak in the source data.
path$ = system.path.program$ + "Samples\Spectroscopy\DSC\Data\";
newbook s:=1 result:=bkn$;
int nfrom =3;
int nto=5;
range ww = !;
ww.ncols = nto - nfrom + 1 + 1; //the first column is for label of variable list
//format comparison variables
range rLabel = 1!1;
rLabel[1]$ = "Peak Curve";
rLabel[2]$ = "xc";
rLabel[3]$ = "A";
rLabel[4]$ = "w";
int ii;
loop(ii,nfrom,nto)
{
//1, import two data files in a series
ind$ = $(ii);
filename$ = "tcal"+ ind$ +".dsc";
file$ = path$ + filename$;
newsheet bkn$;
impfile file$ ;
//2, run pa script mode and save result wks name and report tree.
aa$="(1,2)";
pa iy:=aa$ theme:=the3ndpeaks;
//3, get result and make plot of the 3nd peak
string pp, fitc, resid, peak3;
pp$ =__PEAK$;
fitc$ = __FITCURVE$;
resid$ = __RESIDUAL$;
getresults tr:=trRes iw:=__REPORT$;
//4, plot the 3nd peak to sheet1 for comparison
range r_peak = 1!$(ii-1)[1];
peak3$ = fitc$ + "(7,8)";
plotxy iy:=peak3$ plot:=200;
graphname$ = %H;
insertGraph gname:=graphname$ embed:=1 resizecell:=1 cell:=r_peak;
//5, append peak information
range r_info = 1!$(ii-1)[2:4];
r_info[1] = trRes.Parameters.xc__3.Value;
r_info[2] = trRes.Parameters.A__3.Value;
r_info[3] = trRes.Parameters.w__3.Value;
}
page.active=1;
|