You are on page 1of 8

Lab7 - Practica LAB 7

Lab7 - Practica LAB 7

Fag: Procesamiento Digital De Señales (TELG1011) 25 dokumenter

Universitet: Escuela Superior Politécnica del Litoral

Info
Lab7 - Practica LAB 7
Hjem Mitt bibliotek Oppdag Spør AI Bøker

TELG1011 - Practica de Laboratorio #7


Alvaro Sergio Bustamante Moran, (201401205)

Fecha: Julio 25, 2017

In [165]:

%matplotlib inline
import numpy as np
from scipy import signal

In [166]:

import matplotlib.pyplot as plt


from matplotlib import mlab

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.

Copyright 2012, 2013 Mark Wickert, mwickert@uccs.edu

Create an z-plane pole-zero plot.

Create an z-plane pole-zero plot using the numerator


and denominator z-domain system function coefficient
ndarrays b and a respectively. Assume descending powers of z.

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

def unique_cpx_roots(rlist,tol = 0.001):


"""

The average of the root values is used when multiplicity


is greater than one.

Mark Wickert October 2016


"""
uniq = [rlist[0]]
mult = [1]
for k in range(1,len(rlist)):
N_uniq = len(uniq)
for m in range(N_uniq):
if abs(rlist[k]-uniq[m]) <= tol:
mult[m] += 1
uniq[m] = (uniq[m]*(mult[m]-1) + rlist[k])/float(mult[m])
break
uniq = np.hstack((uniq,rlist[k]))
mult = np.hstack((mult,[1]))
return np.array(uniq), np.array(mult)

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

Practica acerca de filtros IIR y diagrama de polos y ceros

1- Sistemas IIR

1.1 Estructuras resonante s

Disenar un oscilador resonante con las siguientes especificaciones:

Δω = 0.05 Radianes

ω0 = 0.375 ciclos/muestra

Utilizaremos el modelo de funcion de transferencia de filtro resonante:

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)

# Creando los coeficientes numerador y denominador usando funcion transferencia H(z)

b = b0* np.array([1.0, 0, -1.0])


a = np.array([1, -2*r*np.cos(w0), r**2])
a

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

1.2 Filtros tipo notch

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))

# Creando los coeficientes numerador y denominador usando funcion transferencia H(z)

b1 = b0 * np.array([1.0, -2*np.cos(w0), 1.0])


a1 = np.array([1, -2*r*np.cos(w0), r**2])
# Graficar el plano-z
zplane(b1,a1)

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))

# Creando los coeficientes numerador y denominador usando funcion transferencia H(z)

b2 = b0 * np.array([1.0, -2*np.cos(w0), 1.0])


a2 = np.array([1, -2*r*np.cos(w0), r**2])
# Graficar el plano-z
zplane(b2,a2)

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)

C:\Users\Alvaro\Anaconda3\lib\site-packages\ipykernel_launcher.py:6: RuntimeWarning: divide by zero en


countered in log10

C:\Users\Alvaro\Anaconda3\lib\site-packages\scipy\signal\filter_design.py:536: UserWarning: The group


delay is singular at frequencies [1.571], setting to 0
f (" " j i ("{0 3f}" f ( ) f i [ i l ]))
Lab7 - Practica LAB 7
format(", ".join("{0:.3f}".format(ws) for ws in w[singular]))
Hjem Mitt bibliotek Oppdag Spør AI Bøker

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))

# Creando los coeficientes numerador y denominador usando funcion transferencia H(z)

b3 = b0 * np.array([1.0, -2*np.cos(w0), 1.0])


a3 = np.array([1, -2*r*np.cos(w0), r**2])
# Graficar el plano-z
zplane(b3,a3)

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)

C:\Users\Alvaro\Anaconda3\lib\site-packages\ipykernel_launcher.py:6: RuntimeWarning: divide by zero en


countered in log10

C:\Users\Alvaro\Anaconda3\lib\site-packages\scipy\signal\filter_design.py:536: UserWarning: The group


delay is singular at frequencies [2.356], setting to 0
format(", ".join("{0:.3f}".format(ws) for ws in w[singular]))

Dokument fortsetter nedenfor

Oppdag mer fra:


Oppdag mer fra:
Procesamiento Digital De Señales
Lab7 - Practica LAB 7
TELG1011
Hjem Mitt
Escuela Superior Politécnica del bibliotek
Litoral Oppdag Spør AI Bøker
25 dokumenter

Gå til kurset

Mini-Proyecto 1 - Nota: 10

26 Procesamiento Digital De Señales 100% (1)

Mini Proyecto 1 - Nota: 10

15 Procesamiento Digital De Señales 100% (1)

Lab8 - Practica LAB 8

6 Procesamiento Digital De Señales 100% (1)

Lab6 - Practica LAB 6

8 Procesamiento Digital De Señales 100% (1)

Deber 3 dsp

2 Procesamiento Digital De Señales Ingen

DSP D1 Kevin Morocho - nothing

8 Procesamiento Digital De Señales Ingen

You might also like