View Raw SPL
/*****************************************************************************
*                                                                            *
*   SHP.SPL      Copyright (C) 1998-2020 DSP Development Corporation         *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Digital emulation of single pole analog high pass filter    *
*                                                                            *
*   Revisions:   12 Feb 1998  RRR  Creation - from SPOLE.MAC                 *
*                19 Apr 2020  RRR  use filteq, optional filter coef return   *
*                                                                            *
*****************************************************************************/

#if @HELP_SHP

    SHP

    Purpose: Emulates a single pole analog high pass filter.

    Syntax:  SHP(s, fc)

             (b, a) = SHP(s, fc)

             (b, a, yout) = SHP(s, fc)

              s  - input series

              fc - Optional real, cutoff frequency in Hertz, defaults
                   to rate(s) / 10.

    Returns: A series, the low pass filtered result.

             (b, a) = SHP(s, fc) returns the low pass filter coefficients
             derived from the input data.

             (b, a, yout) = SHP(s, fc) returns the filter coefficients and
             the filtered result.

    Example:
             W1: Gsqrwave(100, .01, 4)
             W2: Shp(w1, 10)

             Highpass filters the squarewave in W1 with a cutoff frequency
             of 10 Hz. The sample rate is set to the sample rate of the
             input data.

    Example:
             W1: gsqrwave(100, .01, 4)
             W2: (b, a) = shp(w1, 10);filteq(b, a, w1)

             Same as above accept the filter coefficients are obtained
             and the input data is explicitly processed using
             FILTEQ.

    Remarks:
             The single pole analog highpass filter is implemented in the
             digital domain using the impulse invariance technique.

    See Also:
             Filteq
             Slp
#endif


/* Single pole high pass analog filter */
ITERATE shp(s, fc)
{
        local b1, b, a, rc, ts, hps;

        if (argc < 2)
        {
                if (argc < 1) error("shp - input series required");

                fc = rate(s) / 10;
        }

        /* sample interval */
        ts = deltax(s);

        /* inverse frequency in radians */
        rc = 1 / (2 * pi * fc);

        /* impulse invariant transformation */
        b1 = exp(-1.0 * ts / rc);

        b = ((1 + b1) / 2) * {1, -1};
        a = {1, -b1};

        if (outargc == 2)
        {
                /* (b, a) = shp - just return coefficients */
                return(b, a);
        }

        /* calculate via difference equation */
        hps = filteq(b, a, s);

        if (outargc > 2)
        {
                return(b, a, hps);
        }
        else
        {
                return(hps);
        }
}