View Raw SPL
/*****************************************************************************
*                                                                            *
*   BLOCKSTD.SPL Copyright (C) 2017-2018 DSP Development Corporation         *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    non-overlapping block STD                                   *
*                                                                            *
*   Revisions:    5 May 2017  RRR  Creation                                  *
*                 3 Jun 2018  RRR  Use faster more accurate builtin          *
*                                                                            *
*****************************************************************************/


#if @HELP_BLOCKSTD

    BLOCKSTD

    Purpose: Computes a moving non-overlapping block standard deviation
             of a series.

    Syntax:  BLOCKSTD(s, n, rampflag)

                     s - A series, the input data.

                     n - An integer, the block size to compute the STD.

              rampflag - an optional integer, 0:down, 1:up. Defaults
                         to 1.

                   dof - Optional. An integer, degrees of freedom
                         normalization.

                           0: Normalize by 1 / (N - 1) (default)

                           1: Normalize by 1 / N


    Returns: A series, the N point moving block standard deviation.

    Example:
             W1: 1..12
             W2: blockstd(w1, 2)

             W2 == {0.707, 0.707, 0.707, 0.707, 0.707, 0.707}

    Example:
             W3: 1..12
             W4: blockstd(w1, 3)

             W4 == {1, 1, 1, 1}

    Remarks:
             BLOCKSTD computes the moving N point non-overlapping block
             standard deviation of a series.

             If the block size is not an integral fraction of the series
             length, the end points are not processed.

             See XBLOCKSTD to specify the block size in terms of a duration.

    See Also:
             Decimate
             Movstd
             Stdev
             Xblockstd
#endif


/* N point moving block standard deviation */
blockstd(s, n, rampflag = 1, dof = 0, flag1 = "", flag2 = "")
{
        local y;

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

                rampflag = 1;
        }

        if (isstring(rampflag))
        {
                flag2 = flag1;
                flag1 = rampflag;

                rampflag = 1;
        }

        if (isstring(dof))
        {
                flag2 = flag1;
                flag1 = dof;

                dof = 0;
        }

        /* use faster more accurate builtin */
        y = movstdev(s, n, rampflag, dof, n, n, flag1, flag2);

        return(y);
}