Professional Documents
Culture Documents
Info
Lab7 - Practica LAB 7
Hjem Mitt bibliotek Oppdag Spør AI Bøker
In [165]:
%matplotlib inline
import numpy as np
from scipy import signal
In [166]:
def zplane(b,a,auto_scale=True,size=2,detect_mult=True,tol=0.001):
"""
The code module ssd.py was developed to accompany the book
Signals and Systems for Dummies published by Wiley Publishing.
Parameters
----------
b : ndarray of the numerator coefficients
a : ndarray of the denominator coefficients
auto_scale : bool (default True)
size : plot radius maximum when scale = False
Returns
-------
(M,N) : tuple of zero and pole counts + plot window
Notes
-----
This function tries to identify repeated poles and zeros and will
place the multiplicity number above and to the right of the pole or zero.
The difficulty is setting the tolerance for this detection. Currently it
is set at 1e-3 via the function signal.unique_roots.
Examples
--------
>>> # Here the plot is generated using auto_scale
>>> zplane(b,a)
>>> # Here the plot is generated using manual scaling
>>> zplane(b,a,False,1.5)
"""
M = len(b) - 1
N = len(a) - 1
# Plot labels if multiplicity greater than 1
x_scale = 1.5*size
y_scale = 1.5*size
x_off = 0.02
y_off = 0.01
#N_roots = np.array([1.0])
if M > 0:
N_roots = np.roots(b)
#D_roots = np.array([1.0])
if N > 0:
D_roots = np.roots(a)
if auto_scale:
if M > 0 and N > 0:
size = max(np.max(np.abs(N_roots)),np.max(np.abs(D_roots)))+.1
elif M > 0:
Lab7 - Practica LAB 7
elif M > 0:
Hjem size = Mitt
max(np.max(np.abs(N_roots)),1.0)+.1
bibliotek Oppdag Spør AI Bøker
elif N > 0:
size = max(1.0,np.max(np.abs(D_roots)))+.1
else:
size = 1.1
plt.figure(figsize=(5,5))
plt.axis('equal')
r = np.linspace(0,2*np.pi,200)
plt.plot(np.cos(r),np.sin(r),'r--')
plt.plot([-size,size],[0,0],'k-.')
plt.plot([0,0],[-size,size],'k-.')
if M > 0:
if detect_mult == True:
N_uniq, N_mult = unique_cpx_roots(N_roots,tol=tol)
plt.plot(np.real(N_uniq),np.imag(N_uniq),'ko',mfc='None',ms=8)
idx_N_mult = mlab.find(N_mult>1)
for k in range(len(idx_N_mult)):
x_loc = np.real(N_uniq[idx_N_mult[k]]) + x_off*x_scale
y_loc =np.imag(N_uniq[idx_N_mult[k]]) + y_off*y_scale
plt.text(x_loc,y_loc,str(N_mult[idx_N_mult[k]]),ha='center',va='bottom',fontsize=10)
else:
plt.plot(np.real(N_roots),np.imag(N_roots),'ko',mfc='None',ms=8)
if N > 0:
if detect_mult == True:
D_uniq, D_mult=unique_cpx_roots(D_roots,tol=tol)
plt.plot(np.real(D_uniq),np.imag(D_uniq),'kx',ms=8)
idx_D_mult = mlab.find(D_mult>1)
for k in range(len(idx_D_mult)):
x_loc = np.real(D_uniq[idx_D_mult[k]]) + x_off*x_scale
y_loc =np.imag(D_uniq[idx_D_mult[k]]) + y_off*y_scale
plt.text(x_loc,y_loc,str(D_mult[idx_D_mult[k]]),ha='center',va='bottom',fontsize=10)
else:
plt.plot(np.real(D_roots),np.imag(D_roots),'kx',ms=8)
if M - N < 0:
plt.plot(0.0,0.0,'bo',mfc='None',ms=8)
elif M - N > 0:
plt.plot(0.0,0.0,'kx',ms=8)
if abs(M - N) > 1:
plt.text(x_off*x_scale,y_off*y_scale,str(abs(M-N)),ha='center',va='bottom',fontsize=10)
plt.xlabel('Real Part')
plt.ylabel('Imaginary Part')
plt.title('Pole-Zero Plot')
#plt.grid()
plt.axis([-size,size,-size,size])
return M,N
In [167]:
def freqz_mod(b,a):
[w, H] = signal.freqz(b,a,1000,'True') # mil frecuencias espaciadas equidistantemente
H = np.transpose(H[0:501])
w = np.transpose(w[0:501])
magH = np.abs(H)
dB = 20 * np.log10(magH/magH.max())
phase = np angle(H)
Lab7 - Practica LAB 7
phase = np.angle(H)
Hjem Mitt bibliotek
grpdelay = signal.group_delay((b,a)) Oppdag Spør AI Bøker
return dB, phase, grpdelay, w
In [168]:
%pylab inline
def plot_freq_domain(w1, dB1, phase1, fs):
fig,ax = subplots(2, sharex=True)
freq = fs*w1/(2*np.pi) # convierte frecuencias digitales a analogicas
ax[0].plot(freq,dB1)
ax[0].set_ylabel(r"$ |H(\omega)| [dB]$",fontsize=22)
ymin,ymax = -50 ,5+max(dB1)
ax[0].axis(ymin = ymin,ymax=ymax)
ax[0].grid()
ax[1].plot(freq,phase1*180/np.pi)
ax[1].set_xlabel("frequency $\omega / \pi$",fontsize=16)
ax[1].set_ylabel(r"$ Phase [deg]$",fontsize=22)
ax[1].grid()
Populating the interactive namespace from numpy and matplotlib
1- Sistemas IIR
Δω = 0.05 Radianes
ω0 = 0.375 ciclos/muestra
In [169]:
# Oscilador resonante
fs=1200 #Hz
df=9.549 #Hz
f0=71.619 #Hz
# coeficientes:
r = 1 - df*np.pi/fs
w0 = 2*np.pi*f0/fs
b0 = np.abs(np.exp(2j*w0) - 2*r*np.cos(w0)*np.exp(1j* w0)+r**2) / np.abs(np.exp(2j* w0)-1)
Out[169]:
array([ 1. , -1.81449402, 0.95062651])
In [170]:
# Graficar el plano-z
zplane(b,a)
Out[170]:
(2, 2)
Lab7 - Practica LAB 7
Hjem Mitt bibliotek Oppdag Spør AI Bøker
Usando la frecuencia de muestreo f s = 8000 Hz, disenar un filtro comb con las siguientes especificaciones:
n = 11
Δf = 4 KHz
Filtro Notch 1
In [171]:
# Filtro notch 1
fs = 8000 #HZ
f0 = 1000 #Hz
df = 4000 #Hz
# coeficientes:
r = 1 - df*np.pi/fs
w0 = 2*np.pi*f0/fs
b0 = np.abs(1. - 2*r*np.cos(w0)+r**2) / np.abs(2 - 2*np.cos(w0))
Out[171]:
(2, 2)
In [172]:
# Obtener la respuesta de frecuencia
dBb1, phaseb, delayb, wb1 = freqz_mod(b1,a1)
# Graficar
plot_freq_domain(wb1,dBb1,phaseb,fs)
Lab7 - Practica LAB 7
HjemC:\Users\Alvaro\Anaconda3\lib\site-packages\scipy\signal\filter_design.py:536:
Mitt bibliotek Oppdag Spør AI
UserWarning: The groupBøker
delay is singular at frequencies [0.785], setting to 0
format(", ".join("{0:.3f}".format(ws) for ws in w[singular]))
In [173]:
# Filtro notch 2
fs = 8000 #HZ
f0 = 2000 #Hz
df = 4000 #Hz
# coeficientes:
r = 1 - df*np.pi/fs
w0 = 2*np.pi*f0/fs
b0 = np.abs(1. - 2*r*np.cos(w0)+r**2) / np.abs(2 - 2*np.cos(w0))
Out[173]:
(2, 2)
In [174]:
# Obtener la respuesta de frecuencia
dBb2, phaseb, delayb, wb2 = freqz_mod(b2,a2)
# Graficar
plot_freq_domain(wb2,dBb2,phaseb,fs)
In [175]:
# Filtro notch 3
fs = 8000 #HZ
f0 = 3000 #Hz
df = 4000 #Hz
# coeficientes:
r = 1 - df*np.pi/fs
w0 = 2*np.pi*f0/fs
b0 = np.abs(1. - 2*r*np.cos(w0)+r**2) / np.abs(2 - 2*np.cos(w0))
Out[175]:
(2, 2)
In [176]:
# Obtener la respuesta de frecuencia
dBb3, phaseb, delayb, wb3 = freqz_mod(b3,a3)
# Graficar
plot_freq_domain(wb3,dBb3,phaseb,fs)
Gå til kurset
Mini-Proyecto 1 - Nota: 10
Deber 3 dsp