View Raw SPL
/*****************************************************************************
*                                                                            *
*   SETXRANGE.SPL Copyright (C) 2015-2017 DSP Development Corporation        *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:       Randy Race                                                 *
*                                                                            *
*   Synopsis:     Sets X autoscaled plotting range                           *
*                                                                            *
*   Revisions:    10 Sep 2015  RRR  Creation                                 *
*                                                                            *
*****************************************************************************/


#if @HELP_SETXRANGE

    SETXRANGE

    Purpose: Sets the X axis autoscaled plotting range.

    Syntax:  SETXRANGE(win1, win2, ..., winN, xl, xr, ntics, mode)

               winN - Optional. Zero or more windows. Defaults to the
                      current window.

                 xl - A real. The left window boundary.

                 xr - A real. The right window boundary.

              ntics - Optional. An integer, the number of tic values
                      to display. Defaults to 11 tics.

               mode - Optional. An integer, the range mode.

                      -1: xl and xr specify point numbers
                       0: xl and xr specify plot coordinates (default)
                       1: xl and xr specify time values

    Returns: Nothing. The window is scaled to the X range values. The
             first and last tics are labeled.

    Example:
             W1: integ(gnorm(1000, 1));setxrange(-100, 1100)

             W1 contains a series. The X axis range is set to -100 and
             110 with 11 tic values spaced 120 units apart. The autoscaled
             range is also set to -100 and 1100.

    Example:
             W1: integ(gnorm(1000, 1));setxrange(-100, 1100, 6)

             Same as above except the X axis has 6 tic values spaced
             240 units apart.

    Remarks: The tic interval is determined such that the first and last
             tics of the range are always labeled.

             SETXRANGE also sets the autoscaled range to the input
             range.

             Use SETXRANGE(-1, -1) to reset the plotting range to the
             automatic defaults.

             Set MODE to -1 to specify the plot range in terms of samples.
             For example:

                setxrange(w1, 10, 1000, 5, -1)

             sets W1 to display data from point number 10 throught point
             number 1000 with 5 tic marks.

    See Also:
             Autoscale
             Setx
             Setxauto
             Setyrange
             Xtic
#endif


/* set X autoscaled plotting range */
setxrange(argv)
{
        local j, v, xl, xr, nt, ntics, havewin, mode, md;

        xl = xr = ntics = mode = {};
        havewin = 0;

        /* get scalar args */
        loop (j = 1..argc)
        {
                v = getargv(j);

                if (isscalar(v))
                {
                        if (isempty(xl))
                        {
                                xl = v;
                        }
                        else if (isempty(xr))
                        {
                                xr = v;
                        }
                        else if (isempty(ntics))
                        {
                                ntics = v;
                        }
                        else if (isempty(mode))
                        {
                                mode = v;
                        }
                }
                else if (iswindow(v))
                {
                        havewin = 1;
                }
        }

        if (isempty(xl) || isempty(xr))
        {
                error("setxrange - xleft and xright required");
        }

        if (havewin)
        {
                /* set each window */
                loop (j = 1..argc)
                {
                        v = getargv(j);

                        if (iswindow(v))
                        {
                                nt = isempty(ntics) ? (getxlog(v) ? -1 : 11) : ntics;
                                md = isempty(mode)  ? 0 : mode;
                                setxrange_set(v, xl, xr, nt, md);
                        }
                }
        }
        else
        {
                /* set current window */
                nt = isempty(ntics) ? (getxlog(w0) ? -1 : 11) : ntics;
                md = isempty(mode)  ? 0 : mode;
                setxrange_set(w0, xl, xr, nt, md);
        }
}


/* set range and tics */
setxrange_set(w, xl, xr, ntics, mode)
{
        local r, tics;

        /* range */
        r = abs(xr - xl);

        /* number of tics */
        ntics = (ntics > 0 && ntics < 2) ? 2 : ntics;

        /* tic interval */
        if (setxlog(w))
        {
                r = (xl <= 0) ? xr / 10 : xr / xl;
                tics = (ntics < 0) ? ntics : log10(r) / ntics;
        }
        else
        {
                tics = (ntics < 0) ? ntics : r / (ntics - 1);
        }

        if (xl == xr)
        {
                /* reset */
                tics = -1;
        }

        /* x range */
        setx(w, xl, xr, mode);

        /* autoscale */
        setxauto(w, xl, xr, mode);

        /* tic interval */
        setxtic(w, tics);

        /* label first tic */
        setxticfirst(w, 1);
}