Find curve's peaks by 2nd derivative.
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)
Return OE_NOERROR if succeed, otherwise, non-zero error code is returned.
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); }
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.
ocmath_find_peaks_1st_derivative
origin.h