def stepSig_demo(): from scipy.signal import lti, step2, impulse2 import matplotlib.pyplot as plt s1 = lti([3], [1, 2, 10]) # 以分子分母的最高次幂降序的系数构建传递函数,s1=3/(s^2+2s+10) s2 = lti([1], [1, 0.4, 1]) # s2=1/(s^2+0.4s+1) s3 = lti([5], [1, 2, 5]) # s3=5/(s^2+2s+5) t1, y1 = step2(s1) # 计算阶跃输出,y1是Step response of system. t2, y2 = step2(s2) t3, y3 = step2(s3) t11, y11 = impulse2(s1) t22, y22 = impulse2(s2) t33, y33 = impulse2(s3) f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharex='col', sharey='row') # 开启subplots模式 ax1.plot(t1, y1, 'r', label='s1 Step Response', linewidth=0.5) ax1.set_title('s1 Step Response', fontsize=9) ax2.plot(t2, y2, 'g', label='s2 Step Response', linewidth=0.5) ax2.set_title('s2 Step Response', fontsize=9) ax3.plot(t3, y3, 'b', label='s3 Step Response', linewidth=0.5) ax3.set_title('s3 Step Response', fontsize=9) ax4.plot(t11, y11, 'm', label='s1 Impulse Response', linewidth=0.5) ax4.set_title('s1 Impulse Response', fontsize=9) ax5.plot(t22, y22, 'y', label='s2 Impulse Response', linewidth=0.5) ax5.set_title('s2 Impulse Response', fontsize=9) ax6.plot(t33, y33, 'k', label='s3 Impulse Response', linewidth=0.5) ax6.set_title('s3 Impulse Response', fontsize=9) ##plt.xlabel('Times') ##plt.ylabel('Amplitude') # plt.legend() plt.show()
def plot_time(analog_system, discrete_system, response='step', num_samples=100, sample_rate=48e3, plot_error=False): n = np.arange(num_samples) t = n/sample_rate fig, ax1 = plt.subplots() if response == 'impulse': ta, ya = signal.impulse2(analog_system, T=t) _, yd = signal.dimpulse((*discrete_system,1/sample_rate), t=t) ax1.set_title('Resposta ao impulso') elif response == 'step': ta, ya = signal.step2(analog_system, T=t) _, yd = signal.dstep((*discrete_system,1/sample_rate), t=t) ax1.set_title('Resposta ao degrau') elif response == 'ramp': ramp_factor = 1 ta = np.arange(int(num_samples*ramp_factor))/(ramp_factor*sample_rate) print(ta.shape) ta, ya, xa = signal.lsim2(analog_system, U=ta, T=ta, printmessg=True) _, yd = signal.dlsim((*discrete_system,1/sample_rate), u=t, t=t, x0=None) ax1.set_title('Resposta a rampa') ax1.plot(ta*sample_rate,ya,'b') ax1.plot(n,np.ravel(yd),'k.') ax1.set_ylabel('Amplitude', color='b') ax1.set_xlabel('Amostras') ax1.grid(True) ax1.legend(['Analógico', 'Discreto']) if plot_error: ax2 = ax1.twinx() ax2.plot(n, np.abs(ya-np.ravel(yd)), 'r') ax2.set_ylabel('Erro', color='r') ax2.axis('tight')
from scipy import signal system = ([1.0], [1.0, 2.0, 1.0]) t, y = signal.impulse2(system) print t print y
def test_impulse_invariant(self, sys, sample_time, samples_number): time = np.arange(samples_number) * sample_time _, yout_cont = impulse2(sys, T=time, **self.tolerances) _, yout_disc = dimpulse(c2d(sys, sample_time, method='impulse'), n=len(time)) assert_allclose(sample_time * yout_cont.ravel(), yout_disc[0].ravel())
C = 1e-6 # define the capacitor value # calculate the other parts of the circuit L = 1 / ((2 * np.pi * f0)**2 * C) R = 2 * alpha * L print("R=%.2f Ohm" % R) print("L=%.2e Henry" % L) print("C=%.2e Farad" % C) system = signal.TransferFunction([R * C, 0], [L * C, R * C, 1]) # the sampling frequency to simulate the time-continuous system in discrete time Fs = 1e6 T = np.arange(0, 1e-3, 1 / Fs) t, h = signal.impulse2(system, T=T) plt.figure(figsize=(8, 4)) plt.plot(t * 1000, h, 'navy') plt.grid(True) plt.xlabel('$t$ [ms]') plt.ylabel('$h(t)$') plt.title('Impulse response of RLC circuit') # plt.savefig('1_Impulse response of RLC circuit.png', dpi=600) plt.show() # calculte frequency response w, H = signal.freqresp(system, 2 * np.pi * np.logspace(0, 6, 10000)) # find indices for both test frequencies and evaluate the amplitude indf0 = np.argmin(abs(w - 2 * np.pi * f0)) indf1 = np.argmin(abs(w - 2 * np.pi * f1))
# The initial conditions provided are $x(0)=1$ and $\dot{x}(0)= \dot{y}(0)=y(0)=0$. Working out the math using unilateral laplace transforms, # \begin{align} # X(s)&= \frac{s^2+2}{s^3+3s}\\ # Y(s)&= \frac{2}{s^3+3s}\\ # \end{align} # These are passed to the $sp.impulse$ functions to study the behaviour of the system. # # In[198]: num= [1,0,2] den=[1,0,3,0] X= sp.lti(num,den) T= np.linspace(0,50,301) t,y=sp.impulse2(X,None,T) plotfigure(size,"t","$x$","$x(t)$",t,y,"b-") # In[199]: num= [2] den=[1,0,3,0] X= sp.lti(num,den) T= np.linspace(0,50,301) t,y=sp.impulse2(X,None,T) plotfigure(size,"t","$y$","$y(t)$",t,y,"b-") # ## Problem 4
plt.plot(tp2, mp2, 'mo') plt.plot(tp3, mp3, 'mo') plt.plot([ts1, ts1], [0.0, val_ts1], 'ko') plt.plot([ts2, ts2], [0.0, val_ts2], 'ko') plt.plot([ts3, ts3], [0.0, val_ts3], 'ko') plt.legend(['Escalón Unitario', '$Butter$', '$Cheby$', '$Bessel$']) plt.xlabel('time [seg]', fontsize=12) plt.ylabel('Step Response $c(t)$ [V]', fontsize=12) plt.grid() plt.xlim(t0, tf * 9 / 10) # ---------------------------------------------------------------------------- # Respuesta al Impulso: h(t): t, resp1_h = sig.impulse2(my_tf_bw, None, t) t, resp2_h = sig.impulse2(my_tf_ch, None, t) t, resp3_h = sig.impulse2(my_tf_be, None, t) fig12 = plt.figure() plt.suptitle('Comparación: Respuesta al Impulso h(t)') plt.plot(t, resp1_h, 'b') plt.plot(t, resp2_h, '#ff7f0e') plt.plot(t, resp3_h, 'g') plt.legend(['$Butter$', '$Cheby$', '$Bessel$']) plt.xlabel('time [seg]', fontsize=12) plt.ylabel('Impulse Response $h(t)$', fontsize=12) plt.grid() plt.xlim(-1, 45)
[-2,-3] ]) B = np.array([ [0], [1] ]) C = np.array([ [1,0] ]) D = 0 sys = signal.StateSpace(A,B,C,D) #State space to time domain conversion t1,y1= signal.impulse2(sys) # time and output axis for natural(impulse) response a,b = signal.ss2tf(A,B,C,D) #Importing Num. and Den. of T.F. from State Space Rep. #rounding off num = np.around(a[0],decimals = 0) den = np.around(b,decimals = 0) s = sp.symbols('s') H_s = sp.Poly(num,s)/sp.Poly(den,s) # Getting polynomial expressions print("THE TRANSFER FUNCTION OF THE SYSTEM ") print("H(s) =",H_s) plt.plot(t1,y1,label='impulse response') plt.legend() plt.grid() plt.show()
# Second order system with a repeated root: x''(t) + 2*x(t) + x(t) = u(t) from scipy import signal system = ([1.0], [1.0, 2.0, 1.0]) t, y = signal.impulse2(system) import matplotlib.pyplot as plt plt.plot(t, y)
from scipy.signal import lti, impulse2 import matplotlib.pyplot as plt from pylab import mpl import numpy as np a = [0, 1, 0] b = [1, 5, 6] sys = lti(a, b) #以分子分母的最高次幂降序的系数构建传递函数 t, y = impulse2(sys) plt.plot(t, y, 'r', label="s1 Step", linewidth=0.5) mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体 mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 plt.xlabel('时间(t)') plt.ylabel('h(t)') plt.title('例2.6-1的单位冲击响应') plt.xlim(-0.2, 4) #设置x轴的区间 plt.ylim(-0.2, 1) #设置y轴的区间 plt.vlines(0, -0.2, 1) #x=0 plt.hlines(0, -0.2, 4) #y=0 plt.show()
def get_impulse(self): return signal.impulse2(self.denorm_sys)