[SciPy-User] Difference equation to frequency response?

Robert Kern robert.kern at gmail.com
Tue Aug 30 07:55:30 EDT 2016


On Tue, Aug 30, 2016 at 10:35 AM, Matti Viljamaa <mviljamaa at kapsi.fi> wrote:
>
> What’s a simple way to get the frequency response of a filter that’s
expressed as difference equation using SciPy?
>
> Such as this lowpass filter:
>
>
https://kiritchatterjee.wordpress.com/2014/11/10/a-simple-digital-low-pass-filter-in-c/
>
> y[i] = b * x[i] + (1-b) * y[i-1]

This is a so-called Infinite Impulse Response (IIR) filter. In the terms
that scipy.signal works with, we need to convert it to a transfer function,
which is just a ratio of polynomials.


https://en.wikipedia.org/wiki/Infinite_impulse_response#Transfer_function_derivation

To avoid confusion, let me rename your variables (the transfer function
convention uses `b` to mean the array of coefficients of the numerator
polynomial).

  y[i] = alpha * x[i] + (1-alpha) * y[i-1]

So we get:

  b = [alpha]
  a = [1.0, -(1-alpha)]

scipy.signal has a handy class for filters defined in terms of transfer
functions:

  #
http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.TransferFunction.html#scipy.signal.TransferFunction
  from scipy import signal
  tf = signal.TransferFunction(b, a)

And it has a handy method for computing the frequency response (magnitude
and phase):


http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.dlti.freqresp.html#scipy.signal.dlti.freqresp

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20160830/7d71b95e/attachment.html>


More information about the SciPy-User mailing list