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


#if @HELP_BLOCKVAR

    BLOCKVAR

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

    Syntax:  BLOCKVAR(s, n, rampflag, dof)

                     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 variance.

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

             W2 == {0.5, 0.5, 0.5, 0.5, 0.5, 0.5}

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

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

    Remarks:
             BLOCKVAR computes the N point non-overlapping block variance.

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

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

    See Also:
             Blockavg
             Blockrms
             Blockstd
             Movavg
             Movstd
             Movvar
             Std
             Stdev
             Var
#endif


/* N point moving block variance */
blockvar(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, "variance");

        return(y);
}