In [1]:
import numpy as np
from scipy import signal
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
# plotting options
font = {'size' : 20}
plt.rc('font', **font)
plt.rc('text', usetex=True)
matplotlib.rc('figure', figsize=(18, 6) )
In [3]:
delta_Omega = .01
Omega = np.arange( 0, 2 * np.pi + delta_Omega, delta_Omega )
In [4]:
# constant pre-factor of transfer function
const = -2
# switch for choosing different pol-zero constellations
switch = 3
if switch == 1:
zeros = np.array( [ -1, 1] )
poles = np.array( [ .95j, -.95j ] )
elif switch == 2:
zeros = np.array( [] )
poles = np.array( [2] )
else:
const = 0.5
zeros = np.array( [ 0,0,0] )
poles = np.array( [ .5, .25 + 1j * .8, .25 - 1j * .8] )
In [5]:
plt.subplot(121)
plt.plot( np.real( zeros ), np.imag( zeros ) , 'o')
plt.plot( np.real( poles ), np.imag( poles ) , 'x')
phi = np.linspace(0, 2*np.pi, 256)
unit_circle = np.exp(1j*phi)
plt.plot( np.real( unit_circle ) , np.imag( unit_circle ) )
plt.grid( True )
plt.xlabel( 'real' )
plt.xlabel( 'imag' )
Out[5]:
In [6]:
# construct numerator and denominator by multiplying according distances
numerator = np.prod( [ ( np.exp( 1j * Omega ) - z ) for z in zeros ], axis=0 )
denominator = np.prod( [ ( np.exp( 1j * Omega ) - p ) for p in poles ], axis=0 )
# get frequency response
H = const * numerator / denominator
In [7]:
plt.subplot(121)
plt.plot( Omega, np.abs( H ), label='$|H(\\Omega)|$' )
plt.grid( True )
plt.xlabel('$\\Omega$')
plt.ylabel('$|H(\\Omega)|$')
plt.legend(loc='upper right')
plt.subplot(122)
plt.plot( Omega, np.angle( H ), label='$\\angle H(\\Omega)$' )
plt.grid( True )
plt.xlabel('$\\Omega$')
plt.ylabel('$\\angle \\Omega$')
plt.legend(loc='upper right')
Out[7]:
In [8]:
t, h = signal.dimpulse( ( zeros, poles, const, 1 ) )
In [9]:
plt.stem( t, h[0] )
plt.xlim( (-1, 40) )
plt.grid( True )
plt.xlabel('$n$')
plt.ylabel('$h[n]$')
Out[9]:
In [ ]: