View Raw SPL
/*****************************************************************************
*                                                                            *
*   BLOCKMEDIAN.SPL Copyright (C) 2013 DSP Development Corporation           *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    non-overlapping block median                                *
*                                                                            *
*   Revisions:    4 Jun 2013  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_BLOCKMEDIAN

    BLOCKMEDIAN

    Purpose: Computes a non-overlapping block median of a series.

    Syntax:  BLOCKMEDIAN(s, n, rampflag)

                     s - A series, the input data.

                     n - An integer, the block size to average.

              rampflag - Optional. An integer, endpoint median flag. 

                          0: none
                          1: ramp up and down (default)

    Returns: A series, the N point block median.

    Example:
             W1: 1..12
             W2: blockmedian(w1, 3)
             W3: blockmedian(w1, 4)

             W2 == {2, 5, 8, 11}
             W3 == {2.5, 6.5, 10.5}

    Example:
             W1: 1..12
             W2: blockmedian(w1, 5)
             W3: blockmedian(w1, 5, 0)

             W2 == {3, 8, 11.5}
             W3 == {3, 8, 0}

    Remarks:
             BLOCKMEDIAN computes the N non-overlapping block median of a
             series by decimating the standard N point moving median. 

             If the block size is not an integer factor of the series length,
             the end point median calculation is determined by rampflag. If
             rampflag = 0, the data beyond the end point is assumed to be 0.0.
             If rampflag == 1, the default, the median calculation is adjusted
             for the number of points remaining in the block.

             See MOVMEDIAN for more details on rampflag.

             For multi-column series, BLOCKMEDIAN computes the moving median
             on each column.

    See Also:
             Blockavg
             Decimate
             Movmedian
#endif


/* N point block median */
blockmedian(s, n, rampflag)
{
        local y;

        if (argc < 3)
        {
                if (argc < 2)
                {
                        error("blockmedian - input series and integer blocksize required");
                }

                rampflag = 1;
        }

        /* decimate the N point moving median starting at the Nth sample */
        y = decimate(movmedian(s, n, rampflag), n, n);

        return(y);
}