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

#if @HELP_XMOVVAR

    XMOVVAR

    Purpose: Calculates the "moving" variance of a series given a duration.

    Syntax:  XMOVVAR(series, xdur, dof)

                series - A series, the input series

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

              rampflag - Optional. An integer, the endpoint averaging flag.
                         
                            0: do not ramp
                            1: ramp up (default)

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

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

                           1: Normalize by 1 / N

    Returns: A series or table.

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

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

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

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

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

             W2 computes a 51 point moving variance.

             W3 computes a moving variance with a moving interval of 0.005
             seconds.

             Since a 0.005 interval equates to 51 samples, the two moving
             variance results are equal.

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

               N = XDUR / deltax(s) + 1

             where XDUR >= deltax(s)

             See MOVAVG for more details on RAMPFLAG.

    See Also:
             Movavg
             Movrms
             Movstd
             Movvar
             RMS
             Xmovstd
#endif


/* moving VAR given a duration used to compute the integer sample interval */
ITERATE xmovvar(s, xdur, dof = 0, flag1 = "", flag2 = "")
{
        local y;

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

        return(y);
}