View Raw SPL
/*****************************************************************************
*                                                                            *
*   SLPFREQ.SPL  Copyright (C) 2017 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Frequency response of a digital single pole LPF             *
*                                                                            *
*   Revisions:   15 Nov 2017  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_SLPFREQ

    SLPFREQ

    Purpose: Displays the frequency response of a single pole digital
             low pass filter.

    Syntax:  SLPFREQ(fc, N, Fs, mag)

                fc - A real, the cutoff frequency in Hertz.

                 N - Optional. An integer, the number of frequency samples.
                     Defaults to 4096.

                Fs - Optional. A real, the sample rate of the filter. 
                     Defaults to fc * 1000.

               mag - Optional. An integer, the log scale flag.
                          
                      0: return complex response
                      1: return log magnitude response (default)

    Returns: A series, the frequency response of the filter.

    Example:
             W1: slpfreq(100)

             returns 4096 samples of the magnitude of the frequency
             response for a single pole low pass filter with a cutoff
             frequency of 100 Hz. The result is displayed on a log scale.

    Example:
             W2: slpfreq(100, 10000)

             returns 10000 samples of the magnitude of the frequency
             response for a single pole low pass filter with a cutoff
             frequency of 100 Hz. The result is displayed on a log scale.

    Example:
             W3: slpfreq(10, 6000, 200, 1)

             returns 6000 samples of the complex frequency
             response for a single pole low pass filter with a cutoff
             frequency of 10 Hz and a sample rate of 200 Hz.

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

             See SLP to process data with a single pole low pass
             digital filter.

    See Also:
             Iir
             Slp
             Shp
             Shpfreq

#endif


/* Single pole low pass analog filter */
slpfreq(fc, N, Fs, mag)
{
        local s, a, b;

        if (argc < 4)
        {
                if (argc < 3)
                {
                        if (argc < 2)
                        {
                                if (argc < 1) error("slpfreq - cutoff frequency required");

                                N = 4096
                        }

                        Fs = -1;
                }

                mag = 1;
        }

        if (Fs < 0)
        {
                Fs = fc * 1000;
        }

        /* dummy series with proper sample rate */
        s = {1};
        s.rate = FS;

        /* filter coefficients */
        (b, a) = slp(s, fc);

        /* frequency response */
        f = zfreq(b, a, N, Fs);

        if (not(mag))
        {
                return(f);
        }
        else
        {
                f = 20 * log10(mag(f));

                if (outargc == 0)
                {
                        /* log scale */
                        f;
                        setxlog(1);
                }
                else
                {
                        return(f);
                }
        }
}