View Raw SPL
/*****************************************************************************
*                                                                            *
*   XBLOCKAVG.SPL  Copyright (C) 2023 DSP Development Corporation            *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Calculates the block average with a duration                *
*                                                                            *
*   Revisions:    1 Aug 2023  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_XBLOCKAVG

    XBLOCKAVG

    Purpose: Calculates the moving block average of a series given a duration.

    Syntax:  XBLOCKAVG(series, xdur, "naflag")

              series   - A series, the input series

              xdur     - A real, the X duration used to compute the integer
                         number of samples for the non-overlapping moving
                         interval.


              "naflag" - Optional.A string, the NA handling method. 
                          
                          "omitnan"    : ignore NA values (default)
                          "includenan" : include NA values
  
    Returns: A series or table.

    Example:
             W1: {1, 2, 4, 7};setdeltax(1/100)
             W2: blockavg(w1, 3)
             W3: xblockavg(w1, 0.02)

             W1 contains the input data with a sample rate of 100 Hz.

             W2 computes a 3 point moving block average.
             
             W3 computes the same moving block average except the block size
             is specified in terms of seconds. Since a 0.02 interval equates
             to 3 samples, both functions return the series:
             
             {2.333, 7}

    Example:
             W1: integ(gnorm(10000, 1/10000)) * 1000
             W2: blockavg(w1, 51)
             W3: xblockavg(w1, 0.005)
             W4: W2 - W3

             W1 synthesizes 10000 samples of data with a sample rate of 10kHz.

             W2 computes a 51 point block average.

             W3 computes a moving block average with an interval of 0.005
             seconds.

             Since a 0.005 interval equates to 51 samples, the two block
             average results are equal.

    Remarks:
             XBLOCKAVG computes a moving average with non-overlapping blocks
             of duration XDUR.

             The block duration XDUR is converted to the integer block
             interval N with:

               N = XDUR / deltax(s) + 1

             where XDUR >= deltax(s)

             See BLOCKAVG for more details on NAFLAG.

    See Also:
             Blockavg
             Blockrms
             Blockstd
             Blockvar
             Mean
             Movavg
             Movrms
             Xblockrms
             Xblockstd
             Xblockvar
#endif


/* block average given a duration used to compute the integer sample interval */
ITERATE xblockavg(s, xdur, flag1 = "", flag2 = "", flag3 = "")
{
        local y;

        /* call common driver function */
        y = xmovfun(s, __FUNC__, strextract(__FUNC__, 2, -1), xdur, nan, nan, flag1, flag2, flag3);

        return(y);
}