2.1.17.5.15 ocmath_interpolate


Description

Do interpolation with mode of Linear, Spline or B-Spline.

Syntax

int ocmath_interpolate( const double * pX, double * pY, UINT nSize, const double * pSrcX, const double * pSrcY, UINT nSrcSize, int nMode = INTERP_TYPE_LINEAR, double dSmoothingFactor = 1, const double * pWeights = NULL, double * pCof = NULL, DWORD dwCntrl = 0, int nSplBoundType = 0, int iOption = OPTION_EXTRAPOLATE )

Parameters

pX
[input] pointer to X coordinate to be evaluated. size is nSize
pY
[output] pointer to the interpolation values at evaluation points. size is nSize
if pX[i] is invalid(e.g, out of the range of pSrcX[0] to pSrcX[nSrcSize-1] in linear interpolation), set pY[i] to NANUM.
nSize
[input] size of pX and pY, nSize > 0
pSrcX
[input] pointer to the X value of the curve, must be in strictly ascending order, otherwise it should be sorted before calling this function. size is nSrcSize.
pSrcY
[input] pointer to the Y value of the curve. size is nSrcSize.
nSrcSize
[input] size of pSrcX and pSrcY. if nMode = INTERP_TYPE_LINEAR, nSrcSize >= 1; otherwise nSrcSize >= 4.
nMode
[input] interpolation method. Must be one of the three modes:
INTERP_TYPE_LINEAR(linear interpolation),
INTERP_TYPE_SPLINE(cubic spline interpolation with not-a-knot boundary condition, and the not-a-knot boundary condition assume that the 3rd order derivative are continuous on the 2nd and last 2nd points),
INTERP_TYPE_BSPLINE(B-Spline curve fitting using method by Dierckx.P)
dSmoothingFactor
[input] This argument specifies the closeness to the original data. It is only useful when nMode = INTERP_TYPE_BSPLINE. dSmoothingFactor >= 0.
By means of this parameter, the user can control the tradeoff between closeness of fit and smoothness of fit of the approximation.
If dSmoothingFactor is too large, the spline will be too smooth and signal will be lost ; if it is too small the spline will pick up too much noise.
In the extreme cases the program will return an interpolating spline if dSmoothingFactor=0 and the weighted least-squares polynomial of degree 3 if s is very large.
pWeights
[input] pointer to weights, which is only used in method INTERP_TYPE_BSPLINE, by default(pWeights = NULL) all weights are 1. size is nSrcSize
pCof
[output] pointer to coefficients, which are used in method INTERP_TYPE_SPLINE and INTERP_TYPE_BSPLINE, by default(pCof = Null), size is nSrcSize
dwCntrl
[input] indicates do sorting or not before doing interpolation, when it equals OCMATH_SRCDATA_X_MONOTONIC, don't sort.
nSplBoundType
[input] enumeration, indicate the boundary condition for spline (cubic spline), 0 for natural boundary condition, 1 for not_a_knot (currently only support these 2 kinds)
iOption
[input] enumeration, 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: Success

OE_BAD_PARAM (error code: -18): invalid argument of nMode

OE_INT_ARG_LT (error code: -15): invalid argument of nSize of nSrcSize

OE_NOT_STRICTLY_INCREASING (error code: -17): pSrcX[i], i=0,1,...,nSrcSize-1 not strictly increasing

OE_REAL_ARG_LT(error code: -14): invalid dSmoothingFactor

OE_ALLOC_FAIL (error code: -19): memory allocation failed

OE_COEFF_CONV (error code: - 23): the iterative process has failed to converge using method INTERP_TYPE_BSPLINE. Possibly dSmoothingFactor is too small

Examples

EX1

//Assume that Active worksheet has 4 columns A,B,C and D. Column C and D are x and y data of curve. Column C is in ascending order.
//Column A is x data to be evaluated, interpolation result will be output into column B.
    void    ocmath_interpolate_ex1(double dd = 1)
    {
        int nMode = INTERP_TYPE_BSPLINE;
        double dSmoothingFactor = dd;
        Worksheet    wks = Project.ActiveLayer();
        wks.SetSize(-1, 4);
        DataRange drIn;
        drIn.Add("X", wks, 0, 0, -1, 0);
        drIn.Add("X", wks, 0, 1, -1, 1);
        drIn.Add("X", wks, 0, 2, -1, 2);
        drIn.Add("X", wks, 0, 3, -1, 3);
    
        vector vx, vy, vSrcx, vSrcy;
        drIn.GetData(&vx, 0);
        //drIn.GetData(&vy, 1);
        drIn.GetData(&vSrcx, 2);
        drIn.GetData(&vSrcy, 3);
        
        int    nSize = vx.GetSize();
        int    nSrcSize = vSrcx.GetSize();
        vy.SetSize(nSize);
        
        int iRet = ocmath_interpolate(vx, vy, nSize, vSrcx, vSrcy, nSrcSize, nMode, dSmoothingFactor);
        drIn.SetData(vy, true, 1);//interpolation result 
    }

Remark

Interpolation. Three modes are available: Linear, Spline, B-Spline.
When the mode is B-Spline, if your data, which will be interpolated or extrapolated, is in the interval of the B-Spline, this function calls B-Spline functions of nag to interpolate it, otherwise this function uses the nearest two end points to extend the B-Spline polynomial by linear interpolation.
cf. Dierckx P: an algorithm for smoothing, differentiation and integration of experimental data using spline functions, J.Comp.Appl.Maths 1 (1975) 165-184.

See Also

ocmath_2d_interpolate, ocmath_3d_interpolate

Header to Include

origin.h

Reference

nag_1d_spline_fit(e02bec)nag_1d_spline_fit(e02bec), Nag Manual