ocmath_smooth

 

Description

smoothing. have 3 methods: median filter, Savitzky-Golay smoothing and adjacent averaging smoothing.

Syntax

int ocmath_smooth( int nSize, const double * pY, double * pSmooth, int nLeftpts, int nMethod = SMOOTH_SG, int nBoundary = EDGEPAD_NONE, int nRightpts = -1, int nPolydeg = 2, int nDervOrder = 0, double dPercent = 50, bool bWeight = false, bool bAveraging = false )

Parameters

nSize
[input] vector size of both pY and pSmooth
pY
[input] pointer to Y vector data.
pSmooth
[output] pointer to smoothed data, or derivatives, depending on nDervOrder(only for Savitzky-Golay smoothing).
nLeftpts
[input] if do Savitzky-Golay smoothing,number of data points to the left to be used in filter convolution,
if do median filter, Number of surrounding points to left and right. Thus the number of points considered in each segment is (2*nPts + 1).
if do adjacent averaging smoothing, is the window's size.
nMethod
[input] smoothing method.
SMOOTH_ADJAVE adjacent averaging smoothing
SMOOTH_SG Savitzky-Golay smoothing
SMOOTH_MEDIAN_FILTER median filter
nBoundary
[input] data are padded on both ends. Possible values are
EDGEPAD_NONE no padding
EDGEPAD_REFLECT pad reflect, end points are repeated such that on the left, [-1] = [0], [-2] = [1], [-3] = [2] and etc
EDGEPAD_REPEAT pad with [0] values the left and with [nSize-1] on the right
EDGEPAD_EXTRAPOLATE linear extrapolation using nLeft points on the left and nRight points on the right
EDGEPAD_PERIODIC pad periodic, [-1] = [nSize -1], [-2] = [nSize - 2]
nRightpts
[input] for Savitzky-Golay smoothing. number of data points to the right to be used in filter convolution, default (-1) will assume nLeft.
Total number of points used in the polynomial fits are (nLeft + nRight + 1) and it must be odd, namely nLeft + nRight must be even.
nPolydeg
[input] for Savitzky-Golay smoothing.The polynomial order. Higher order will preserve sharper features. nPolyDeg must be less then (nLeft + nRight + 1).
nDervOrder
[input] for Savitzky-Golay smoothing. order of derivative desired (0 = smoothing). To generate a fourth derivative, a minimum quartic (order 4) smoothing must be used.
For a third derivative, a minimum cubic (order 3) smoothing is needed. Similarly, a second derivative requires a minimum quadratic (order 2) smoothing.
dPercent
[input] for median filter.The percent for computing median.
bWeight
[input] for adjacent averaging smoothing. weight smooth or not. default is not(FALSE)
bAveraging
[input] for median filter.If true, the function will average the most nearest neighbors if there is no match to the given percent.

Return

OE_NOERROR for success

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 smooth on it. 
//The result is output in a new worksheet and the smoothing curve will plot in the 
//original data plot with color red.
void ocmath_smooth_ex1()
{
    GraphLayer gl = Project.ActiveLayer();
    if (!gl)
    {
        out_str("Active layer is not a graph.");
        return;
    }
 
    //get XY data from the first dataplot
    DataPlot dp = gl.DataPlots(0);                
    DataRange dr;
    vector vx, vy;
    if(dp.GetDataRange(dr))
    {
        DWORD dwPlotID;
        if(dr.GetData(DRR_GET_DEPENDENT | DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vy, &vx) < 0)
        {
            printf("get data failed GetData");
            return;
        }
    }
    vector vSmooth;
    vSmooth.SetSize(vy.GetSize());
 
    //do Savitzky-Golay smoothing, Left=Right=7, quadratic
    int nRet = ocmath_smooth(vy.GetSize(), vy, vSmooth, 3,SMOOTH_SG, EDGEPAD_NONE, 3, 2);
 
    //new a worksheet to put the result
    Worksheet wks;
    wks.Create("Smooth");
    wks.SetSize(-1, 2);
    wks.SetColDesignations("XY");
    
    DataRange drOut;
    drOut.Add("X", wks, 0, 0, -1, 0);
    drOut.Add("Y", wks, 0, 1, -1, 1);
    drOut.SetData(&vSmooth, &vx);
    
    //plot the curve after smoothing
    int nPlot = gl.AddPlot(drOut, IDM_PLOT_LINE);
    dp = gl.DataPlots(nPlot);
    dp.SetColor(1);
}

Remark

See Also

Header to Include

origin.h

Reference