2.1.17.2.9 ocmath_ave_multiple_curves


Description

Average multiple curves and produce a single averaged curve.

Syntax

int ocmath_ave_multiple_curves( uint nSize, const OneCurveData * pCurves, int * pnAveSize, double * pAveX, double * pAveY, double * pAveErr = NULL, int nErrType = AVMC_SE, bool bInterp = false, int nMethod = INTERP_TYPE_LINEAR, double dTolerance = 0.0, int * pAveCount = NULL, double* pAveMin = NULL, double* pAveMax = NULL, double dSmoothingFactor = 1, int iOption = OPTION_EXTRAPOLATE )

Parameters

nSize
[input] number of curves.
pCurves
[input] pointer to structures that must be filled with pointers to input data.
pnAveSize
[modify] on input, allocated memory size of pAveX, pAveY and pAveErr buffer, should be large enough, for example, equal to curves' total size;
on output, size of pAveX, pAveY and pAveErr buffer, which are determined internally.
pAveX
[input] pointer to buffer of the X values for the averaged curve
pAveY
[output] pointer to buffer to receive Y values for the averaged curve
pAveErr
[output] if not NULL, pointer to buffer to receive the error bar data for the averaged curve
nErrType
[input] to specify return what type data in pAveErr array.
AVMC_SE, standard error of mean
AVMC_SD, standard deviation
AVMC_MIN, minimum
AVMC_MAX, maximum
bInterp
[input] interpolate curves or not.
nMethod
[input] if bInterp is true, which interpolation method would be used.
dTolerance
[input] if bInterp is false, the tolerance for X values; valid only if dTolerance is greater than 0
pAveCount
[output] if not NULL and if bInterp is false, pointer to buffer to receive count of averaged values for each point of the averaged curve
pAveMin
[output] if not NULL, pointer to buffer to receive min data for the
averaged curve. Its default value is NULL.
pAveMax
[output] if not NULL, pointer to buffer to receive max data for the
averaged curve. Its default value is NULL.
iOption
[input] specify how to extrapolate Y values in extrapolated range. Must be one of the three values:
OPTION_EXTRAPOLATE(extrapolate Y using the last two points),
OPTION_SET_MISSING(set all Y values in the extrapolated range to be missing values),
OPTION_REPEAT_LAST(use the Y value of the closest input X value for all values in the extrapolated range)

Return

OE_NOERROR Succeed

OE_SIZE_LT The number of curves is less than 2

OE_AVMC_BUFFER_TOO_SMALL The allocated memory size *pnAveSize is not big enough

Examples

EX1

//This example assume the active window is a graph.
//All dataplots on the graph are averaged into one curve.
//The result curve and the error bar are added to the graph.
void ocmath_ave_multiple_curves_ex1()
{
    GraphPage gp = Project.GraphPages(0);
    if(!gp)
    {
        return;
    }
    GraphLayer gl = gp.Layers();
    if (!gl)
    {
        return;
    }
 
    int nPlots = gl.DataPlots.Count();
    int ii;
    int nAveSize = 0;
    int nTotalSize = 0;
 
    OneCurveData* multiCurveData = (OneCurveData*) malloc(sizeof(OneCurveData)*nPlots);
 
    double* xx;
    double* yy;
 
    for(ii = 0; ii < nPlots; ii++)
    {
        DataPlot dp = gl.DataPlots(ii);        
        DataRange dr;
        vector vDatax, vDatay;
        if(dp.GetDataRange(dr))
        {
            DWORD dwPlotID;
            if(dr.GetData(DRR_GET_DEPENDENT | DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vDatay, &vDatax) < 0)
            {
                printf("get_plot_data failed GetData");
                return;
            }
        }
        nAveSize = vDatay.GetSize();
 
        xx = (double*) malloc (sizeof(double) * nAveSize);
        yy = (double*) malloc (sizeof(double) * nAveSize);
 
        memcpy(xx, vDatax, sizeof(double)*nAveSize);
        memcpy(yy, vDatay, sizeof(double)*nAveSize);
 
        multiCurveData[ii].nSize = nAveSize;
        multiCurveData[ii].pX = xx;
        multiCurveData[ii].pY = yy;
        nTotalSize += nAveSize; 
    }
 
    vector vxAve(nTotalSize);
    vector vyAve(nTotalSize);
    vector vErr(nTotalSize);
    vector<int> vCount(nTotalSize);
    
    for(int jj = 0; jj < nTotalSize; jj++)
    	vxAve[jj]=jj;
 
    int nRet = ocmath_ave_multiple_curves(nPlots, multiCurveData, &nTotalSize, vxAve, vyAve, vErr, AVMC_SE, true,
                        INTERP_TYPE_LINEAR, 0, vCount);
 
    if( nRet!=OE_NOERROR )
    {
        printf("error code: %d\n", nRet);
        return;
    }
 
    vxAve.SetSize(nTotalSize);
    vyAve.SetSize(nTotalSize);
    vErr.SetSize(nTotalSize);
    vCount.SetSize(nTotalSize);
 
    for (ii = 0; ii < nPlots; ii++)
    {
        free(multiCurveData[ii].pX);
        free(multiCurveData[ii].pY);
    }
    free(multiCurveData);
 
    Worksheet wksAve;
    wksAve.Create("Origin", CREATE_HIDDEN);
    wksAve.SetSize(-1, 4);
    wksAve.Columns(0).SetType(OKDATAOBJ_DESIGNATION_X);
    wksAve.Columns(1).SetType(OKDATAOBJ_DESIGNATION_Y);        
    wksAve.Columns(2).SetType(OKDATAOBJ_DESIGNATION_ERROR);
    wksAve.Columns(3).SetType(OKDATAOBJ_DESIGNATION_Y);
    DataRange drOut;
    drOut.Add("X", wksAve, 0, 0, -1, 0);
    drOut.Add("X", wksAve, 0, 1, -1, 1);
    drOut.Add("X", wksAve, 0, 2, -1, 2);
    drOut.Add("X", wksAve, 0, 3, -1, 3);
    drOut.SetData(vxAve, false, 0);
    drOut.SetData(vyAve, false, 1);
    drOut.SetData(vErr, false, 2);
    vector vCount1;
    vCount = vCount;
    drOut.SetData(vCount1, false, 3);
 
    DataRange drPlot;
    drPlot.Add("X", wksAve, 0, 0, -1, 0);
    drPlot.Add("Y", wksAve, 0, 1, -1, 1);
    gl.AddPlot(drPlot, IDM_PLOT_SCATTER);
}

Remark

See Also

ocmath_ave_multiple_curves_by_length_parameter

Header to Include

origin.h

Reference