Hi triley35,
The Origin C code pasted below will find all negative peaks from all y data columns in active worksheet. It will create a report wks with x, y values of peak locations for each y data in your original wks.
The code has a separate function to find peaks, which is coded to use the LabTalk curve object. The parameters for peak serach correspond to what you will find in the Pick Peaks tool in the GUI.
The code does not create any result graphs, but that sort of thing can be easily added with more Origin C code.
Hope this helps you to get started.
Easwar
OriginLab
////////////////////////////////////////////////////////////////////////////////////
// This is the main function that can be called from Script Window
// or from custom toolbar buttons etc.
// To call from script window, make the worksheet with data active
// and type command in script window, such as:
// find_neg_peaks_all_y
// find_neg_peaks_all_y 0.05 0.05 0.8
// where in the second command above the default values of search parameters
// are changed. These search parameters are same as the ones you find in the
// Pick Peaks GUI tool (menu item Tools->Pick Peaks)
//
void find_neg_peaks_all_y(double dRectHeight = 0.05, double dRectWidth = 0.05, double dMinHeight = 0.05)
{
// Declare Worksheet object using active layer
Worksheet wksData = Project.ActiveLayer();
if( !wksData )
{
out_str("Active Layer is not a worksheet");
return;
}
// Create a new worksheet page for reporting
WorksheetPage wpgResult;
wpgResult.Create("Origin");
string str;
str.Format("Peak report for data from %s", wksData.GetPage().GetName());
wpgResult.Label = str;
wpgResult.TitleShow = WIN_TITLE_SHOW_BOTH;
// Point to worksheet
Worksheet wksResult = wpgResult.Layers(0);
// Delete all columns in default result sheet
while( wksResult.DeleteCol(0) );
// Loop thru all columns of data worksheet
int nYCols = 0;
foreach(Column colData in wksData.Columns)
{
// Check if it is a Y column
if( OKDATAOBJ_DESIGNATION_Y == colData.GetType() )
{
// Add a two new columns to result worksheet for each Y data column
int nIndexX = wksResult.AddCol();
// Set first one as type X
wksResult.Columns(nIndexX).SetType(OKDATAOBJ_DESIGNATION_X);
int nIndexY = wksResult.AddCol();
// Set column label of result cols using name of data col
string strDataColName = colData.GetDatasetName();
int nLabelLength = strDataColName.GetLength();
string strLabel = strDataColName + "\nPeak X";
wksResult.Columns(nIndexX).SetLabel(strLabel);
wksResult.Columns(nIndexX).SetWidth(nLabelLength);
strLabel = strDataColName + "\nPeak Y";
wksResult.Columns(nIndexY).SetLabel(strLabel);
wksResult.Columns(nIndexY).SetWidth(nLabelLength);
// Call function to find negative peaks
int nPeaks = find_neg_peaks(strDataColName, wksResult, nIndexX, dRectHeight, dRectWidth, dMinHeight);
// Increment column count
nYCols++;
}
}
// Turn on display of labels in result wks
wksResult.ShowLabels();
// If no Y cols found in data sheet
if( 0 == nYCols )
{
// Delete result sheet and quit
wpgResult.Destroy();
out_str("No Y columns found in data worksheet");
}
// Delete temp worksheet
//wpgTemp.Destroy();
}
////////////////////////////////////////////////////////////////////////////////////
// This function finds the X values of negative peak locations
// The width and height parameters determine how peaks are located
// See the PickPeaks tool documentation for more info.
static int find_neg_peaks(string strDataColName, Worksheet& wksResult, int nIndexX, double dRectHeight,
double dRectWidth, double dMinHeight)
{
// Declare curve using data column
Curve crvData(strDataColName);
// Multiply curve by -1 to detect negative peaks
crvData *= -1;
// Create a temp dataset to hold peak index values
Dataset dsPkIndex;
dsPkIndex.Create(crvData.GetSize());
dsPkIndex = NANUM;
// Use LabTalk curve object to find peaks
// Place peak results in temp wks
using LTCurve = LabTalk.curve;
// Reset curve object
LTCurve.Reset();
// Assign dataset name
LTCurve.Data$ = strDataColName;
// Assign column to hold peak index in temp wks
LTCurve.PeakIndex$ = dsPkIndex.GetName();
// Set peak search parameters
LTCurve.PickPeaks.RectHeight = dRectHeight;
LTCurve.PickPeaks.RectWidth = dRectWidth;
LTCurve.PickPeaks.MinHeight = dMinHeight;
// Find peaks assuming no baseline
LTCurve.PickPeaks(1);
// Set data curve back to original
crvData *= -1;
// For all peaks found, fill the X, Y values into temp wks cols 2, 3
Dataset dsPkX(wksResult, nIndexX);
Dataset dsPkY(wksResult, nIndexX + 1);
// Remove missing values from peak index dataset and then find size
dsPkIndex.TrimRight();
int nPeaks = dsPkIndex.GetSize();
dsPkX.SetSize(nPeaks);
dsPkY.SetSize(nPeaks);
// Get X data of original data curve
Dataset dsXData;
if( crvData.HasX() )
crvData.AttachX(dsXData);
for(int i = 0; i < nPeaks; i++)
{
// If X data exists, find X value of index
if( dsXData )
dsPkX[i] = dsXData[dsPkIndex[i ] - 1];
// else use index value as X
else
dsPkX[i] = dsPkIndex[i] - 1;
// Fill Y value
dsPkY[i] = crvData[dsPkIndex[i] - 1]
}
// Return number of negative peaks found
return nPeaks;
}