def _init_sub(self): """further init function""" sys = signal.lti(self._num, self._den) if self._fmin is not None and self._fmax is not None : wnp = np.logspace(np.log10(self._fmin*2*np.pi), np.log10(self._fmax*2*np.pi), num=1000) # consider frequencies w, mag, ph = signal.bode(sys, w=wnp) f = w/(2.*np.pi) # consider frequencies else: w, mag, ph = signal.bode(sys, n=1000) f = w/(2.*np.pi) # consider frequencies self._fmin = f[0] self._fmax = f[-1] #self.aM.tick_params(labelsize='x-small') #self.aP.tick_params(labelsize='x-small') self._aM_bl, = self.aM.semilogx(f, mag) self.aM.set_xlim([self._fmin, self._fmax]) [ymin, ymax] = self.aM.get_ylim() dy = (ymax - ymin) * 0.05 self.aM.set_ylim([ymin - dy, ymax + dy]) self.aM.set_ylabel('magnitude (dB)', size='small') self._aP_bl, = self.aP.semilogx(f, ph) self.aP.set_xlim([self._fmin, self._fmax]) [ymin, ymax] = self.aP.get_ylim() dy = (ymax - ymin) * 0.05 self.aP.set_ylim([ymin - dy, ymax + dy]) self.aP.set_ylabel('phase (degrees)', size='small') self._aM_pl, = self.aM.plot([], [], 'ro') self._aP_pl, = self.aP.plot([], [], 'ro') viniz = (np.log10(self._fmax) - np.log10(self._fmin))/4. + \ np.log10(self._fmin) self._slider = Slider(self.aS, r'freq', np.log10(self._fmin), np.log10(self._fmax), valinit=viniz) self._slider.vline.set_alpha(0.) self._slider.label.set_fontsize('small') self._slider.on_changed(self._onchange) txts = filter(lambda x: type(x)==mpl.text.Text, self.aS.get_children()) txts[1].set_visible(False) self.aT.set_axis_bgcolor('0.95') self.aT.xaxis.set_ticks_position('none') self.aT.yaxis.set_ticks_position('none') self.aT.spines['right'].set_visible(False) self.aT.spines['left'].set_visible(False) self.aT.spines['bottom'].set_visible(False) self.aT.spines['top'].set_visible(False) self.aT.set_xticks([]) self.aT.set_yticks([]) self._txt['f'] = self.aT.text(0, 0, '') self._txt['m'] = self.aT.text(0.34, 0, '') self._txt['p'] = self.aT.text(0.73, 0, '')
def tfc (num, den, freq, mf='std', pf='rad'): """Calculate magnitude and phase of a transfer function for a given frequency. Parameters: num, den: 1D-arrays see scipy.signal.lti for documentation freq: scalar frequency (Hz) mf: string, optional, default: 'std' accepted values: 'std', 'dB' pf: string, optional, default: 'rad' accepted values: 'rad', 'deg' Returns: mag, ph mag: scalar if mf valid else None its unity measure depends on mf NB: mag_dB = 20 * log10(mag_std) ph: scalar if pf valid else None its unity measure depends on pf """ mag, ph = None, None sys = signal.lti(num, den) w, magl, phl = signal.bode(sys, w=[2*np.pi*freq]) mag_dB, ph_deg = magl[0], phl[0] mag_std, ph_rad = 10**(mag_dB/20.), ph_deg/180.*np.pi if mf == 'std': mag = mag_std elif mf == 'dB': mag = mag_dB if pf == 'rad': ph = ph_rad elif pf == 'deg': ph = ph_deg return mag, ph
def bode_plot(): # パラメータ設定 m = 1 c = 1 k = 400 A = np.array([[0, 1], [-k/m, -c/m]]) B = np.array([[0], [k/m]]) C = np.array([1, 0]) D = np.array([0]) s1 = signal.lti(A, B, C, D) w, mag, phase = signal.bode(s1, np.arange(1, 500, 1)) # プロット plt.figure(1) plt.subplot(211) plt.loglog(w, 10**(mag/20)) plt.ylabel("Amplitude") plt.axis("tight") plt.subplot(212) plt.semilogx(w, phase) plt.xlabel("Frequency[Hz]") plt.ylabel("Phase[deg]") plt.axis("tight") plt.ylim(-180, 180) plt.savefig('../files/150613ABCD01.svg')
def second_ordre(f0, Q, filename='defaut.png', type='PBs', f=None): '''Petite fonction pour faciliter l'utilisation de la fonction "bode" du module "signal" quand on s'intéresse à des filtres du 2e ordre. Il suffit de donner la fréquence propre f0 et le facteur de qualité Q pour obtenir ce que l'on veut. Autres paramètres: * filename: le nom du fichier ('defaut.png' par défaut) * type: le type du filtre, à choisir parmi 'PBs' (passe-bas), 'PBd' (passe-bande) et 'PHt' (passe-haut). On peut aussi définir soi-même le numérateur sous forme d'une liste de plusieurs éléments, le degré le plus haut donné en premier. NB: le '1.01' des définitions est juste là pour améliorer sans effort le rendu graphique. * f: les fréquences à échantillonner (si None, la fonction choisit d'elle-même un intervalle adéquat). ''' den = [1. / f0**2, 1. / (Q * f0), 1] # Le dénominateur de la fonction de transfert if type == 'PBs': num = [1.01] # Le numérateur pour un passe-bas elif type == 'PBd': num = [1.01 / (Q * f0), 0] # pour un passe-bande elif type == 'PHt': num = [1.01 / f0**2, 0, 0] # pour un passe-haut else: # sinon, c'est l'utilisateur qui le définit. num = type # Définition de la fonction de transfert s1 = signal.lti(num, den) # Obtention des valeurs adéquates f, GdB, phase = signal.bode(s1, f) # Dessin du diagramme proprement dit diag_bode(f, GdB, phase, filename)
def plot_bode(self,b,c): s1 = signal.lti(b,c) w, mag, phase = signal.bode(s1) plt.figure() plt.grid() plt.semilog(w, mag) plt.semilog(w, phase) plt.show()
def plot(self): ''' Here goes the matplotlib magic: this function generates the Bode diagram ''' w, mag, phase = signal.bode(self.sig) self.graphMg.clear() self.graphPh.clear() self.graphMg.plot(w, mag) self.graphPh.plot(w, phase, 'r')
def test_05(self): # Test that bode() finds a reasonable frequency range. # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) n = 10 # Expected range is from 0.01 to 10. expected_w = np.logspace(-2, 1, n) w, mag, phase = bode(system, n=n) assert_almost_equal(w, expected_w)
def test_03(self): # Test bode() magnitude calculation. # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) jw = w * 1j y = np.polyval(system.num, jw) / np.polyval(system.den, jw) expected_mag = 20.0 * np.log10(abs(y)) assert_almost_equal(mag, expected_mag)
def test_04(self): # Test bode() phase calculation. # 1st order low-pass filter: H(s) = 1 / (s + 1) system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) jw = w * 1j y = np.polyval(system.num, jw) / np.polyval(system.den, jw) expected_phase = np.arctan2(y.imag, y.real) * 180.0 / np.pi assert_almost_equal(phase, expected_phase)
def test_02(self): # Test bode() phase calculation (manual sanity check). # 1st order low-pass filter: H(s) = 1 / (s + 1), # angle(H(s=0.1)) ~= -5.7 deg # angle(H(s=1)) ~= -45 deg # angle(H(s=10)) ~= -84.3 deg system = lti([1], [1, 1]) w = [0.1, 1, 10] w, mag, phase = bode(system, w=w) expected_phase = [-5.7, -45, -84.3] assert_almost_equal(phase, expected_phase, decimal=1)
def test_01(self): # Test bode() magnitude calculation (manual sanity check). # 1st order low-pass filter: H(s) = 1 / (s + 1), # cutoff: 1 rad/s, slope: -20 dB/decade # H(s=0.1) ~= 0 dB # H(s=1) ~= -3 dB # H(s=10) ~= -20 dB # H(s=100) ~= -40 dB system = lti([1], [1, 1]) w = [0.1, 1, 10, 100] w, mag, phase = bode(system, w=w) expected_mag = [0, -3, -20, -40] assert_almost_equal(mag, expected_mag, decimal=1)
def update(**kwargs): lti.update(**kwargs) g_impulse.data_source.data['x'], g_impulse.data_source.data['y'] = ( impulse(lti.sys, T=t)) g_step.data_source.data['x'], g_step.data_source.data['y'] = ( step(lti.sys, T=t)) g_poles.data_source.data['x'] = lti.sys.poles.real g_poles.data_source.data['y'] = lti.sys.poles.imag w, mag, phase, = bode(lti.sys) g_bode_mag.data_source.data['x'] = w g_bode_mag.data_source.data['y'] = mag g_bode_phase.data_source.data['x'] = w g_bode_phase.data_source.data['y'] = phase push_notebook()
def test_from_state_space(self): # Ensure that bode works with a system that was created from the # state space representation matrices A, B, C, D. In this case, # system.num will be a 2-D array with shape (1, n+1), where (n,n) # is the shape of A. # A Butterworth lowpass filter is used, so we know the exact # frequency response. a = np.array([1.0, 2.0, 2.0, 1.0]) A = linalg.companion(a).T B = np.array([[0.0], [0.0], [1.0]]) C = np.array([[1.0, 0.0, 0.0]]) D = np.array([[0.0]]) with suppress_warnings() as sup: sup.filter(BadCoefficients) system = lti(A, B, C, D) w, mag, phase = bode(system, n=100) expected_magnitude = 20 * np.log10(np.sqrt(1.0 / (1.0 + w**6))) assert_almost_equal(mag, expected_magnitude)
def plotzpk(zz, pp, kk, legend_txt, figval=1): sys = sig.ZerosPolesGain(zz, -1 * pp, kk) freq = np.logspace(-2, 2, 500) freq, mag, phase = sig.bode(sys, freq) mag = 10.0**(mag / 20.0) phase = (phase + 180.0) % (360.0) - 180.0 fig = plt.figure(figval) plt.subplot(211) plt.loglog(freq, mag, label=legend_txt) plt.legend() plt.grid(True) plt.ylabel('Gain') plt.subplot(212) plt.semilogx(freq, phase) plt.grid(True) plt.ylim([-190, 190]) plt.ylabel('Phase [deg]') plt.xlabel('Frequency [Hz]') figurename = 'result' + str(figval) + '.png' fig.savefig('./static/' + figurename) return figurename
def bode2(self, num, den): w, mag, phase = signal.bode((num, den)) plt.figure("Bode") plt.ion() plt.rc('font', family='serif') plt.rc('xtick', labelsize='x-small') plt.rc('ytick', labelsize='x-small') f, axarr = plt.subplots(2, sharex=True) plt.subplot(211) plt.semilogx(w, mag, 'black', label='OL TF') plt.legend(loc='upper right') plt.title('Bode Diagram') plt.ylabel('Magnitude (dB)') plt.grid(True, which="both") plt.subplot(212) plt.semilogx(w, phase, 'black', label='Phase') plt.ylabel('Phase (Deg)') plt.xlabel('Frequency (rad/s)') plt.grid(True, which="both") return plt.show()
def computarMinMaxGain( self, min_freq, max_freq ): # conseguir minima y maxima ganancia de la etapa dado un rango de frecuencias self.tf = conseguir_tf(self.transfer_expression, self.var) w, mag, pha = signal.bode( self.tf, logspace(log10(min_freq), log10(max_freq), 10000)) self.w = w self.mag = mag minGain = 1e8 maxGain = -1e8 for m in self.mag: minGain = min(minGain, m) maxGain = max(maxGain, m) self.minGain = minGain self.maxGain = maxGain if config.debug: print("Ganancia mínima:", minGain) print("Ganancia máxima:", maxGain)
def denormalize_range(self): if self.denorm_percent != 0: w=np.linspace(1,self.wan,100000) w,mag,phase=signal.bode(self.norm_sys,w) for i in range(0,len(mag)):#Frequency where H(s)=Aa is found a=np.absolute(-mag[i]-self.Ao) if np.absolute(-mag[i]-self.Ao)<0.001: break max_denorm_w=(self.wan/w[i])*0.9999#In logarithmic scale wan/frec previously found indicates the maximum frequency that can be scaled diff=max_denorm_w-1#Difference between max denorm frequency and wp=1 denorm_w=max_denorm_w-diff*(1-self.denorm_percent)#Depending on denormalization percent denorm frequency will vary betwen 1 and max_denorm den=np.poly1d([1]) num=np.poly1d([1]) for pole in self.poles: den=den*np.poly1d([-1/(denorm_w*pole),1]) for zero in self.zeroes: num=num*np.poly1d([1/(denorm_w*zero),1]) self.den=den self.poles=np.roots(self.den) self.num=num self.zeroes=np.roots(self.num) self.norm_sys = signal.TransferFunction(self.num,self.den) #Filter system is obtained
def run(dlc=None, dlc_noipc=None, SAVE=None): f = np.linspace(0, 1.5, 1000)[1:] Yol = OLResponse.Response(18) Ycl = Spectrum(dlc(wsp=18, controller='ipcpi')[0]) C = make() P = BladeModel.Blade(18) sys = ControlDesign.Turbine(P, C) mag = signal.bode(sys.S, w=f * (2 * np.pi))[1] #actualMag = 20*np.log10(Ycl(f)/Yol(f)) w, H = signal.freqresp(sys.S, n=100000) f = w / (2 * np.pi) Ycl_pred = abs(H) * Yol(f) fig, ax = ControllerEvaluation.magplotSetup(F1p=0.16) ax.set_xlim(0.05, 1.5) ax.axhline(0, lw=1, c='0.8', ls='-') ax.plot(f, Yol(f), label='Open loop') ax.plot(f, Ycl_pred, '--k', label='Closed loop (linear)') ax.plot(f, Ycl(f), 'r', label='Closed loop (HAWC2)') #ax.plot(f, mag, label='$S(C_{PI})$, linear model') #ax.plot(f, actualMag, '--', label='$S(C_{PI})$, HAWC2 model') ax.set_ylabel('Magnitude [m]') ax.set_xlim(0.05, 1.5) ax.set_ylim(0.01, 0.4) ax.set_yscale('log') ax.legend(loc='upper right') if SAVE: plt.savefig(SAVE, dpi=200, bbox_inches='tight') plt.show() print()
def denormalize_range(self): if self.denorm_percent != 0: w = np.linspace(1, self.wan, 100000) w, mag, phase = signal.bode(self.norm_sys, w) for i in range(0, len(mag)): a = np.absolute(-mag[i] - self.Ap) if np.absolute(-mag[i] - self.Ap) < 0.001: break max_denorm_w = w[i] diff = max_denorm_w - 1 denorm_w = max_denorm_w - diff * (1 - self.denorm_percent) den = np.poly1d([1]) num = np.poly1d([1]) for pole in self.poles: den = den * np.poly1d([-denorm_w / (pole), 1]) for zero in self.zeroes: num = num * np.poly1d([denorm_w / (zero), 1]) self.den = den self.poles = np.roots(self.den) self.num = num self.zeroes = np.roots(self.num) self.norm_sys = signal.TransferFunction( self.num, self.den) #Filter system is obtained
def bode(system, fig=None, axes=None): # calculate bode plot for system w, mag, phase = signal.bode(system, w=np.logspace(-1, 2.5, 1000)) f = w / (2 * pi) # set up plot if axes is None: fig, axes = plt.subplots(2, 1, figsize=[4, 4], sharex=True) plt.subplots_adjust(hspace=0.05) axes[0].grid('off') axes[1].grid('off') axes[0].set_ylabel('Magnitude [dB]') axes[1].set_ylabel('Phase [$^o$]') axes[1].set_xlabel('Frequency [rad/s]') axes[0].set_xscale('log') axes[0].axhline(0, linestyle='-', color='0.7', lw=1) axes[1].axhline(0, linestyle='-', color='0.7', lw=1) # plot axes[0].plot(f, mag) axes[1].plot(f, phase) axes[0].autoscale(axis='x', tight=True) return fig, axes
def kmax_pbd(): g = float(input()) f1 = float(input()) etha = float(input()) w0 = f1 * 2 * np.pi #de las ecuaciones, decia que w_max = w0 k = 2 * etha * g / w0 ceros = [k, 0] polos = [1 / (w0**2), 2 * etha / w0, 1] sys = signal.TransferFunction(ceros, polos) w, dB, phase = signal.bode(sys, w=None, n=500) f = w / np.pi fig, ((ax1), (ax2)) = plt.subplots(2) ax1.semilogx(f, dB) ax1.set_xlabel('Hz') ax1.set_ylabel('dB') ax1.set_title('Base 10') ax1.grid(True) ax2.semilogx(w, dB) ax2.set_xlabel('rad/s') ax2.set_ylabel('dB') ax2.set_title('Base 10') ax2.grid(True) fig.tight_layout() plt.show() return 0
def bode(num, den): import matplotlib.pyplot as plt from scipy import signal w, mag, phase = signal.bode((num, den)) constant_line_gain = [0] * w plt.figure("Bode") plt.ion() f, axarr = plt.subplots(2, sharex=True) plt.subplot(211) plt.semilogx(w, mag, 'dodgerblue') plt.semilogx(w, constant_line_gain, color='grey', linestyle='-.') plt.title('Bode Diagram') plt.ylabel('Magnitude (dB)') plt.subplot(212) plt.semilogx(w, phase, 'dodgerblue') plt.ylabel('Phase (Deg)') plt.xlabel('Frequency (rad/s)') return plt.show()
def bodeSym2(sym): # esta funcion recibe una ecuacion simbolica y te saca el bode s = Symbol('s') sym = simplify(sym) num, den = fraction(sym) num = expand(num) den = expand(den) if (num == num.subs(s, 1234567890)): num_coeffs = float(num) else: num_pol = Poly(num) num_coeffs = num_pol.all_coeffs() if (den == den.subs(s, 1234567890)): den_coeffs = float(den) else: den_pol = Poly(den) den_coeffs = den_pol.all_coeffs() n = np.array(num_coeffs).astype(np.float64) d = np.array(den_coeffs).astype(np.float64) H = signal.TransferFunction(n, d) w, mag, pha = signal.bode(H) return w, mag
num = [1] # Coefficients in denominator of transfer function # High order to low order, eg 1*s^2 + 0.1*s + 1 den = [1, 0.1, 1] # Scan over zeta, a parameter for a second-order system zetaRange = np.arange(0.1,1.1,0.1) f1 = plt.figure() print "no prob" # for i in range(0,2): den = [1, 2*zetaRange[1], 1] print den s1 = signal.lti(num, den) # Specify our own frequency range: np.arange(0.1, 5, 0.01) w, mag, phase = signal.bode(s1, np.arange(0.1, 5, 0.01).tolist()) plt.semilogx (w, mag, color="blue", linewidth="1") plt.xlabel ("Frequency") plt.ylabel ("Magnitude") # plt.savefig ("c:\\mag.png", dpi=300, format="png") plt.figure() for i in range(0,9): den = [1, zetaRange[i], 1] s1 = signal.lti(num, den) w, mag, phase = signal.bode(s1, np.arange(0.1, 10, 0.02).tolist()) plt.semilogx (w, phase, color="red", linewidth="1.1") plt.xlabel ("Frequency") plt.ylabel ("Amplitude")
premier ordre (passe-bas) et un du second ordre (passe-bande). ''' from scipy import signal # Pour les fonctions 'lti' et 'bode' import numpy as np # Pour np.logspace # Ceci est un filtre passe-bas donc potentiellement intégrateur à HF num = [1] # Polynôme au numérateur (-> 1 ) # Polynôme au dénominateur (-> 0.01*jw + 0.999) den = [0.01, 0.999] # L'intervalle de fréquences considéré (échelle log) f = np.logspace(-1, 4, num=200) s1 = signal.lti(num, den) # La fonction de transfert f, GdB, phase = signal.bode(s1, f) # Fabrication automatique des données from bode import diag_bode # Pour générer le diagramme de Bode # Appel effectif à la fonction dédiée. diag_bode(f, GdB, phase, 'PNG/S11_integrateur.png') # Ceci est un filtre passe-bande du second ordre (intégrateur à HF) num2 = [0.01, 0] # Numérateur (-> 0.01*jw ) # Dénominateur (-> 0.01*(jw)**2 + 0.01*jw + 1) den2 = [10**-2, 0.01, 1] f = np.logspace(-1, 4, num=200) # Intervalle de fréquences en échelle log s2 = signal.lti(num2, den2) # Fonction de transfert f, GdB, phase = signal.bode(s2, f) # Fabrication des données
cc = RpCp / b dd = 1 #PM|max inverse tan [1/2((b-1)/sqrt(b))] #PM=math.degrees(math.atan(1.18)) #49.720136931043555 PM = math.degrees(math.atan(1 / 2 * ((b - 1) / math.sqrt(b)))) print(PM) #s1 = signal.lti([8763432.556,1.21051E+13], [4.38755E-08,1,0]) #s1 = signal.lti([0.026655419,36819.66581], [1.40184E-15, 3.19503E-08, 0]) #s1 = signal.lti([8763432.556,1.21051E+13], [4.38755E-08, 1, 0,0]) #s1 = signal.lti([a,b], [4.38755E-08, 1, 0]) #s1 = signal.lti([100,100], [1,110,1000]) s1 = signal.lti([aa, bb], [cc, dd, 0, 0]) w, mag, phase = signal.bode(s1) plt.figure(1) plt.subplot(211) plt.semilogx(w, mag) plt.grid(True) plt.subplot(212) plt.semilogx(w, phase) # bode phase plot plt.grid(True) #s2 = signal.lti([8763432.556,1.21051E+13], [4.38755E-08,1,0,0]) #w2, mag2, phase2 = signal.bode(s2) # #plt.subplot(413)
import numpy as np import math from scipy import signal import control import matplotlib.pyplot as plt #if using termux import subprocess import shlex #end if H = 0.01 #setting the value of H for required phase margin num = [4e10*((np.pi)**2)*H] den = [1,6.28*(10+1e4),4e5*((np.pi)**2)] G = signal.lti(num,den) w, mag, phase = signal.bode(G,w=np.linspace(1,1e5,100000)) sys = control.tf(num, den) gm, pm, wpc, wgc = control.margin(sys) print('Phase Margin in degrees:', pm) print('Gain cross-over frequency in rad/sec:', wgc) #Magnitude plot plt.subplot(2, 1, 1) plt.semilogx(w, mag,'g') plt.plot(wgc, 0, 'o') plt.text(49404,0, '({}, {})'.format(49404,0)) plt.ylabel("20$log_{10}(|G(j\omega)|$") plt.title("Magnitude Plot") plt.grid()
def time_bode(self): signal.bode(self.system)
#freq = np.arange (400e6, 500e6, 1e6) #de 400 a 500MHz cada 1MHz #Zp = #Zt = 1. / (1./Zm + 1./Zp) #print Zt.simplify() H = 1 / s * (s**2 * Lm * Cm + s * Rm * Cm + 1) / (s**2 * Lm * Cm * Cp + s * Rm * Cm * Cp + Cm + Cp) print(H.simplify()) #convierto a lti con Lm = 86uHy y Cp 2.3pF #num = [1.29e-16, 3.9e-11, 1] #den = [2.967e-28, 8.97e-23, 3.8e-12, 0] #convierto a lti con Lm = 86uHy y Cp 1.9pF num = [1.29e-16, 3.9e-11, 1] den = [2.451e-28, 7.41e-23, 3.4e-12, 0] #convierto a lti con Lm = 86nHy #num = [1.29e-19, 3.9e-11, 1] #den = [2.967e-31, 8.97e-23, 3.8e-12, 0] saw = lti(num, den) w, mag, phase = bode(saw, freq) plt.figure(1) plt.semilogx(w, mag, color="blue", linewidth="1") plt.show()
plt.ylabel('|H(jw)|(db)') plt.title('Hand Calculated Magnitude of H(jw)') plt.subplot(1, 2, 2) plt.semilogx(w, phase_H) plt.grid(True) plt.xlabel('w (rad/s)') plt.ylabel('phase angle (rad)') plt.title('Hand Calculated Phase of H(jw)') plt.show() #%% ################## Bode using sig.bode ################## num = [(1 / (R * C)), 0] den = [1, (1 / (R * C)), (1 / (L * C))] (freq, mag, phase) = sig.bode((num, den)) myFigSize = (12, 8) plt.figure(figsize=myFigSize) plt.subplot(1, 2, 1) plt.semilogx(freq, mag) plt.grid(True) plt.xlabel('w(rad/s)') plt.ylabel('|H(jw)|(db)') plt.title('Magnitude of H(jw) with sig.bode') plt.subplot(1, 2, 2) plt.semilogx(freq, phase) plt.grid(True) plt.xlabel('w (rad/s)') plt.ylabel('phase angle (rad)')
w_0 = np.sqrt(e_Max / X_Max) w_C = np.sqrt(M / (M - 1)) * w_0 w_2 = (M - 1) / M * w_C w_3 = (M + 1) / M * w_C k = 10**(np.log10(g_max / X_Max)) # In[10]: ###Original W_s = signal.lti([900], [0.08 * 0.02, 0.1, 1, 0]) #step = signal.step(W_s) #impulse = signal.impulse(W_s) w, mag, phase = signal.bode(W_s) plt.subplot(1, 2, 1) plt.semilogx(w, mag, linewidth=4.0) plt.xlabel('$\omega$') plt.ylabel('Magnitude, dB') plt.title('Bode Plot Mag Vs Freq') plt.subplot(1, 2, 2) plt.semilogx(w, phase, linewidth=4.0) plt.xlabel('$\omega$') plt.ylabel('Phase, deg') plt.title('Bode Plot Phase Vs Freq')
plt.plot(time, y) plt.title("y(t)") plt.xlabel("time") plt.ylabel("y") plt.show() #task 5 """ R = 100 L = 0.000001s C = 1000000/s H = C/(R+L+C) """ H = sig.lti([1000000], [0.000001, 100, 1000000]) #finding H(s) as laplace of num/den w, mag, phase = sig.bode(H) #finding bode parameters of H fig, a = plt.subplots(2, 1) a[0].set_title("Bode plot of H(s)") a[0].semilogx(w, mag) a[0].set_ylabel(r'$|H(s)|$') a[1].semilogx(w, phase) a[1].set_ylabel(r'$\angle(H(s))$') plt.show() #task 6 time = np.linspace(0, 0.01, 100000) x = np.cos(1000 * time) - np.cos(1000000 * time) time, y, svec = sig.lsim(H, x, time) #finding y(t) as inverse plt.plot(time, y) plt.title(r'$v_{o}(t)$') plt.xlabel("time")
# # recode l1..utf8 monfichier.py # # Il faudra alors modifier la première ligne en # coding: utf8 # pour que Python s'y retrouve. ''' Un filtre "bizarre" dont il faut trouver la fonction de transfert sachant qu'il est du premier ordre. ''' from scipy import signal # Pour lti et bode import numpy as np # Pour l'échantillonnage num = [0.001,10.1] # Numérateur (-> 0.001*jw + 10.1) den = [0.0101,1] # Dénominateur (-> 0.0101*jw + 1 ) # Extraction des données f = np.logspace(0,6,num=200) s1 = signal.lti(num,den) f, GdB, phase = signal.bode(s1,f) # Et préparation du diagramme from bode import diag_bode diag_bode(f,GdB,phase,'PNG/S11_filtre_bizarre.png')
# # Imports # import numpy as np from scipy import signal import matplotlib.pyplot as plt # # Transfer function as LTI # sig = signal.lti([8000, 0],[1, 5000, 10^6]) # # Frequency range & Bode magnitude # bode() returns magnitude in dB # w = np.logspace(1,4,100) w, mag, phase = signal.bode(sig,w) # # Plot setup # plt.figure() plt.semilogx(w, mag) plt.title('Bode Plot of G(s) = 8000s / (s^2 + 5000s + 10^6)') plt.xlabel('Frequency [rad/sec]') plt.ylabel('Amplitude [dB]') plt.grid(axis='both',which='both') plt.show()
from scipy import signal import matplotlib.pyplot as plt s1 = signal.lti([1], [1, 1]) w, mag, phase = signal.bode(s1) plt.figure() plt.semilogx(w, mag) # bode magnitude plot plt.figure() plt.semilogx(w, phase) # bode phase plot plt.show()
indx = np.searchsorted(self.x, [x])[0] x = self.x[indx] y = self.y[indx] # update the line positions self.lx.set_ydata(y) self.ly.set_xdata(x) self.txt.set_text('x=%1.2f, y=%1.2f' % (x, y)) # print('x=%1.2f, y=%1.2f' % (x, y)) plot.draw() # define start/end freq and freq step for bode plots startFreq = omega_3/10 endFreq = omega_4*10 freqStep = np.ceil((endFreq-startFreq)/1000) # bode plot for magnitude and phase tf = signal.lti(tf_num, tf_den) w, mag, phase = signal.bode(tf, np.arange(startFreq, endFreq, freqStep).tolist()) fig, ax = plot.subplots() plot.semilogx(w/(2*np.pi), mag, color="blue", linewidth="2") #plot.autoscale(enable=False, axis='both', tight=False) plot.xlabel("Frequency (Hz)") plot.ylabel("Magnitude (dB)") plot.axis([omega_3/(2*np.pi)/10, omega_4/(2*np.pi)*10, -100, 10]) cursor = SnaptoCursor(ax, w/(2*np.pi), mag) plot.connect('motion_notify_event', cursor.mouse_move) plot.show()
from scipy import signal import matplotlib.pyplot as plt #setting the numerator and denominator of the the transfer fucntion numerator = [0.1] denominator = [1, 0.1] #generate the bode response with w w, h, angle = signal.bode((numerator, denominator)) #plot the bode response plt.subplot(2, 1, 1) plt.semilogx(w, h, color="Orange") plt.title("Bode Plot") plt.legend(("Magnitude", ), loc="upper right") plt.ylabel("dB") #plot the phase response plt.subplot(2, 1, 2) plt.semilogx(w, angle, color="blue") plt.xlabel("w(rad/s)") plt.legend(("Phase", ), loc="upper right") plt.ylabel("dB")
def analyze_lti(lti, t=np.linspace(0,10,100)): """Create a set of plots to analyze an lti system Args: lti (instance of an Interactive LTI system subclass ): lti system to interact with t (numpy array): Timepoints at which to evaluate the impulse and step responses """ t_impulse, y_impulse = impulse(lti.sys) fig_impulse = figure( title="impulse response", x_range=(0, 10), y_range=(-0.1, 3), x_axis_label='time') g_impulse = fig_impulse.line(x=t_impulse, y=y_impulse) t_step, y_step = step(lti.sys) fig_step = figure( title="step response", x_range=(0, 10), y_range=(-0.04, 1.04) ) g_step = fig_step.line(x=t_step, y=y_step) fig_pz = figure(title="poles and zeroes", x_range=(-4, 1), y_range=(-2,2)) g_poles = fig_pz.circle( x=lti.sys.poles.real, y=lti.sys.poles.imag, size=10, fill_alpha=0 ) g_rootlocus = fig_pz.line(x=[0, -10], y=[0, 0], line_color='red') w, mag, phase, = bode(lti.sys) fig_bode_mag = figure(x_axis_type="log") g_bode_mag = fig_bode_mag.line(w, mag) fig_bode_phase = figure(x_axis_type="log") g_bode_phase = fig_bode_phase.line(w, phase) fig_bode = gridplot( [fig_bode_mag], [fig_bode_phase], nrows=2, plot_width=300, plot_height=150, sizing_mode="fixed") grid = gridplot( [fig_impulse, fig_step], [fig_pz, fig_bode], plot_width=300, plot_height=300, sizing_mode="fixed" ) show(grid, notebook_handle=True) def update(**kwargs): lti.update(**kwargs) g_impulse.data_source.data['x'], g_impulse.data_source.data['y'] = ( impulse(lti.sys, T=t)) g_step.data_source.data['x'], g_step.data_source.data['y'] = ( step(lti.sys, T=t)) g_poles.data_source.data['x'] = lti.sys.poles.real g_poles.data_source.data['y'] = lti.sys.poles.imag w, mag, phase, = bode(lti.sys) g_bode_mag.data_source.data['x'] = w g_bode_mag.data_source.data['y'] = mag g_bode_phase.data_source.data['x'] = w g_bode_phase.data_source.data['y'] = phase push_notebook() interact(update, **lti.update_kwargs)
amp = df["amp"] sim = pd.read_csv("spice_data/bandreject.csv") freqsim=sim["freq"] ampsim=sim["amp"] sys = signal.lti( [C*Cg*Rg*Zg, BWP*C*Cg*Rg*Zg + C*Rg + Cg*Rg + Cg*Zg, BWP*C*Rg + BWP*Cg*Rg + 1, BWP] , [C*Cg*R*Rg + C*Cg*R*Zg + C*Cg*Rg*Zg, BWP*C*Cg*R*Rg + BWP*C*Cg*Rg*Zg + C*R + C*Rg + Cg*Rg + Cg*Zg, BWP*C*R + BWP*C*Rg + BWP*Cg*Rg + 1, BWP] ) f=logspace(2,7.30102999566,100) w= 2 * pi * f w, mag, phase = signal.bode(sys, w) color1="blue"; color2="green"; color3="red"; plt.semilogx(f,mag,color1) plt.semilogx(freq,amp,color2) plt.semilogx(freqsim,ampsim,color3) plt.grid(True, which="both") blue_patch = mpatches.Patch(color=color1, label='Teórico') green_patch = mpatches.Patch(color=color2, label='Práctica') red_patch = mpatches.Patch(color=color3, label='Simulación') plt.legend(handles=[green_patch, blue_patch, red_patch]) plt.show()
def test_06(self): # Test that bode() doesn't fail on a system with a pole at 0. # integrator, pole at zero: H(s) = 1 / s system = lti([1], [1, 0]) w, mag, phase = bode(system, n=2) assert_equal(w[0], 0.01) # a fail would give not-a-number
plt.subplot(2, 1, 2) plt.semilogx(w, Deg) plt.yticks([-90, -45, 0, 45, 90]) plt.xlabel('w (rad/sec)') plt.ylim([-90, 90]) plt.ylabel('/_ H (degrees)') plt.grid() plt.show() #%% Part 1 Task 2 num = [(1 / (R * C)), 0] den = [1, (1 / (R * C)), (1 / (L * C))] omega, mag, phase = sig.bode((num, den), w) fig = plt.figure(figsize=(10, 7)) plt.subplot(2, 1, 1) plt.semilogx(omega, mag) plt.ylabel('|H| dB') plt.grid() plt.title('Bode Plot Using sig.bode()') plt.subplot(2, 1, 2) plt.semilogx(omega, phase) plt.yticks([-90, -45, 0, 45, 90]) plt.xlabel('w (rad/sec)') plt.ylim([-90, 90]) plt.ylabel('/_ H (degrees)') plt.grid()
def test_07(self): # bode() should not fail on a system with pure imaginary poles. # The test passes if bode doesn't raise an exception. system = lti([1], [1, 0, 100]) w, mag, phase = bode(system, n=2)
#end if j = 22 s = np.zeros(j, dtype=object) w = np.zeros(j, dtype=object) mag = np.zeros(j, dtype=object) phase = np.zeros(j, dtype=object) m = 0 #Defining the transfer function for k in range(6, j, 2): m = 1000.00 / (2.1000001 - k * 0.1) s[k] = signal.lti([k * 0.1, 2.1 * k * 0.1 * 5, k * 0.1 * 25], [1, (2.1 - (k * 0.1)) * 5, 25]) #G(s) #signal.bode takes transfer function as input and returns frequency,magnitude and phase arrays w[k], mag[k], phase[k] = signal.bode(s[k], n=100000) plt.semilogx(20 * log(w[k]), mag[k], label=m / 1000.00) plt.grid() plt.xlabel('Frequency(rad/s)') plt.ylabel('Magnitude(db)') plt.title('Normalised gain for different Q-Factors for RC = 1/5') plt.legend() #if using termux plt.savefig('./figs/ee18btech11030/ee18btech11030_fc.pdf') plt.savefig('./figs/ee18btech11030/ee18btech11030_fc.eps') subprocess.run( shlex.split("termux-open ./figs/ee18btech11030/ee18btech11030_fc.pdf")) #else
def fdt_teorica(self, numeratore=1, denominatore=1): fdt = signal.lti(numeratore, denominatore) _w_plot, self._ampiezza_teo, _fase_teo = signal.bode(fdt, n=1000) self._f_teo = _w_plot / (2 * np.pi) self._fase_teo = _fase_teo # rad -> deg
Pour un exercice de reconnaissance d'un filtre qui puisse jouer le rôle d'intégrateur dans un certain intervalle de fréquences. On en génère un du premier ordre (passe-bas) et un du second ordre (passe-bande). ''' from scipy import signal # Pour les fonctions 'lti' et 'bode' import numpy as np # Pour np.logspace # Ceci est un filtre passe-bas donc potentiellement intégrateur à HF num = [1] # Polynôme au numérateur (-> 1 ) den = [0.01,0.999] # Polynôme au dénominateur (-> 0.01*jw + 0.999) f = np.logspace(-1,4,num=200) # L'intervalle de fréquences considéré (échelle log) s1 = signal.lti(num,den) # La fonction de transfert f,GdB,phase = signal.bode(s1,f) # Fabrication automatique des données from bode import diag_bode # Pour générer le diagramme de Bode # Appel effectif à la fonction dédiée. diag_bode(f,GdB,phase,'PNG/S11_integrateur.png') # Ceci est un filtre passe-bande du second ordre (intégrateur à HF) num2 = [0.01,0] # Numérateur (-> 0.01*jw ) den2 = [10**-2,0.01,1] # Dénominateur (-> 0.01*(jw)**2 + 0.01*jw + 1) f = np.logspace(-1,4,num=200) # Intervalle de fréquences en échelle log s2 = signal.lti(num2,den2) # Fonction de transfert f,GdB,phase = signal.bode(s2,f) # Fabrication des données diag_bode(f,GdB,phase,'PNG/S11_integrateur2.png') # et du diagramme
# Coefficients in numerator of transfer function num = [1] # Coefficients in denominator of transfer function # High order to low order, eg 1*s^2 + 0.1*s + 1 den = [1, 0.1, 1] # Scan over zeta, a parameter for a second-order system zetaRange = np.arange(0.1, 1.1, 0.1) f1 = plt.figure() for i in range(0, 9): den = [1, 2 * zetaRange[i], 1] print(den) s1 = signal.lti(num, den) # Specify our own frequency range: np.arange(0.1, 5, 0.01) w, mag, phase = signal.bode(s1, np.arange(0.1, 5, 0.01).tolist()) plt.semilogx(w, mag, color="blue", linewidth="1") plt.xlabel("Frequency") plt.ylabel("Magnitude") plt.savefig("c:\\mag.png", dpi=300, format="png") plt.figure() for i in range(0, 9): den = [1, zetaRange[i], 1] s1 = signal.lti(num, den) w, mag, phase = signal.bode(s1, np.arange(0.1, 10, 0.02).tolist()) plt.semilogx(w, phase, color="red", linewidth="1.1") plt.xlabel("Frequency") plt.ylabel("Amplitude") plt.savefig("c:\\phase.png", dpi=300, format="png")
from scipy import signal import matplotlib.pyplot as plt from pylab import * #if using termux import subprocess import shlex #end if s1 = signal.lti( [16200, 21 * 16200, 110 * 16200], [11, 18 * 11, 99 * 11, 162 * 11, 0]) #Defining the transfer function w, mag, phase = signal.bode( s1 ) #signal.bode takes transfer function as input and returns frequency,magnitude and phase arrays subplot(2, 1, 1) plt.grid() plt.xlabel('Frequency(rad/s)') plt.ylabel('Magnitude(db)') plt.semilogx(w, mag, 'b') # Bode Magnitude plot plt.axhline(y=0, xmin=0, color='r', linestyle='dashed') plt.axvline(x=38.95, ymin=0, color='k', linestyle='dashed') plt.plot(38.95, 0, 'o') plt.text(38.95, 0, '({}, {})'.format(38.95, 0)) subplot(2, 1, 2) plt.xlabel('Frequency(rad/s)')
plt.subplot (2,1,1) plt.semilogx(omega, h_mag) plt.grid() plt.ylabel('Magnitude of H(s)') plt.title('Task 1 H(s) Bode Plot') plt.subplot (2,1,2) plt.semilogx(omega, h_phi) plt.grid() plt.ylabel('Phase of H(s) (rad)') plt.xlabel('rad/s') plt.show() w, bodeMag, bodePhase = sig.bode((Hsnum,Hsden), omega) plt.figure(figsize = (15,10)) plt.subplot (2,1,1) plt.semilogx(w, bodeMag) plt.ylabel('Magnitude of H(s)') plt.title('Task 2 H(s) Bode Plot') plt.subplot (2,1,2) plt.semilogx(w, bodePhase) plt.grid() plt.ylabel('Phase of H(S) (deg)') plt.xlabel('rad/s') plt.show() sys = con.TransferFunction(Hsnum, Hsden)
def fdt_teorica(self, numeratore=1, denominatore=1): fdt = signal.lti(numeratore, denominatore) self._w_plot, self._ampiezza_teo, _fase_teo = signal.bode( fdt, w=np.linspace(3e2, 10e5, 10000)) self._f_teo = self._w_plot / (2 * np.pi) self._fase_teo = _fase_teo # rad -> deg
# Original Signal s = signal.lti([25], [1, 6, 5, 0]) # Single Pass Compensated transfer function s1 = signal.lti([25 * 1, 25], [1 * 0.0074 * 2, 1 + 6 * 0.0074 * 2, 6 + 5 * 0.0074 * 2, 5, 0]) # Double Pass Compensated transfer function s2 = signal.lti(25 * np.polymul([alpha * t, 1], [alpha * t, 1]), np.polymul([t, 1], np.polymul([1, 6, 5, 0], [t, 1]))) # Compensator transfer function #s = signal.lti([0.5, 1], [0.0074, 1]) w, mag, phase = signal.bode(s) w1, mag1, phase1 = signal.bode(s1) w2, mag2, phase2 = signal.bode(s2) plt.subplot(2, 1, 1) plt.xlabel('Frequency(rad/s)') plt.ylabel('Magnitude(deg)') plt.semilogx(w, mag, 'b-') plt.semilogx(w1, mag1, 'r-') plt.semilogx(w2, mag2, 'k-') #plt.axvline(x = 2.038,ymin=0,color='k',linestyle='dashed') plt.legend(['Uncompensated', 'Single Pass', 'Double Pass']) #plt.plot(2.038,0,'o') plt.grid() subplot(2, 1, 2)
if((n%2) == 1): den = poly_mult(den,H_odd) num = np.zeros(np.size(den)) num[np.size(den)-1] = 1 #Scaling num = 10**6*num den[1] = 1e2*den[1] den[2] = 1e4*den[2] den[3] = 10**6*den[3] print(num,'\n',den) system = sig.lti(num,den) w, Hmag, Hphase = sig.bode(system) ############# Plotting Bode ############################# Ampl = 10**(0.05*Hmag) #plt.subplot(211) plt.semilogx(w,Ampl,'k') # Plot amplitude, not dB. plt.title('Butter_filter') #plt.axis([10e3, 10e6, 0, 1]) plt.yticks([0,.1,.8, .707, 1]) plt.grid(which='both') plt.xlabel('$\omega$ (rad/s)') plt.ylabel('|H|') plt.xticks([90,100,220]) plt.show()
fig = plt.figure(figsize=(11,6)) plt.subplot(2,3,1) num1_sk_high_3 = 1 num2_sk_high_3 = 0 num3_sk_high_3 = 0 num4_sk_high_3 = 0 den1_sk_high_3 = 1 den2_sk_high_3 = (1/(C1*R3)+1/(C1*R2)+1/(C2*R2)+1/(C3*R2)) den3_sk_high_3 = (1/(C1*C2*R3*R2)+1/(C1*C3*R3*R2)+1/(C1*C3*R2*R1)+1/(C2*C3*R1*R2)) den4_sk_high_3 = 1/(C1*C2*C3*R1*R2*R3) system = signal.lti([num1_sk_high_3, num2_sk_high_3, num3_sk_high_3 , num4_sk_high_3], [den1_sk_high_3, den2_sk_high_3 , den3_sk_high_3, den4_sk_high_3]) f = logspace(1, 5) w = 2 * pi * f w, mag, phase = signal.bode(system,w) plt.semilogx(f, mag, label="Menor 10%"); ################################################## R1=R1*1.056 num1_sk_high_3 = 1 num2_sk_high_3 = 0 num3_sk_high_3 = 0 num4_sk_high_3 = 0 den1_sk_high_3 = 1 den2_sk_high_3 = (1/(C1*R3)+1/(C1*R2)+1/(C2*R2)+1/(C3*R2)) den3_sk_high_3 = (1/(C1*C2*R3*R2)+1/(C1*C3*R3*R2)+1/(C1*C3*R2*R1)+1/(C2*C3*R1*R2)) den4_sk_high_3 = 1/(C1*C2*C3*R1*R2*R3) system = signal.lti([num1_sk_high_3, num2_sk_high_3, num3_sk_high_3 , num4_sk_high_3], [den1_sk_high_3, den2_sk_high_3 , den3_sk_high_3, den4_sk_high_3]) f = logspace(1, 5)
"""Tests for `gwpy.plot.filter` """ import numpy from numpy import testing as nptest from scipy import signal from ...frequencyseries import FrequencySeries from .. import BodePlot from .utils import FigureTestBase # design ZPK for BodePlot test ZPK = [100], [1], 1e-2 FREQUENCIES, MAGNITUDE, PHASE = signal.bode(ZPK, n=100) class TestBodePlot(FigureTestBase): FIGURE_CLASS = BodePlot def test_init(self, fig): assert len(fig.axes) == 2 maxes, paxes = fig.axes # test magnigtude axes assert maxes.get_xscale() == 'log' assert maxes.get_xlabel() == '' assert maxes.get_yscale() == 'linear' assert maxes.get_ylabel() == 'Magnitude [dB]' # test phase axes assert paxes.get_xscale() == 'log'