View Raw SPL
/*****************************************************************************
*                                                                            *
*   XYTOINTV.SPL Copyright (C) 2009 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Converts an XY series to an interval series using xvals     *
*                                                                            *
*   Revisions:    3 Jun 2009  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#include 


#if @HELP_XYTOINTV

    XYTOINTV

    Purpose: Converts XY to an interval series using the X value difference.

    Syntax:  XYTOINTV(xy, force)

                  xy - An XY series, the input.

               force - An optional integer, force the conversion.

                        0: check if difference between X values is
                           constant (default)

                        1: assume difference between X values is constant

   Alternative Syntax:

             XYTOINTV(x, y, force)

                  x  - A series, the X values.

                  y  - A series, the Y values.

               force - An optional integer, force the conversion.

                        0: check if difference between X values is
                           constant (default)

                        1: assume difference between X values is constant

    Returns: An interval series.

    Example:
             W1: 1..10
             W2: grand(10, 1)
             W3: xy(W1, W2)
             W4: xytointv(W3)

             W4 contains an interval series where the X values are
             the same as the values of W1. The starting offset of the
             series in W4 is 1.0

    Example:
             W1: 1..10
             W2: grand(10, 1)
             W3: xytointv(W1, W2)

             Same as the first example except the explicit X and Y series
             are given.

    Remarks:
             If the difference of the X values is constant, XYTOINTV uses
             this difference as the DELTAX value of the output series. The
             Y values are copied as is without interpolation.

             If the X value difference is not constant, XYINTERP is used to
             linearly interpolate the input to an interval series.

             Setting FORCE to 1 forces the algorithm to assume the difference
             of the X values is constant, essentially using the difference
             if the first two X values as the DELATX value.

             Setting FORCE to 2 forces XYINTERP to be called.

    See Also:
             Xyinterp
#endif


/* use first two xvals to create an interval series */
xytointv(x, y, force)
{
        local yout, dx, xdiff, tol = 1e-8;

        /* parse args */
        if (argc < 3)
        {
                if (argc < 2)
                {
                        if (argc < 1) error("xytointv - input series required");
                        
                        y     = yval(x);
                        x     = xvals(x);
                        force = 0;
                }
                if (not(isarray(y)))
                {
                        force = y;
                        y     = yvals(x);
                        x     = xvals(x);
                }
                else
                {
                        force = 0;
                }
        }

        if (length(x) < 2)
        {
                error("xytointv - input series must contain at least 2 samples");
        }

        if (force == 0)
        {
                xdiff = extract(lderiv(x), 2, -1);
                force = abs(min(xdiff) - max(xdiff)) <= tol;
        }

        if (force == 1)
        {
                /* all xvals are the same, set dx to difference of xvals */
                yout = y;
                dx   = x[2] - x[1];

                /* set offset, dx, and units */
                setxoffset(yout, x[1]);
                setdeltax(yout, dx);

                setvunits(yout, getvunits(y));
                sethunits(yout, getvunits(x));
        }
        else
        {
                /* use xyinterp */
                yout = xyinterp(x, y);
        }

        return(yout);
}