View Raw SPL
/*****************************************************************************
*                                                                            *
*   LINRMS.SPL   Copyright (C) 2012 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Moving RMS with zero phase                                  *
*                                                                            *
*   Revisions:   13 Apr 2012  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_LINRMS

    LINRMS

    Purpose: Moving RMS with zero phase shift

    Syntax:  LINRMS(s, n, ramp)

              s - A series, the input data

              n - An integer, number of points per block to compute
                  the RMS as the series is processed

           ramp - Optional, an integer. Ramp the calculation at the endpoints:

                    0: do not ramp
                    1: ramp up at the beginning and down at the end (default)

    Returns: A series

    Example:
             W1: 1..5
             W2: linrms(w1, 3)

             W2 contains the series:

             {1.65, 2.369, 3.215, 3.951, 4.552}


             The X offset of the result is 1.0.

   Example:
             W1: integ(gnorm(1000, 1/100))
             W2: movavg(w1, 10)
             W3: linrms(w1, 10);overp(abs(w1), lred);overp(w2, lgreen)

             W1 contains 1000 samples of synthesized data.
             W2 performs a 10 point standard moving RMS.
             W3 performs a phaseless moving RMS by revering the original
             data, computing a 10 point moving RMS, reversing the result
             and computing another 10 point moving RMS. Compared to
             the standard moving RMS, the peaks of the resulting smoothed 
             series line up with the absolute value of the original data.

    Remarks:
             LINRMS computes a phaseless moving RMS by reversing the
             input series, computing an N point moving RMS, reversing
             the result and computing another N point moving RMS. The
             reversal steps help ensure that the peak locations of the
             original data are preserved.

    See Also:
             Movavg
             Movrms
             Reverse
#endif


/* compute a phaseless moving RMS */
ITERATE linrms(s, n, ramp)
{
        local y;

        if (argc < 3)
        {
                if (argc < 2)
                {
                        if (argc < 1) error("linrms - input series required");
                        
                        n = 10;
                }
                
                ramp = 1;
        }

        /* phaseless computation by time reversal */
        y = movrms(rev(movrms(rev(s), n, ramp)), n, ramp);

        /* remove start and end points */
        y = extract(y, n, length(s), xoffset(s));

        return(y);
}