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 transfunc(self, polos, **kwargs): wc = kwargs.get('wc', 0) w0 = kwargs.get('w0', 0) Bw = kwargs.get('bw', self.Bp) ordem = kwargs.get('ord', self.ordem) resp = kwargs.get('response', self.tipo) G_db = kwargs.get('G', self.G_bp) fcn = 0 if resp == 'lp': self.den_norm = np.real(np.poly(polos)) denm = np.zeros(len(self.den_norm)) for i in range(0, len(polos) + 1): denm[i] = self.den_norm[i] * np.power(wc, i) num = denm[-1] fcn = signal.TransferFunction((pow(10, -G_db / 20.0)) * num, denm) if resp == 'hp': self.den_norm = np.real(np.poly(polos)) denm = np.zeros(len(polos) + 1) for i in range(0, len(polos) + 1): denm[i] = self.den_norm[len(polos) - i] * np.power(wc, i) num = np.zeros(len(polos) + 1) num[0] = denm[0] fcn = signal.TransferFunction((pow(10, -G_db / 20.0)) * num, denm) if (resp == 'bp'): self.den_norm = np.real(np.poly(polos)) [num, den] = signal.lp2bp(self.den_norm[-1], self.den_norm, w0, Bw) print('EMBAIXO') print(self.den_norm) fcn = signal.TransferFunction((pow(10, -G_db / 20.0)) * num, den) if (resp == 'bs'): self.den_norm = np.real(np.poly(polos)) [num, den] = signal.lp2bs(self.den_norm[-1], self.den_norm, w0, Bw) fcn = signal.TransferFunction((pow(10, -G_db / 20.0)) * num, den) self.fcn = fcn return fcn
def transfunc(self, polos, **kwargs): wc = kwargs.get('wc', 0) w0 = kwargs.get('w0', 0) Bw = kwargs.get('bw', self.Bp) ordem = kwargs.get('ord', self.ordem) fcn = 0 if self.tipo == 'lp': self.den_norm = np.real(np.poly(polos)) denm = np.zeros(len(self.den_norm)) for i in range(0, len(polos) + 1): denm[i] = self.den_norm[i] * np.power(wc, i) num = denm[-1] fcn = signal.TransferFunction(num, denm) if self.tipo == 'hp': self.den_norm = np.real(np.poly(polos)) denm = np.zeros(len(polos) + 1) for i in range(0, len(polos) + 1): denm[i] = self.den_norm[len(polos) - i] * np.power(wc, i) num = np.zeros(len(polos) + 1) num[0] = denm[0] fcn = signal.TransferFunction(num, denm) if (self.tipo == 'bp'): self.den_norm = np.real(np.poly(polos)) [num, den] = signal.lp2bp(self.den_norm[-1], self.den_norm, w0, Bw) fcn = signal.TransferFunction(num, den) if (self.tipo == 'bs'): self.den_norm = np.real(np.poly(polos)) [num, den] = signal.lp2bs(self.den_norm[-1], self.den_norm, w0, Bw) fcn = signal.TransferFunction(num, den) return fcn
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): 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) elif self.tipo == "PF": num, den = signal.lp2bp(coef[-1], coef, self.Wo, self.Bp) H = signal.TransferFunction(num, den) elif self.tipo == "RF": num, den = signal.lp2bs(coef[-1], coef, self.Wo, self.Bp) 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) elif self.tipo == "PF": num, den = signal.lp2bp(coef[-1], coef, self.Wo, self.Bp) H = signal.TransferFunction(num * aux, den) elif self.tipo == "RF": num, den = signal.lp2bs(coef[-1], coef, self.Wo, self.Bp) H = signal.TransferFunction(num * aux, den) self.H = H return H
def transfunc(self, polos, **kwargs): wp = kwargs.get('wp', 0) w0 = kwargs.get('w0', 0) Bw = kwargs.get('bw', self.Bp) ordem = kwargs.get('ord', self.ordem()) resp = kwargs.get('response', self.tipo) G_db = kwargs.get('G', self.G_bp) fcn = 0 if resp == 'lp': self.den_norm = np.real(np.poly(polos)) denm = np.zeros(len(self.den_norm)) for i in range(0, len(polos) + 1): denm[i] = self.den_norm[i] * np.power(wp, i) if (ordem % 2 == 0): num = denm[-1] * (1 / np.sqrt(1 + np.power(self.eps, 2))) else: num = denm[-1] fcn = signal.TransferFunction(pow(10, -G_db / 20.0) * num, denm) if resp == 'hp': self.den_norm = np.real(np.poly(polos)) denm = np.zeros(len(polos) + 1) for i in range(0, len(polos) + 1): denm[i] = self.den_norm[len(polos) - i] * np.power(wp, i) num = np.zeros(len(polos) + 1) if (self.ordem() % 2 == 0): num[0] = denm[0] * (1 / np.sqrt(1 + np.power(self.eps, 2))) else: num[0] = denm[0] fcn = signal.TransferFunction(pow(10, -G_db / 20.0) * num, denm) if (resp == 'bp'): self.den_norm = np.real(np.poly(polos)) [num, den] = signal.lp2bp(self.den_norm[-1], self.den_norm, w0, Bw) if (self.ordem() % 2 == 0): num[0] = num[0] * (1 / np.sqrt(1 + np.power(self.eps, 2))) else: num[0] = num[0] fcn = signal.TransferFunction(num, den) if (resp == 'bs'): self.den_norm = np.real(np.poly(polos)) [num, den] = signal.lp2bs(self.den_norm[-1], self.den_norm, w0, Bw) if (self.ordem() % 2 == 0): num = num[::] * (1 / np.sqrt(1 + np.power(self.eps, 2))) else: num[-1] = num[-1] fcn = signal.TransferFunction(num, den) self.fcn = fcn return fcn
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 transfunc(self, zeros, polos, **kwargs): ws = kwargs.get('ws', 0) w0 = kwargs.get('w0', 0) Bw = kwargs.get('bw', self.Bs) G_db = kwargs.get('G', self.G_bp) ordem = kwargs.get('ord', self.ordem()) fcn = 0 if self.tipo == 'lp': if(len(zeros) == 1): self.num_norm = np.array([1]) else: self.num_norm = np.real(np.poly(zeros)) self.den_norm = np.real(np.poly(polos)) numm = self.num_norm denm = self.den_norm for i in range(0, len(polos) + 1): denm[i] = denm[i]*pow(ws, i) for i in range(0, len(zeros)+1): numm[i] = numm[i]*pow(ws, i) if(self.ativo == True and len(polos) == 2): k = 1 else: k = denm[-1]/numm[-1] fcn = signal.TransferFunction((pow(10, -G_db/20.0))*k*numm, denm) if(self.tipo == 'hp'): self.den_norm = np.real(np.poly(polos)) self.num_norm = np.real(np.poly(zeros)) numm = self.num_norm[::-1] denm = self.den_norm[::-1] for i in range(0, len(polos) + 1): denm[i] = denm[i]*pow(ws, i) for i in range(0, len(zeros)+1): numm[i] = numm[i]*pow(ws, i) if(ordem%2 != 0): numm = np.append(numm, 0) k = denm[0]/numm[0] fcn = signal.TransferFunction((pow(10, -G_db/20.0))*k*numm, denm) if(self.tipo == 'bp'): self.den_norm = np.real(np.poly(polos)) self.num_norm = np.real(np.poly(zeros)) k = self.den_norm[-1]/self.num_norm[-1] numm, denm = signal.lp2bp(k*self.num_norm, self.den_norm, w0, Bw) fcn = signal.TransferFunction((pow(10, -G_db/20.0))*numm, denm) self.fcn = fcn return fcn
def get_max_emg(context, filename, order,f_bp1, f_bp2, f_lp, muscle): with open(filename) as f: reader = csv.reader(f, delimiter=",") header = reader.next() dataMax = np.array([[float(col) for col in row] for row in reader]) with open(filename) as f: reader = csv.reader(f, delimiter=",") header = reader.next() dataMin = np.array([[float(col) for col in row] for row in reader]) fs = len(data)/(data[-1,0]-data[0,0]) fny = fs/2 try: idx = header.index(muscle) except ValueError: return 0 data = data[:,idx] ## HIGH PASS FILTER bp_low = f_bp1 bp_high = f_bp2 centerfreq = (bp_high+bp_low)/2 bandwidthfreq = (bp_high -bp_low) (b,a) = butter(2,0.5) # Transform to band pass (b,a) = lp2bp(b,a,centerfreq/fny, bandwidthfreq/fny) data = filtfilt(b,a,data) data = np.abs(data ) ## Low pass filter envelope lp_low = f_lp (b,a) = butter(2,lp_low/fny) data = filtfilt(b,a,data) ## Get max values dmax = np.max( data ) return (dmax)
def func_tranf(self): butterworth.raizes_unit(self) poli = list() poli = np.poly(self.Sk) coefReal = poli.real 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) elif self.tipo == "PF": num, den = signal.lp2bp(coefReal[-1], coefReal, self.Wo, self.Bw) H = signal.TransferFunction(num, den) elif self.tipo == "RF": num, den = signal.lp2bs(coefReal[-1], coefReal, self.Wo, self.Bw) H = signal.TransferFunction(num, den) self.H = H return H
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
from scipy import signal import matplotlib.pyplot as plt lp = signal.lti([1.0], [1.0, 1.0]) bp = signal.lti(*signal.lp2bp(lp.num, lp.den)) w, mag_lp, p_lp = lp.bode() w, mag_bp, p_bp = bp.bode(w) plt.plot(w, mag_lp, label='Lowpass') plt.plot(w, mag_bp, label='Bandpass') plt.semilogx() plt.grid() plt.xlabel('Frequency [rad/s]') plt.ylabel('Magnitude [dB]') plt.legend()
ripple = 1 # dB eps = np.sqrt(10**(ripple / 10) - 1) # Diseño un Butter. # z,p,k = sig.buttap(nn) z, p, k = sig.besselap(nn, norm='delay') num_lp, den_lp = sig.zpk2tf(z, p, k) # paso de un Butter. a maxima planicidad si eps != 1 # num_lp_shift, den_lp_shift = sig.lp2lp(num_lp,den_lp, eps**(-1/nn) ) num_lp_shift, den_lp_shift = (num_lp, den_lp) # obtengo la transferencia normalizada del pasabanda num_bp_n, den_bp_n = sig.lp2bp(num_lp_shift, den_lp_shift, wo=1, bw=1 / .253) # obtengo la transferencia desnormalizada del pasabanda num_bp, den_bp = sig.lp2bp(num_lp_shift, den_lp_shift, wo=8.367, bw=33) # Averiguo los polos y ceros z_bp_n, p_bp_n, k_bp_n = sig.tf2zpk(num_bp_n, den_bp_n) str_aux = 'Pasabanda normalizado' print(str_aux) print('-' * len(str_aux)) this_lti = sig.TransferFunction(num_bp_n, den_bp_n) pretty_print_lti(this_lti) print('\n\n')
from splane import analyze_sys, pzmap, grpDelay, bodePlot, pretty_print_lti nn = 2 # orden ripple = 1 # dB eps = np.sqrt(10**(ripple / 10) - 1) # Diseño un Butter. z, p, k = sig.buttap(nn) num_lp, den_lp = sig.zpk2tf(z, p, k) # paso de un Butter. a maxima planicidad si eps != 1 num_lp_butter, den_lp_butter = sig.lp2lp(num_lp, den_lp, eps**(-1 / nn)) # obtengo la transferencia normalizada del pasabanda num_bp_n, den_bp_n = sig.lp2bp(num_lp_butter, den_lp_butter, wo=1, bw=1 / .253) # obtengo la transferencia desnormalizada del pasabanda num_bp, den_bp = sig.lp2bp(num_lp_butter, den_lp_butter, wo=8.367, bw=33) # Averiguo los polos y ceros z_bp_n, p_bp_n, k_bp_n = sig.tf2zpk(num_bp_n, den_bp_n) str_aux = 'Pasabanda normalizado' print(str_aux) print('-' * len(str_aux)) this_lti = sig.TransferFunction(num_bp_n, den_bp_n) pretty_print_lti(this_lti) print('\n\n')
# 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) pretty_print_SOS(xp_sos) pretty_print_SOS(xp_sos, mode='omegayq') print_console_subtitle('Respuesta en frecuencia') analyze_sys(xp_sos, str_designed_filter, same_figs=False)