ocmath_derivative

 

Description

Simple derivative calculations without smoothing. Missing values are ignored. There is no smoothing performed in this function. Derivative is computed by taking the average of the two data points slopes from the previous and next data points.

Syntax

int ocmath_derivative( const double * pXData, double * pYData, uint nSize, DWORD dwCntrl = 0 )

Parameters

pXData
[input] pointer to X vector data
pYData
[modify] pointer to Y vector data, result of derivatives are put back to replace original data
nSize
[input] number of points in pXData and pYData
dwCntrl
[input] options, DERV_PEAK_AS_ZERO, will fill zero if data change direction, otherwise the average is used

Return

return OE_NOERROR for success, otherwise return errors

Examples

EX1

// Create some data and compare the average and zero fill method of numerical derivative
void    ocmath_derivative_ex1()
{
    GraphLayer gl = Project.ActiveLayer();
    if (!gl)
    {
        out_str("Active layer is not a graph.");
        return;
    }
    
    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;
        }
    }
    
    // Create copies of the 'y' data to operate on
    vector vderiv1;
    vderiv1 = vy;
    vector vderiv2;
    vderiv2 = vy;
    Worksheet wks;
    wks.Create("Derivative");
    wks.SetSize(-1, 4);
    DataRange drOut;
    drOut.Add("X", wks, 0, 0, -1, 0);
    drOut.Add("X", wks, 0, 1, -1, 1);
    drOut.Add("X", wks, 0, 2, -1, 2);
    drOut.Add("X", wks, 0, 3, -1, 3);
    drOut.SetData(vx, false, 0);
    drOut.SetData(vy, false, 1);
    // First try using averaging
    if(OE_NOERROR == ocmath_derivative(vx, vderiv1, vy.GetSize()))
    {
        drOut.SetData(vderiv1, false, 2);
    }
    // Now use the fill with zero option
    if(OE_NOERROR == ocmath_derivative(vx, vderiv2, vy.GetSize(), DERV_PEAK_AS_ZERO))
    {
        drOut.SetData(vderiv2, false, 3);
    }
}

Remark

See Also

header to Include

origin.h

Reference