2.1.23.2.14 ocmath_pick_peaks


Description

find peaks by simply looking at the Y values

Syntax

int ocmath_pick_peaks( const double * pYData, uint i1, uint i2, uint * pPeaks, uint nMaxPeaks, uint nPtsLeft, double peakMin, double peakYMin = 0, int nPtsRight = -1 )

Parameters

pYData
[input] pointer to Y vector data,
i1
[input] starting index to search for peaks
i2
[input] ending index (inclusive) to search for peaks
pPeaks
[output] array to receive the indices for the peak positions in pYData
nMaxPeaks
[input] size of pPeaks, if too many peaks found, function will return prematurely, so must check return value to be smaller then nMaxPeaks -1
nPtsLeft
[input] number of points to the left of the current point in the search window
peakMin
[input] height of the search window
peakYMin
[input] if specified (> 0), then ignore all peaks which is less then this Y offset from the minimum of pYData
nPtsRight
[input] optional value to specify a search window which is not the same size on the left and on the right

Return

-1 if given parameters are not suitable, otherwise return number of peaks found

Examples

EX1

//Assume in the current graph, curve's XY data is in the first data plot. This piece
//of code get the XY data of the curve from the first data plot and get the peaks.
//The result is output in a new worksheet and the peaks will plot in the original 
//data plot.
void ocmath_pick_peaks_ex1()
{
    GraphLayer gl = Project.ActiveLayer();
    if (!gl)
    {
        return;
    }
 
    //get the data from the first dataplot
    DataPlot dp = gl.DataPlots(0);        
    DataRange dr;
    vector vxData, vyData;
    if(dp.GetDataRange(dr))
    {
        DWORD dwPlotID;
        if(dr.GetData(DRR_GET_DEPENDENT | DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vyData, &vxData) < 0)
        {
            printf("get_plot_data failed GetData");
            return;
        }
    }
 
    vector<uint> vPeaks;
    uint nMaxPeaks = vyData.GetSize();
    vPeaks.SetSize(nMaxPeaks);
    uint nPtsLeft = 3;
    double peakMin = 5;
    double peakYMin = 0;
    int nPtsRight = -1;
    int numPeaks = ocmath_pick_peaks(vyData, 0, vyData.GetSize()-1, vPeaks, nMaxPeaks, nPtsLeft, peakMin,peakYMin, nPtsRight);
    vPeaks.SetSize(numPeaks);
    vector vxPeaks, vyPeaks;
    vxPeaks.SetSize(numPeaks);
    vyPeaks.SetSize(numPeaks);
    for(int ii = 0; ii < numPeaks; ii++)
    {
        vxPeaks[ii] = vxData[vPeaks[ii]];
        vyPeaks[ii] = vyData[vPeaks[ii]];
    }
 
    //new a worksheet to output the result
    WorksheetPage wksPage;
    wksPage.Create();
    Worksheet wksResult = wksPage.Layers(0);
    int nIndCol, nXCol, nYCol;
    nIndCol = wksResult.AddCol("Indices");
    nXCol = wksResult.AddCol("X Coordinate");
    nYCol = wksResult.AddCol("Y Coordinate");
    wksResult.Columns(nIndCol).SetType(OKDATAOBJ_DESIGNATION_X);
    wksResult.Columns(nXCol).SetType(OKDATAOBJ_DESIGNATION_X);
    wksResult.Columns(nYCol).SetType(OKDATAOBJ_DESIGNATION_Y);
    
    DataRange drOut;
    drOut.Add("X", wksResult, 0, nIndCol, -1, nIndCol);
    drOut.Add("Y", wksResult, 0, nXCol, -1, nXCol);
    drOut.Add("Z", wksResult, 0, nYCol, -1, nYCol);
    drOut.SetData(&vyPeaks, &vxPeaks, &vPeaks);
    
    //plot the peaks in the data plot
    XYRange plotRange;
    plotRange.Add("X", wksResult, 0, nXCol, -1, nXCol);
    plotRange.Add("Y", wksResult, 0, nYCol, -1, nYCol);
    gl.AddPlot(plotRange, IDM_PLOT_SCATTER);
}

Remark

See Also

ocmath_find_peaks_1st_derivative, ocmath_find_peaks_2nd_derivative

Header to Include

origin.h

Reference