def func_tranf(self): chebyshev.raizes_unit(self) poli = np.poly(self.Sk) coef = poli.real D = list() aux = 0 for i in range(-self.N, 1): D.append(coef[aux] * pow(self.Wp, i)) aux = aux + 1 if self.N % 2 != 0: if self.tipo == "PB": num, den = signal.lp2lp(coef[-1], coef, self.Wp) H = signal.TransferFunction(num[-1], den) elif self.tipo == "PA": num, den = signal.lp2hp(coef[-1], coef, self.Wp) H = signal.TransferFunction(num, den) else: aux = 1 / np.sqrt(1 + self.e**2) if self.tipo == "PB": num, den = signal.lp2lp(coef[-1], coef, self.Wp) H = signal.TransferFunction(num * aux, den) elif self.tipo == "PA": num, den = signal.lp2hp(coef[-1], coef, self.Wp) H = signal.TransferFunction(num * aux, den) self.H = H return H
def func_tranf(self): chebyshev.raizes_unit(self) poli_polos = np.poly(self.Sk) polos = poli_polos.real poli_zeros = list() zeros = list() for i in range(1, self.Nv+1): Wz = 1 / np.cos((2*i - 1) * np.pi / (2*self.N)) poli_zeros.append(complex(0, -Wz)) poli_zeros.append(complex(0, Wz)) poli_zeros = np.poly(poli_zeros) zeros = poli_zeros.real aux = polos[-1] for i in range(0, len(polos)): polos[i] = polos[i] * zeros[-1] for i in range(0, len(zeros)): zeros[i] = zeros[i] * aux if self.tipo == "PB": num, den = signal.lp2lp(zeros, polos, self.Ws) elif self.tipo == "PA": num, den = signal.lp2hp(zeros, polos, self.Ws) elif self.tipo == "PF": num, den = signal.lp2bp(zeros, polos, self.Wo, self.Bs) elif self.tipo == "RF": num, den = signal.lp2bs(zeros, polos, self.Wo, self.Bs) H = signal.TransferFunction(num, den) self.H = H return H
def TransfFreq(self, zeros, polos, Kp): if self.tipo == "PB": num, den = signal.lp2lp(zeros, polos, Kp) elif self.tipo == "PA": num, den = signal.lp2hp(zeros, polos, Kp) elif self.tipo == "PF": num, den = signal.lp2bp(zeros, polos, Kp, self.Bp) elif self.tipo == "RF": num, den = signal.lp2bs(zeros, polos, Kp, self.Bp) H = signal.TransferFunction(num, den) return H
def func_tranf(self): butterworth.raizes_unit(self) poli = list() poli = np.poly(self.Sk) coefReal = poli.real self.Wc = butterworth.freq_corte(self) if self.tipo == "PB": num, den = signal.lp2lp(coefReal[-1], coefReal, self.Wc) H = signal.TransferFunction(num, den) elif self.tipo == "PA": num, den = signal.lp2hp(coefReal[-1], coefReal, self.Wc) H = signal.TransferFunction(num, den) self.H = H return H
def transf(self, ordem, tipo, **kwargs): kf = kwargs.get('kf', 0) Bw = kwargs.get('bw', 0) if (tipo == 'lp'): den = np.zeros(ordem + 1, dtype=float) for i in range(0, ordem + 1): den[i] = self.coeficientes[i] * pow(kf, i) num = den[-1] self.tf = signal.TransferFunction(num, den) if (tipo == 'hp'): num = np.zeros(ordem + 1, dtype=float) den = np.zeros(ordem + 1, dtype=float) num, den = signal.lp2hp(1, self.coeficientes, kf) self.tf = signal.TransferFunction(num, den) return self.tf
def transf(self, ordem, tipo, **kwargs): kf = kwargs.get('kf', 0) w0 = kwargs.get('w0', 0) Bw = kwargs.get('bw', 0) G = kwargs.get('G', 1) Glin = pow(10.0, G / 20.0) if (tipo == 'lp'): den = np.zeros(ordem + 1, dtype=float) for i in range(0, ordem + 1): den[i] = self.coeficientes[i] * pow(kf, i) num = den[-1] if (tipo == 'hp'): num, den = signal.lp2hp(1, self.coeficientes, kf) if (tipo == 'bp'): num, den = signal.lp2bp(self.tf.den[-1], self.tf.den, w0, Bw) self.tf = signal.TransferFunction(Glin * num, den) return self.tf
def fun_trans(self): k = self.raizes() wc = self.freq_corte() Bw = (wc[2] - wc[1]) self.nden = np.real(np.poly(k)) #denominador normalizado den = np.zeros( len(k)) #criando o array para o denominador transformado if self.tipo == 'Passa-baixa': [num, den] = sig.lp2lp(self.nden[-1], self.nden, wc[0]) FT = sig.TransferFunction(num, den) #gerando a FT para a freq. de corte if self.tipo == 'Passa-alta': [num, den] = sig.lp2hp(self.nden[-1], self.nden, wc[0]) FT = sig.TransferFunction(num, den) if self.tipo == 'Passa-faixa': [num, den] = sig.lp2bp(self.nden[-1], self.nden, wc[0], Bw) FT = sig.TransferFunction(num, den) if self.tipo == 'Rejeita-faixa': [num, den] = sig.lp2bs(self.nden[-1], self.nden, wc[0], Bw) FT = sig.TransferFunction(num, den) return FT
def fun_trans(self): raizes = self.raizes() wc = self.freq_corte() n = self.ordem() eps = self.epso() nden = np.real(np.poly(raizes)) if (n % 2 == 0): nnum = nden[-1] * (1 / np.sqrt(1 + np.power(eps, 2))) else: nnum = nden[-1] if self.tipo == 'Passa-baixa': [num, den] = sig.lp2lp(nnum, nden, wc) if self.tipo == 'Passa-alta': [num, den] = sig.lp2hp(nnum, nden, wc) if self.tipo == 'Passa-faixa': Bw = self.Wp2 - self.Wp1 [num, den] = sig.lp2bp(nnum, nden, wc, Bw) if self.tipo == 'Rejeita-faixa': Bw = self.Wp2 - self.Wp1 [num, den] = sig.lp2bs(nnum, nden, wc, Bw) FT = sig.TransferFunction(num, den) return FT
my_tf_lp = transf_f (NUM_lp,DEN_lp) # --------------------------------------------------------------------------- # Ahora Utilizando la función Cheby Tipo 1 dentro del módulo scipy: z, p, k = sig.cheb1ap (orden_filtro, eps) NUM_ch, DEN_ch = sig.zpk2tf ( z, p, k ) # --------------------------------------------------------------------------- # Filtro Destino - Filtro High-Pass: # Calculo w0: NUM_hp, DEN_hp = sig.lp2hp ( NUM, DEN, w0 ) my_tf_hp = transf_f ( NUM_hp, DEN_hp ) my_z_hp, my_p_hp, my_k_hp = sig.tf2zpk (NUM_hp, DEN_hp ) # --------------------------------------------------------------------------- # --------------------------------------------------------------------------- # Filtrado de la Señal: tt, s_filtrada, x = sig.lsim2 ((NUM_hp, DEN_hp), sgnal, t ) # --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed May 8 23:19:00 2019 @author: mariano """ import scipy.signal as sig import numpy as np from splane import analyze_sys, pzmap, grpDelay, bodePlot z, p, k = sig.buttap(3) num_lp, den_lp = sig.zpk2tf(z, p, k) eps = np.sqrt(10**(0.1) - 1) num_lp_d, den_lp_d = sig.lp2lp(num_lp, den_lp, eps**(-1 / 3)) num_hp_d, den_hp_d = sig.lp2hp(num_lp_d, den_lp_d) num_hp_d, den_hp_d = sig.lp2hp(num_lp, den_lp, eps**(-1 / 3)) #%matplotlib qt5 analyze_sys([sig.TransferFunction(num_lp, den_lp)], ['mp_norm']) analyze_sys([sig.TransferFunction(num_lp_d, den_lp_d)], ['mp_desnorm'])
w_zt_lp_n = np.abs(w_zt_lp_n) z_lp_n, p_lp_n, k_lp_n = sig.buttap(orden_filtro) NUM_LP_n, DEN_LP_n = sig.zpk2tf(z_lp_n, p_lp_n, k_lp_n) NUM_LP_n = [1, 0, 9] # Le agrego el cero de transmisión. my_tf_lp_n = transf_f(NUM_LP_n, DEN_LP_n) bodePlot(my_tf_lp_n) pzmap(my_tf_lp_n) # ---------------------------------------------------------------------------- # Filtro Destino High-Pass: NUM_HP_n, DEN_HP_n = sig.lp2hp(NUM_LP_n, DEN_LP_n) z_hp_n, p_hp_n, k_hp_n = sig.tf2zpk(NUM_HP_n, DEN_HP_n) my_tf_hp_n = transf_f(NUM_HP_n, DEN_HP_n) bodePlot(my_tf_hp_n) pzmap(my_tf_hp_n) # Si ahora quiero que para w--> inf haya 0dB: k_hp_n = k_hp_n * (1 / 9) NUM_HP_n, DEN_HP_n = sig.zpk2tf(z_hp_n, p_hp_n, k_hp_n) my_tf_hp_n = transf_f(NUM_HP_n, DEN_HP_n) bodePlot(my_tf_hp_n) # Desnormalizo: w0 = wp_hp
wb_lp = eps**(-1 / orden_filtro) # Omega de Butterworth Low Pass NUM_lp, DEN_lp = sig.lp2lp(NUM, DEN, wb_lp) # Renormalizo con wb_lp else: NUM_lp, DEN_lp = sig.lp2lp(NUM, DEN) # Caso de Butterworth # --------------------------------------------------------------------------- # --------------------------------------------------------------------------- # Llevo mi Filtro Prototipo (Low-Pass) a mi Filtro Destino (High-Pass) if (eps != 1): # Caso de Máxima Planicidad!! wb_hp = 1 / abs(wb_lp) # Omega de Butterworth High Pass NUM_hp, DEN_hp = sig.lp2hp(NUM, DEN, wb_hp) # Renormalizo con wb_hp else: NUM_hp, DEN_hp = sig.lp2hp(NUM, DEN) # Caso de Butterworth # --------------------------------------------------------------------------- # --------------------------------------------------------------------------- # Calculo la Transferencia de mi Filtro Objetivo (High-Pass) my_tf_lp = tf(NUM_lp, DEN_lp) my_tf_hp = tf(NUM_hp, DEN_hp) # --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
from splane import tfadd, tfcascade, analyze_sys, pzmap, grpDelay, bodePlot, pretty_print_lti nn = 2 # orden ripple = 3 # dB eps = np.sqrt(10**(ripple/10)-1) z,p,k = sig.besselap(nn, norm='delay') # z,p,k = sig.buttap(nn) num_lp, den_lp = sig.zpk2tf(z,p,k) num_lp, den_lp = sig.lp2lp(num_lp, den_lp, eps**(-1/nn) ) num_hp, den_hp = sig.lp2hp(num_lp,den_lp) lp_sys = sig.TransferFunction(num_lp,den_lp) hp_sys = sig.TransferFunction(num_hp,den_hp) xover = tfadd(lp_sys, hp_sys) bandpass = tfcascade(lp_sys, hp_sys) pretty_print_lti(lp_sys) pretty_print_lti(hp_sys) pretty_print_lti(xover) pretty_print_lti(bandpass) analyze_sys([lp_sys, hp_sys, xover, bandpass], ['lp', 'hp', 'xover', 'bandpass'])
from scipy import signal import matplotlib.pyplot as plt lp = signal.lti([1.0], [1.0, 1.0]) hp = signal.lti(*signal.lp2hp(lp.num, lp.den)) w, mag_lp, p_lp = lp.bode() w, mag_hp, p_hp = hp.bode(w) plt.plot(w, mag_lp, label='Lowpass') plt.plot(w, mag_hp, label='Highpass') plt.semilogx() plt.grid() plt.xlabel('Frequency [rad/s]') plt.ylabel('Magnitude [dB]') plt.legend()
# High pass chebyshev # Cutoff: 3kHz # Passband ripple: 1dB # Sampling freq: 8000 Hz from scipy import signal import numpy as np import matplotlib.pyplot as plt fs = 8000 N = 512 df = (fs / 2) / N f = np.arange(0, fs / 2, df) B, A = signal.lp2hp([1.9652], [1, 1.9652], wo=38627) b, a = signal.bilinear(B, A, fs) print(b, a) w, h = signal.freqz(b, a, N) fig = plt.figure() plt.title('Digital filter frequency response') ax1 = fig.add_subplot(111) plt.plot(f, 20 * np.log10(abs(h)), 'b') plt.ylabel('Amplitude [dB]', color='b') plt.xlabel('Frequency [Hz]') ax2 = ax1.twinx()
else: print_console_subtitle('Respuesta en frecuencia') analyze_sys(lowpass_proto_lti, '{:s} orden {:d}'.format(aprox_name, this_order)) # Freq. transformation ####################### if aprox_type != 'LP': print_console_alert('Frequency transformation LP -> ' + aprox_type) if aprox_type == 'HP': num, den = sig.lp2hp(lowpass_proto_lti.num, lowpass_proto_lti.den) if aprox_type == 'BP': num, den = sig.lp2bp(lowpass_proto_lti.num, lowpass_proto_lti.den, wo=1, bw=BWbp) xp_tf = sig.TransferFunction(num, den) print_console_subtitle('Transformed transfer function') pretty_print_lti(xp_tf) print_console_subtitle('Cascade of second order systems (SOS' 's)') xp_sos = tf2sos_analog(num, den)