View Raw SPL
/*****************************************************************************
*                                                                            *
*   MOVRMS.SPL   Copyright (C) 1999 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Calculates the moving RMS                                   *
*                                                                            *
*   Revisions:    6 May 1999  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_MOVRMS

    MOVRMS

    Purpose: Calculates the moving RMS of a series

    Syntax:  MOVRMS(series, n, rampflag)

              series   - the input series

              n        - an integer, the moving block size

              rampflag - an optional integer, 0:down 1:up, Defaults
                         to 1.

    Returns: A series or table

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

             returns the series {1, 1.581, 2.646, 4.796, 5.701, 7}


             W3: Movrms(w1, length(w1))

             W3[length(w1)] == rms(w1) == 4.183

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

             See XMOVRMS to specify the interval N as a duration.

    See Also:
             Blockrms
             Movavg
             Movstd
             RMS
             Xblockrms
             Xmovrms
#endif


/* moving RMS given a duration used to compute the sample interval N */
ITERATE movrms(s, n, ramp = 1, flag1 = "", flag2 = "", flag3 = "")
{
        local mrms;

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

        if (n < 1) error(sprintf("%s - n must be > 0", __FUNC__));

        if (isstring(ramp))
        {
                if (isstring(flag1) && strlen(flag1) == 0)
                {
                        flag1 = ramp;
                }
                else if (isstring(flag2) && strlen(flag2) == 0)
                {
                        flag2 = ramp;
                }
                else if (isstring(flag3) && strlen(flag3) == 0)
                {
                        flag3 = ramp;
                }

                ramp = 1;
        }

        /* RMS calculation */
        mrms = movavg(s, n, ramp, "rms", flag1, flag2, flag3);

        return(mrms);
}