View Raw SPL
/*****************************************************************************
*                                                                            *
*   MOVRMS2.SPL  Copyright (C) 2015 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Moving RMS with end point padding                           *
*                                                                            *
*   Revisions:   14 Oct 2015  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_MOVRMS2

    MOVRMS2

    Purpose: Moving RMS with end point padding

    Syntax:  MOVRMS2(s, n)

              s - a series, the input data

              n - an integer, number of points for the RMS as the series is
                  processed


    Returns: A series


    Example:
             W1: 1..5
             W2: movrms(w1, 3)
             W3: movrms2(w1, 3)

             W2 contains the series:

             {1.000, 1.581, 2.160, 3.109, 4.082, 4.528, 5.000}

             and W3 contains the series:

             {1.414, 2.160, 3.109, 4.082, 4.690}

             The standard moving average ramps the average up and down
             at the end points whereas MOVRMS2 pads the endpoints.

    Remarks:
             MOVRMS2 is an adjusted moving RMS.  The standard
             MOVRMS function performs a "stepped up" RMS at the
             start of the series and a "stepped down" RMS at the
             end.  MOVRMS2 pads the end points with the initial and
             final values before calculating the moving RMS.

    See Also:
             Movavg
             Movavg2
             Movrms
#endif


/* moving RMS with end point padding */
ITERATE movrms2(s, n)
{
        local l, t;

        /* check input arguments */
        if (argc < 2) error("movrms2 - input series number of points required");

        l = length(s);

        /* pad end points with first and last values */
        t = concat(rep( {s[1, ..]}, n), s, rep( {s[l, ..]}, n));

        /* calculate moving RMS and extract proper region */
        t = extract(movrms(t, n), n + floor(n / 2) + 1, l);

        /* reset series attributes */
        setxoffset(t, xoffset(s));
        setdeltax(t, deltax(s));
        sethunits(t, gethunits(s));
        setvunits(t, getvunits(s));

        /* preserve date/time */
        setdate(t, getdate(s));
        settime(t, gettime(s));

        return(t);
}