View Raw SPL
/*****************************************************************************
*                                                                            *
*   BLOCKAVG.SPL Copyright (C) 2012 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    non-overlapping block average                               *
*                                                                            *
*   Revisions:   11 Jul 2012  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_BLOCKAVG

    BLOCKAVG

    Purpose: Computes a moving block average of a series.

    Syntax:  BLOCKAVG(s, n, "naflag")

               s - A series, the input data.

               n - An integer, the block size to average.

    Returns: A series, the N point moving block average.

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

             W2 == {1.5, 3.5, 5.5, 7.5, 9.5, 11.5}

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

             W4 == {2, 5, 8, 11}

    Remarks:
             BLOCKAVG computes the N point moving non-overlapping block
             average of a series. 

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

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

    See Also:
             Decimate
             Movavg
             Xblockavg
#endif


/* N point moving block average */
blockavg(s, n, flag1 = "", flag2 = "", flag3 = "")
{
        local y;

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

        /* internal moving average with start and blocksize */
        y = movavg(s, n, 1, n, n, flag1, flag2, flag3);

        return(y);
}