View Raw SPL
/*****************************************************************************
*                                                                            *
*   LOGSPACE.SPL Copyright (C) 2001, 2018 DSP Development Corporation        *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns n log spaced samples from 10^lo to 10^hi value      *
*                                                                            *
*   Revisions:   24 Jul 2001  RRR  Creation                                  *
*                 4 Jun 2018  RRR  End point adjustment                      *
*                                                                            *
*****************************************************************************/


#if @HELP_LOGSPACE

    LOGSPACE

    Purpose: Creates a series of n log spaced values from 10^lo to 10^hi inclusive

    Syntax:  LOGSPACE(lo, hi, n)

                 lo - optional real, the start range, defaults to 0.0,

                 hi - optional real, the end range, defaults to 3.0.

                  n - optional integer, the number of samples, defaults
                      to 100.

    Returns: A series of n logarithmically spaced values.

    Example:
             logspace(1, 5, 5)

             returns {10, 100, 1000, 10000, 100000}


    Example:
             logspace(0, 5, 5)

             returns {1, 17.783, 316.228, 5623.413, 100000}

    Remarks:
             The DELTAX values of the resulting series is (10^hi - 10^lo)/(n-1)
             and the XOFFSET is set to 10^lo.

    See Also:
             .. (Range Specifier)
             Gline
             Linspace
#endif


/* generate N log space values from lo to hi inclusive */
logspace(lo, hi, n)
{
        local dx, off, lspace, plo, phi, slope = 1.0;

        if (argc < 3)
        {
                if (argc < 2)
                {
                        if (argc < 1)
                        {
                                lo = 0.0;
                        }
                        
                        hi = 3;
                }
                
                n = 100;
        }

        n = castint(n);

        if (hi == pi) hi = log10(pi);

        /* use the fast gline to create series */

        dx = (hi - lo);
        
        if (dx == 0 || n <= 1)
        {
                lspace = {10.0 ^ hi};
        }
        else
        {
                if (dx < 0)
                {
                        dx    = abs(dx);
                        slope = -1.0;
                }

                /* generate on 0..n scale, multiply then divide for accuracy */
                lspace = 10 ^ (lo + (gline(n, 1, slope, 0) * dx) / (n - 1));

                plo = 10.0 ^ lo;
                phi = 10.0 ^ hi;

                /* endpoints */
                lspace[{1, end}] = {plo, phi};

                /* set offset and deltax */
                off = (isinfvalue(plo) || isnavalue(plo)) ? 0.0 : plo;
                setxoffset(lspace, off);

                dx = abs(phi - plo) / (n - 1);
                dx = (isinfvalue(dx) || isnavalue(dx)) ? 1.0 : dx;
                setdeltax(lspace,  dx);
        }

        return(lspace);
}