ocmath_find_peaks_1st_derivative

 

Description

Find curve's peaks by 1st derivative.

Syntax

int ocmath_find_peaks_1st_derivative( UINT * lSize, const double * pX, const double * pY, double * pXPeaks, double * pYPeaks, int * pnIndices, DWORD dwCtrl = POSITIVE_DIRECTION, bool bUseSGderivativeForFindPeak = false, 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
nPtsSmooth
[input] smooth points number, if equal to 0, then omit smooth

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_1st_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_1st_derivative( &nDataSize, vxData, vyData, vxPeaks, vyPeaks, vnIndices, POSITIVE_DIRECTION | NEGATIVE_DIRECTION,false);
    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 1st derivative. Calculate curve's smoothed 1st derivative on every point in px, py.

Then the point is a peak, if the signs of smoothed 1st derivatives are different on its left point and right point.

POSITIVE_DIRECTION peak, if the smoothed 1st derivative is greater than zero on its left point, and less than zero on its right point.

NEGATIVE_DIRECTION peak, if the smoothed 1st derivative is less than zero on its left point, and greater than zero on its right point.

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_2nd_derivative

header to Include

origin.h

Reference