View Raw SPL
/*****************************************************************************
*                                                                            *
*   MOVSTD.SPL   Copyright (C) 1999, 2018 DSP Development Corporation        *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Calculates the moving standard deviation                    *
*                                                                            *
*   Revisions:    6 May 1999  RRR  Creation                                  *
*                 3 Jun 2018  RRR  Use faster more accurate builtin          *
*                                                                            *
*****************************************************************************/

#if @HELP_MOVSTD

    MOVSTD

    Purpose: Calculates the centered moving standard deviation of a series.

    Syntax:  MOVSTD(series, n, dof, "nanflag", "edgeflag")

                  series - A series, the input.

                        n - An integer, the block size.

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

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

                             1: Normalize by 1 / N

                 "naflag" - Optional. A string, the NA handling method.

                             "includenan" : include NA values (default)
                                "omitnan" : ignore NA values

               "edgeflag" - Optional. A string, the edge processing flag.

                                "full" : process all segments (default)

                             "discard" : ignore partial segments 

                              "shrink" : shrink the segment size to actual
                                         number of samples at the edge segments  

    Returns: A series or table

    Example:
             W1: {1, 2, 4, 7}
             W2: movstd(w1, 3)

             returns the series {0, 0.707, 1.528, 2.517, 2.121, 0}

    Example:
             W1: {1, 2, 4, 7}
             W3: movstd(w1, length(w1))

             W3[length(w1)] == stdev(w1) == 2.645751

    Remarks:
             For speed, this routines uses the builtin function MOVSTDEV.

             See XMOVSTD to specify the interval N as a duration.

    See Also:
             Blockstd
             Movavg
             Std
             Stdev
             Xblockstd
             Xmovstd
#endif


/* centered moving standard deviation */
ITERATE movstd(s, n, dof = 0, flag1 = "", flag2 = "", flag3 = "")
{
        local mstd;

        /* hurdles */
        if (argc < 2)
        {
                error(sprintf("%s - series and block size required", __FUNC__));
        }

        if (isstring(dof))
        {
                flag3 = flag2;
                flag2 = flag1;
                flag1 = dof;
                dof   = 0;
        }

        /* use faster, more accurate built-in */
        mstd = movstdev(s, n, 1, dof, "includenan", "center", flag1, flag2, flag3);

        return(mstd);
}