View Raw SPL
/*****************************************************************************
*                                                                            *
*   MOVMEAN.SPL  Copyright (C) 2022 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Calculates the centered moving average                      *
*                                                                            *
*   Revisions:    9 Nov 2022  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_MOVMEAN

    MOVMEAN

    Purpose: Calculates the centered moving average of a series.

    Syntax:  MOVMEAN(series, n, "nanflag", "edgeflag")

                  series - A series, the input.

                        n - An integer, the block size.

                 "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: movmean(w1, 3)

             returns the series {1.5, 2.3333, 4.3333, 5.5}

    Example:
             W1: {1, 2, 4, 7}
             W3: movmean(w1, length(w1), "discard")

             W3[1] == mean(w1) == 3.5

    Remarks:   
             See XMOVMEAN to specify the interval N as a duration.

             See MOVAVG for details on NAFLAG and EDGEFLAG.

    See Also:
             Blockavg
             Mean
             Movavg
             Xblockmean
             Xmovmean
#endif


/* centered moving average */
movmean(s, n, flag1 = "", flag2 = "", flag3 = "")
{
        local y;

        if (argc < 2) error(sprintf("%s - series and integer required", __FUNC__));

        y = movavg(s, n, 1, "includenan", "center", flag1, flag2, flag3);

        return(y);
}