How To Plot Frequency Response Of Cw Filter In Python
Compute the frequency response of a digital filter.
Given the M-order numerator b and Due north-guild denominator a of a digital filter, compute its frequency response:
jw - jw - jwM jw B ( east ) b [ 0 ] + b [ ane ] e + ... + b [ G ] e H ( e ) = ------ = ----------------------------------- jw - jw - jwN A ( e ) a [ 0 ] + a [ i ] due east + ... + a [ North ] e
- Parameters
-
- b array_like
-
Numerator of a linear filter. If b has dimension greater than 1, information technology is causeless that the coefficients are stored in the first dimension, and
b.shape[1:]
,a.shape[1:]
, and the shape of the frequencies array must be compatible for broadcasting. - a array_like
-
Denominator of a linear filter. If b has dimension greater than ane, it is assumed that the coefficients are stored in the first dimension, and
b.shape[1:]
,a.shape[i:]
, and the shape of the frequencies array must be uniform for dissemination. - worN {None, int, array_like}, optional
-
If a single integer, then compute at that many frequencies (default is Northward=512). This is a user-friendly alternative to:
np . linspace ( 0 , fs if whole else fs / 2 , N , endpoint = include_nyquist )
Using a number that is fast for FFT computations can result in faster computations (come across Notes).
If an array_like, compute the response at the frequencies given. These are in the aforementioned units equally fs.
- whole bool, optional
-
Usually, frequencies are computed from 0 to the Nyquist frequency, fs/2 (upper-half of unit-circle). If whole is Truthful, compute frequencies from 0 to fs. Ignored if worN is array_like.
- plot callable
-
A callable that takes ii arguments. If given, the return parameters w and h are passed to plot. Useful for plotting the frequency response inside
freqz
. - fs bladder, optional
-
The sampling frequency of the digital system. Defaults to two*pi radians/sample (and so w is from 0 to pi).
New in version i.2.0.
- include_nyquist bool, optional
-
If whole is False and worN is an integer, setting include_nyquist to Truthful will include the last frequency (Nyquist frequency) and is otherwise ignored.
New in version 1.v.0.
- Returns
-
- due west ndarray
-
The frequencies at which h was computed, in the same units as fs. By default, west is normalized to the range [0, pi) (radians/sample).
- h ndarray
-
The frequency response, equally circuitous numbers.
Notes
Using Matplotlib'due south matplotlib.pyplot.plot
function as the callable for plot produces unexpected results, as this plots the real part of the complex transfer office, not the magnitude. Endeavor lambda w, h: plot(west, np.abs(h))
.
A directly computation via (R)FFT is used to compute the frequency response when the following conditions are met:
-
An integer value is given for worN.
-
worN is fast to compute via FFT (i.eastward.,
next_fast_len(worN)
equals worN). -
The denominator coefficients are a single value (
a.shape[0] == one
). -
worN is at to the lowest degree every bit long as the numerator coefficients (
worN >= b.shape[0]
). -
If
b.ndim > 1
, thenb.shape[-one] == one
.
For long FIR filters, the FFT approach can have lower error and be much faster than the equivalent direct polynomial calculation.
Examples
>>> from scipy import indicate >>> b = point . firwin ( eighty , 0.5 , window = ( 'kaiser' , 8 )) >>> due west , h = signal . freqz ( b )
>>> import matplotlib.pyplot as plt >>> fig , ax1 = plt . subplots () >>> ax1 . set_title ( 'Digital filter frequency response' )
>>> ax1 . plot ( west , twenty * np . log10 ( abs ( h )), 'b' ) >>> ax1 . set_ylabel ( 'Amplitude [dB]' , color = 'b' ) >>> ax1 . set_xlabel ( 'Frequency [rad/sample]' )
>>> ax2 = ax1 . twinx () >>> angles = np . unwrap ( np . angle ( h )) >>> ax2 . plot ( w , angles , 'g' ) >>> ax2 . set_ylabel ( 'Angle (radians)' , colour = 'thousand' ) >>> ax2 . grid () >>> ax2 . axis ( 'tight' ) >>> plt . show ()
Broadcasting Examples
Suppose we have two FIR filters whose coefficients are stored in the rows of an array with shape (2, 25). For this sit-in, we'll use random data:
>>> rng = np . random . default_rng () >>> b = rng . random (( 2 , 25 ))
To compute the frequency response for these 2 filters with one phone call to freqz
, we must pass in b.T
, because freqz
expects the outset axis to agree the coefficients. Nosotros must and then extend the shape with a trivial dimension of length ane to allow broadcasting with the array of frequencies. That is, nosotros pass in b.T[..., np.newaxis]
, which has shape (25, 2, i):
>>> w , h = signal . freqz ( b . T [ ... , np . newaxis ], worN = 1024 ) >>> due west . shape (1024,) >>> h . shape (two, 1024)
Now, suppose nosotros have ii transfer functions, with the aforementioned numerator coefficients b = [0.v, 0.5]
. The coefficients for the ii denominators are stored in the first dimension of the 2-D array a:
a = [ 1 1 ] [ - 0.25 , - 0.5 ]
>>> b = np . array ([ 0.5 , 0.5 ]) >>> a = np . array ([[ 1 , ane ], [ - 0.25 , - 0.5 ]])
Simply a is more than 1-D. To get in uniform for broadcasting with the frequencies, we extend information technology with a trivial dimension in the call to freqz
:
>>> w , h = signal . freqz ( b , a [ ... , np . newaxis ], worN = 1024 ) >>> w . shape (1024,) >>> h . shape (2, 1024)
How To Plot Frequency Response Of Cw Filter In Python,
Source: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.freqz.html
Posted by: heidrickwred1975.blogspot.com
0 Response to "How To Plot Frequency Response Of Cw Filter In Python"
Post a Comment