View Raw SPL
/*****************************************************************************
*                                                                            *
*   IDXTOY.SPL  Copyright (C) 2003 DSP Development Corporation               *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Converts an index to a Y value                              *
*                                                                            *
*   Revisions:   30 Jan 2003  RRR  Creation                                  *
*                                                                            *
*                                                                            *
*****************************************************************************/

#include 


#if @HELP_IDXTOY

    IDXTOY

    Purpose: Converts index values of a series to Y values.

    Syntax:  IDXTOY(series, idx)

             series    - input series

              idx      - An integer or series of integers. The indices
                         ranging from 1 to length(s)

    Returns: A real or series.

    Example:
             W1: 1..0.1..10
             W2: idxtoy(w1, 1..5)

             W2 contains the series {1, 1.1, 1.2, 1.3, 1.4}, the first 5
             Y values of W1.

    Remarks:
             For XY or XYZ series, the actual Y values are looked up.

             For interval series, the Y values are computed as follows:

                 y = (idx - start) * deltay(s) + yoffset(s)

             where start is the value of SPL_START_INDEX.

    See Also:
             Idx
             Idxtox
             Yoidx
             Yvals
#endif



/* convert index (i.e. col) to y value */
idxtoy(s, idx)
{
        local y, ptype, start;

        if (argc < 2)
        {
                error(sprintf("%s - input series and index value required", __FUNC__));
        }

        if (numcols(s) > 1)
        {
                ptype = getplottype(s);

                if (itemtype(s) == 2 && ptype >= 1 && ptype <= 5)
                {
                        /* starting index */
                        start = castint(getconf("SPL_START_INDEX"));

                        /* 2D interval series - use faster computation */
                        y = (idx - start) * deltay(s) +  yoffset(s);
                }
                else
                {
                        y = idxtoy_lookup_iterate(s, idx);
                }
        }
        else
        {
                y = idxtoy_lookup(s, idx);
        }
        
        return(y);
}


/* direct index lookup */
idxtoy_lookup(s, idx)
{
        local y;

        y = yvals(s);
        y = y[idx, ..];

        return(y);
}


/* multi-column lookup */
ITERATE idxtoy_lookup_iterate(s, y)
{
        local idx;

        idx = idxtoy_lookup(s, y);

        return(idx);
}