View Raw SPL
/*****************************************************************************
*                                                                            *
*   LINAVG.SPL   Copyright (C) 2009-2017 DSP Development Corporation         *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Moving average with zero phase                              *
*                                                                            *
*   Revisions:   24 Aug 2009  RRR  Creation                                  *
*                 9 Mar 2011  RRR  Extraction length                         *
*                 1 Mar 2017  RRR  XY xvalues restore                        *
*                                                                            *
*****************************************************************************/


#if @HELP_LINAVG

    LINAVG

    Purpose: Moving average with zero phase shift

    Syntax:  LINAVG(s, n, ramp)

              s - A series, the input data

              n - An integer, number of points to average 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: linavg(w1, 3)

             W2 contains the series:

             {1.5, 2.167, 3.0, 3.833, 4.5}

             The X offset of the result is 1.0.

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

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

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

    See Also:
             Movavg
             Movavg2
             Reverse
#endif


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

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

        if (n > 1)
        {
                /* phaseless computation by time reversal */
                y = movavg(rev(movavg(rev(s), n, ramp)), n, ramp);

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

                if (isxy(s))
                {
                        /* restore X values */
                        y = xy(xvals(s), y);
                }

                return(y);
        }
        else
        {
                return(s);
        }
}