def test_discrete_time_impulse(self, tsystem): # discrete time impulse sampled version should match cont time dt = 0.1 t = np.arange(0, 3, dt) sys = tsystem.sys sysdt = sys.sample(dt, 'impulse') np.testing.assert_array_almost_equal(impulse_response(sys, t)[1], impulse_response(sysdt, t)[1])
def testSimulation(self): T = range(100) U = np.sin(T) # For now, just check calling syntax # TODO: add checks on output of simulations tout, yout = step_response(self.siso_ss1d) tout, yout = step_response(self.siso_ss1d, T) tout, yout = impulse_response(self.siso_ss1d, T) tout, yout = impulse_response(self.siso_ss1d) tout, yout, xout = forced_response(self.siso_ss1d, T, U, 0) tout, yout, xout = forced_response(self.siso_ss2d, T, U, 0) tout, yout, xout = forced_response(self.siso_ss3d, T, U, 0)
def testSimulation(self): T = range(100) U = np.sin(T) # For now, just check calling syntax # TODO: add checks on output of simulations tout, yout = step_response(self.siso_ss1d) tout, yout = step_response(self.siso_ss1d, T) tout, yout = impulse_response(self.siso_ss1d, T) tout, yout = impulse_response(self.siso_ss1d) tout, yout, xout = forced_response(self.siso_ss1d, T, U, 0) tout, yout, xout = forced_response(self.siso_ss2d, T, U, 0) tout, yout, xout = forced_response(self.siso_ss3d, T, U, 0)
def test_impulse_response_mimo(self, tsystem): """"Test impulse response of MIMO systems.""" sys = tsystem.sys t = tsystem.t yref = tsystem.yimpulse _t, y_00 = impulse_response(sys, T=t, input=0, output=0) np.testing.assert_array_almost_equal(y_00, yref, decimal=4) _t, y_11 = impulse_response(sys, T=t, input=1, output=1) np.testing.assert_array_almost_equal(y_11, yref, decimal=4) yref_notrim = np.zeros((2, len(t))) yref_notrim[:1, :] = yref _t, yy = impulse_response(sys, T=t, input=0) np.testing.assert_array_almost_equal(yy[:,0,:], yref_notrim, decimal=4)
def test_impulse_response_siso(self, tsystem, kwargs): """Test impulse response of SISO systems.""" sys = tsystem.sys t = tsystem.t yref = tsystem.yimpulse out = impulse_response(sys, T=t, **kwargs) tout, yout = out[:2] assert len(out) == 3 if ('return_x', True) in kwargs.items() else 2 np.testing.assert_array_almost_equal(tout, t) np.testing.assert_array_almost_equal(yout, yref, decimal=4)
def impulse(sys, T=None, input=0, output=0, **keywords): ''' Impulse response of a linear system If the system has multiple inputs or outputs (MIMO), one input and one output must be selected for the simulation. The parameters `input` and `output` do this. All other inputs are set to 0, all other outputs are ignored. Parameters ---------- sys: StateSpace, TransferFunction LTI system to simulate T: array-like object, optional Time vector (argument is autocomputed if not given) input: int Index of the input that will be used in this simulation. output: int Index of the output that will be used in this simulation. **keywords: Additional keyword arguments control the solution algorithm for the differential equations. These arguments are passed on to the function :func:`lsim`, which in turn passes them on to :func:`scipy.integrate.odeint`. See the documentation for :func:`scipy.integrate.odeint` for information about these arguments. Returns ------- yout: array Response of the system T: array Time values of the output See Also -------- lsim, step, initial Examples -------- >>> T, yout = impulse(sys, T) ''' T, yout = timeresp.impulse_response(sys, T, 0, input, output, transpose = True, **keywords) return yout, T
def impulse(sys, T=None, input=0, output=0, **keywords): ''' Impulse response of a linear system If the system has multiple inputs or outputs (MIMO), one input and one output must be selected for the simulation. The parameters `input` and `output` do this. All other inputs are set to 0, all other outputs are ignored. Parameters ---------- sys: StateSpace, TransferFunction LTI system to simulate T: array-like object, optional Time vector (argument is autocomputed if not given) input: int Index of the input that will be used in this simulation. output: int Index of the output that will be used in this simulation. **keywords: Additional keyword arguments control the solution algorithm for the differential equations. These arguments are passed on to the function :func:`lsim`, which in turn passes them on to :func:`scipy.integrate.odeint`. See the documentation for :func:`scipy.integrate.odeint` for information about these arguments. Returns ------- yout: array Response of the system T: array Time values of the output See Also -------- lsim, step, initial Examples -------- >>> T, yout = impulse(sys, T) ''' T, yout = timeresp.impulse_response(sys, T, 0, input, output, transpose = True, **keywords) return yout, T
def test_impulse_response_warnD(self, tsystem): """Test warning about direct feedthrough""" with pytest.warns(UserWarning, match="System has direct feedthrough"): _ = impulse_response(tsystem.sys, tsystem.t)
const_ref = 5 w = 0.25 sin_ref = lambda x:0.1*np.sin(w*x) kp = 2;kd =2;ki = 5;ks = 5 G = ctrl.TransferFunction(1,[m,b,k]) #Transfer function is (kds^2+kps+ki)/s C1 = ctrl.TransferFunction([kd,kp,ki],[1,0]) C2 = ctrl.TransferFunction([kp,ki+ks,kp*w**2,ki*w**2+kp*w**2],[1,0,w**2]) C = C2 open_loop = G*C closed_loop = ctrl.feedback(G*C,1) t1,y1 = impulse_response(TransferFunction(const_ref,[1,0])*closed_loop,T=50) sintf = TransferFunction(0.1*w,[1,0,w**2]) t2,y2 = impulse_response(sintf*closed_loop,T=50) fig, axs = plt.subplots(2) fig.suptitle('Two Responses') axs[0].plot(t1, y1) # axs[0].plot(t1,const_ref*ones(len(t1))) axs[1].plot(t2, y2) axs[1].plot(t2, sin_ref(t2)) axs[1].legend(['x','r']) plt.show() print(ctrl.pole(open_loop)) print(ctrl.pole(closed_loop))