1 # 8_10.py 2 3 import numpy as np 4 import matplotlib.pyplot as plt 5 from scipy import signal 6 7 # H(z) = (z) / (z - 0.5) => H(z) = 1 / (1 - 0.5z^-1) 8 zeros = [0] 9 poles = [0.5] 10 k = 1 11 12 # zeros = [0, 1] 13 # poles = [0.5, 1.5] 14 # k = 1 15 16 system_zpk = signal.dlti(zeros, poles, k, dt=1.0) 17 18 n_points = 100 19 n_1, y_1 = signal.dimpulse(system_zpk, n=n_points) 20 n_2, y_2 = signal.dstep(system_zpk, n=n_points) 21 22 # la salida de dimpulse es una lista, tomamos el primer elemento 23 h_n = y_1[0].flatten() 24 y_n = y_2[0].flatten() 25 26 fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10)) 27 28 # respuesta al impulso 29 markerline, stemlines, baseline = ax1.stem(n_1, h_n, basefmt=" ") 30 plt.setp(baseline, color='black', linewidth=0) 31 ax1.set_title('Respuesta al Impulso $h[n]$') 32 ax1.set_ylabel('Amplitud') 33 ax1.grid(True, linestyle='--', alpha=0.7) 34 35 # respuesta al escalon 36 markerline, stemlines, baseline = ax2.stem(n_2, y_n, basefmt=" ") 37 plt.setp(baseline, color='black', linewidth=0) 38 ax2.set_title('Respuesta al Escalón $y[n]$') 39 ax2.set_xlabel('Muestra ($n$)') 40 ax2.set_ylabel('Amplitud') 41 ax2.grid(True, linestyle='--', alpha=0.7) 42 43 plt.setp(baseline, color='black', linewidth=1) 44 plt.tight_layout() 45 plt.show() 46 47 # transferencia como diagrama de bode (modulo/fase) 48 w, h = signal.dfreqresp(system_zpk) 49 50 fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10)) 51 52 # magnitud 53 ax1.plot(w, 20 * np.log10(abs(h))) 54 ax1.set_title('Respuesta en Frecuencia (Magnitud)') 55 ax1.set_ylabel('Amplitud [dB]') 56 ax1.grid(True) 57 58 # fase 59 ax2.plot(w, np.angle(h)) 60 ax2.set_title('Respuesta en Frecuencia (Fase)') 61 ax2.set_ylabel('Fase [rad]') 62 ax2.set_xlabel('Frecuencia [rad/muestra]') 63 ax2.grid(True) 64 65 plt.tight_layout() 66 plt.show()
