View Raw SPL
/*****************************************************************************
*                                                                            *
*   NEXTPOW2.SPL Copyright (C) 1997-2002 DSP Development Corporation         *
*                           All Rights Reserved                              *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Finds exponent of 2^p                                       *
*                                                                            *
*   Revisions:   18 Aug 1997  RRR  Creation                                  *
*                 9 Oct 2002  RRR  documentation, local fixup                *
*                                                                            *
*****************************************************************************/

#include 


#if @HELP_NEXTPOW2

    Purpose: Determines the exponent for the next power of 2 >= input value.

    Syntax:  NEXTPOW2(s)

              s - A real value or a series or table.

    Returns: An integer.

    Example:

             nextpow2(55)

             returns 6.  2^6 == 64.


             nextpow2(64)

             returns 6.  2^6 == 64.


             nextpow2(100)

             returns 7.  2^7 == 128.


             W1: 1..1024
             nextpow2(w1)

             returns 10.  The length is 1024; 2^10 == 1024.

Remarks:
             If the input is a series or table, the return value is the
             next power of 2 greater than or equal to the length of the
             series.

    See Also:
             BESTPOW2
             FFT
             LOG2
#endif



/* find next power of 2 for scalar or length of series */
nextpow2(s)
{
        local n;

        if (isarray(s))
        {
                /* for series, use length */
                n = length(s);
        }
        else
        {
                n = abs(castint(s));
        }
        
        n = ceil(log2(n));
        
        return(n);
}