2.1.17.4.3 ocmath_integrate


Description

Integrate area under curve using Trapezoid Rule. Missing values are ignored.

Syntax

int ocmath_integrate( const double * pXData, const double * pYData, uint i1, uint i2, IntegrationResult * pInteg, double * pIntegral = NULL, int areaType = MATHEMATICAL_AREA, const double * pBaseLine = NULL, bool bSortAscending = false, int nMethodSearchHalfWidth = SEARCH_FROM_BOTH_ENDS)

Parameters

pXData
[input] pointer to X vector data
pYData
[input] pointer to Y vector data
i1
[input] starting index for the integration
i2
[input] ending index (inclusive) for the integration
pInteg
[output] result of integration.
pIntegral
[output] optional cumulative vector from the integration, must be allocated with with at least (i2+1) points
areaType
[input] MATHEMATICAL_AREA, direct cumulative vector from the integration; ABSOLUTE_AREA, integrate absolute area
pBaseLine
[input] integrate baseline
bSortAscending
[input] sort X before perform integration
nMethodSearchHalfWidth
[input] SEARCH_FROM_BOTH_ENDS, find half width from two ends by using linear search; SEARCH_FROM_PEAK, find half width from peak center by using linear search

Return

return OE_NOERROR for success, otherwise return errors

Examples

EX1

// This example reports all the properties of an ocmath_IntegResult structure after integrating a plotted dataset.
// Active window must be a 2D Graph window with plotted data.
void    ocmath_integrate_ex1()
{
    printf("Area = %f\n", ocmath_integrate_sub1(Project.ActiveCurveBase()));
}
// Function dumps integration result and returns area
double    ocmath_integrate_sub1(curvebase& cuv, IntegrationResult* presult = NULL, int nFrom = 0, int nTo = -1)
{
    IntegrationResult    result;
    // make a local copy of the input curve so that we can be sure that it is numeric(double) so that
    // we can get its double* directly to pass into ocmath functions which all expect double* from vectors
    Curve cc(cuv); 
    if(nTo < 0)
        nTo = cc.GetUpperIndex();
    vector  vx, vy;
    cc.CopyData(vx, vy);
    if(ocmath_integrate(vx, cc, nFrom, nTo, &result))
        return NANUM;
    if(presult)
        *presult = result;
    else // show in script window if NULL for presult
    {
        Tree trTemp;
        TreeNode tr = trTemp.AddNode("IntegResults");
        tr = result;
        out_tree(tr);
    }
    return result.Area;    
}

EX2

//integrate nY col with baseline nYb col from given range of index
//nY col is assumed to have its sample interval, or using Row# as X
//nYb is assumed to share same X as nY
int integ_with_base(Worksheet wks, int nY, int nYb, IntegrationResult& result = NULL, int nFrom = 0, int nTo = -1)
{
    Column yy(wks, nY);
    Column yb(wks, nYb);
    if(!yy || !yb)
        return -1;
    
    vector vy;
    vectorbase &y1 = yy.GetDataObject();
    vectorbase &y2 = yb.GetDataObject();
    vy = y1 - y2;
    double x1, xinc;
    if(!yy.IsEvenSampling(&x1, &xinc))
        x1 = xinc = 1;
    double x2 = x1 + (vy.GetSize()-1) * xinc;
    
    vector vx;
    vx.Data(x1, x2, xinc);
    ASSERT(vx.GetSize() == vy.GetSize());
    
    if(nTo < 0) nTo = vy.GetSize()-1;
    
    return ocmath_integrate(vx, vy, nFrom, nTo, &result);
}
void test_integ_with_base(int nFrom = 0, int nTo = -1, int nY = 0, int nB = 1)
{
    Worksheet wks = Project.ActiveLayer();
    IntegrationResult result;
    int nErr = integ_with_base(wks, nY, nB, result, nFrom, nTo);
    if(nErr != 0)
        out_int("Err ", nErr);
    else
    {
        Tree trTemp;
        TreeNode tr = trTemp.AddNode("IntegResults");
        tr = result;
        out_tree(tr);
    }
}

Remark

See Also

Header to Include

origin.h

Reference