ocmath_find_peaks_2nd_derivative

 

Description

Find curve's peaks by 2nd derivative.

Syntax

int ocmath_find_peaks_2nd_derivative( UINT * lSize, const double * px, const double * py, double * pxPeaks, double * pyPeaks, int * pnIndices, DWORD dwCtrl, int nDerivativeMethod = 1, double * pSecondDeriv=NULL, int nPtsSmooth = 11, int nPolyOrder=2)

Parameters

lSize
[modify] on input, size of px, py, pxPeaks, pyPeaks, pnIndices; on output, return number of peaks
px
[input] it contains curve's X coordinate's datas
py
[input] it contains curve's Y coordinate's datas
pxPeaks
[output] Its size is equal to the size of px. it contains peaks' X coordinate's datas,
if the number of peaks is less than its size, the excrescent elements of it are filled
with 0.
pyPeaks
[output] Its size is equal to the size of py. it contains peaks' Y coordinate's datas,
if the number of peaks is less than its size, the excrescent elements of it are filled
with 0.
pnIndices
[output] Its size is equal to the size of px. it contains peaks' indices, if peaks's number is
less than its size, the excrescent elements of it are filled with 0.
dwCtrl
[input] combine of POSITIVE_DIRECTION, NEGATIVE_DIRECTION, KEEP_OPPOSITE_SIDE
nDerivativeMethod
[input] derivative method: DERIVATIVE_WITHOUT_SMOOTH = 0, DERIVATIVE_WITH_ADJ_SMOOTH, DERIVATIVE_USE_SG_SECOND_DERIV, DERIVATIVE_WITH_CONTINUOUS_SG_FIRST_DER_SMOOTH
pSecondDeriv
[output] to keep second derivative vector of different methods
nPtsSmooth
[input] smooth points number
nPolyOrder
[input] polynomial order of SG derivative method

Return

Return OE_NOERROR if succeed, otherwise, non-zero error code is returned.

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 find the feets of
//the peaks. The result is output in a new worksheet and the feets will plot in the 
//original data plot.
void ocmath_find_peaks_2nd_derivative_ex1( )
{
    GraphLayer gl = Project.ActiveLayer();
    if (!gl)
    {
        return;
    }
 
    //get data from the first data plot
    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;
        }
    }
 
    uint nDataSize = vxData.GetSize();
    int iSize = vxData.GetSize();
 
    vector vxPeaks, vyPeaks;
    vector<int> vnIndices;
 
    vxPeaks.SetSize(nDataSize);
    vyPeaks.SetSize(nDataSize);
    vnIndices.SetSize(nDataSize);
 
    int nRet = ocmath_find_peaks_2nd_derivative( &nDataSize, vxData, vyData, vxPeaks, vyPeaks, vnIndices, POSITIVE_DIRECTION | NEGATIVE_DIRECTION, 1, NULL, 11, 3);
    if( nRet < OE_NOERROR )
    {
        printf("error code: %d\n", nRet);
        return;
    }
    vxPeaks.SetSize(nDataSize);
    vyPeaks.SetSize(nDataSize);
    vnIndices.SetSize(nDataSize);
    
    //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, &vnIndices);
    
    //show the feets 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

Find curve's peaks by 2nd derivative. Calculate curve's smoothed 2nd derivative on every point in px, py.

Then the point is a peak, if the smoothed 2nd derivative on it is extremum.

POSITIVE_DIRECTION peak, if the smoothed 2nd derivative on it is local maximum.

NEGATIVE_DIRECTION peak, if the smoothed 2nd derivative on it is local minimum.

lsize returns the number of peaks, pxPeaks and pyPeaks contain X and Y coordinate's datas of peaks,and pnIndices contains the indices of peaks.

See Also

ocmath_find_peaks_1st_derivative

Header to Include

origin.h

Reference