ocmath_find_peaks_by_local_maximum

 

Description

Find local maximum point in a local scope selected by nLocalPts. For a current point marked by nIndex, the scope is [nIndex-nLocalPts, nIndex+nLocalPts]. The local maximum point will be save as a peak.

Syntax

int ocmath_find_peaks_by_local_maximum( UINT * lSize, const double * pX, const double * pY, double * pXPeaks, double * pYPeaks, int * pnIndices, DWORD dwCtrl, int nLocalPts )

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
nLocalPts
[input] Finding maximum range. For current point nIndex, the range is [nIndex-nLocalPts, nIndex+nLocalPts]

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 peaks.
//The result is output in a new worksheet and the peaks will plot in the 
//original data plot.
void ocmath_find_peaks_by_local_maximum_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_by_local_maximum( &nDataSize, vxData, vyData, vxPeaks, vyPeaks, vnIndices, POSITIVE_DIRECTION | NEGATIVE_DIRECTION,11);
    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 result 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_by_search_window

header to Include

origin.h

Reference