Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def firstorder_gen(params, period=0.01, noiseamp=1.0):
"""Simple first order transfer function affected variable
with sinusoid cause.
"""
samples = params[0]
delay = params[1]
P1 = control.matlab.tf([10], [100, 1])
timepoints = np.array(range(samples + delay))
sine_input = np.array([np.sin(period * t * 2 * np.pi) for t in timepoints])
P1_response = control.matlab.lsim(P1, sine_input, timepoints)
affected_random_add = (seed_rand(51, samples + delay) - 0.5) * noiseamp
cause = sine_input[:samples]
if delay == 0:
offset = None
else:
offset = samples
affected = P1_response[0][delay:] + affected_random_add[delay:]
tspan = P1_response[1][:offset]
return tspan, cause, affected
err_outputH2, Time, Xsim = cnt.lsim(H_sample2, err_inputH[1, :], Time)
err_outputH3, Time, Xsim = cnt.lsim(H_sample3, err_inputH[2, :], Time)
Yout = np.zeros((3, npts))
Yout11, Time, Xsim = cnt.lsim(g_sample11, Usim[0, :], Time)
Yout12, Time, Xsim = cnt.lsim(g_sample12, Usim[1, :], Time)
Yout13, Time, Xsim = cnt.lsim(g_sample13, Usim[2, :], Time)
Yout14, Time, Xsim = cnt.lsim(g_sample14, Usim[3, :], Time)
Yout21, Time, Xsim = cnt.lsim(g_sample21, Usim[0, :], Time)
Yout22, Time, Xsim = cnt.lsim(g_sample22, Usim[1, :], Time)
Yout23, Time, Xsim = cnt.lsim(g_sample23, Usim[2, :], Time)
Yout24, Time, Xsim = cnt.lsim(g_sample24, Usim[3, :], Time)
Yout31, Time, Xsim = cnt.lsim(g_sample31, Usim[0, :], Time)
Yout32, Time, Xsim = cnt.lsim(g_sample32, Usim[1, :], Time)
Yout33, Time, Xsim = cnt.lsim(g_sample33, Usim[2, :], Time)
Yout34, Time, Xsim = cnt.lsim(g_sample34, Usim[3, :], Time)
Ytot1 = Yout11 + Yout12 + Yout13 + Yout14
Ytot2 = Yout21 + Yout22 + Yout23 + Yout24
Ytot3 = Yout31 + Yout32 + Yout33 + Yout34
Ytot = np.zeros((3, npts)) #
Ytot[0, :] = (Ytot1 + err_outputH1).squeeze()
Ytot[1, :] = (Ytot2 + err_outputH2).squeeze()
Ytot[2, :] = (Ytot3 + err_outputH3).squeeze()
###############################################################################plot
Yout_id11, Time, Xsim = cnt.lsim(gid11, Usim[0, :], Time)
err_inputH = np.zeros((4, npts))
err_inputH = fset.white_noise_var(npts, var_list)
err_outputH1, Time, Xsim = cnt.lsim(H_sample1, err_inputH[0, :], Time)
err_outputH2, Time, Xsim = cnt.lsim(H_sample2, err_inputH[1, :], Time)
err_outputH3, Time, Xsim = cnt.lsim(H_sample3, err_inputH[2, :], Time)
Yout = np.zeros((3, npts))
Yout11, Time, Xsim = cnt.lsim(g_sample11, Usim[0, :], Time)
Yout12, Time, Xsim = cnt.lsim(g_sample12, Usim[1, :], Time)
Yout13, Time, Xsim = cnt.lsim(g_sample13, Usim[2, :], Time)
Yout14, Time, Xsim = cnt.lsim(g_sample14, Usim[3, :], Time)
Yout21, Time, Xsim = cnt.lsim(g_sample21, Usim[0, :], Time)
Yout22, Time, Xsim = cnt.lsim(g_sample22, Usim[1, :], Time)
Yout23, Time, Xsim = cnt.lsim(g_sample23, Usim[2, :], Time)
Yout24, Time, Xsim = cnt.lsim(g_sample24, Usim[3, :], Time)
Yout31, Time, Xsim = cnt.lsim(g_sample31, Usim[0, :], Time)
Yout32, Time, Xsim = cnt.lsim(g_sample32, Usim[1, :], Time)
Yout33, Time, Xsim = cnt.lsim(g_sample33, Usim[2, :], Time)
Yout34, Time, Xsim = cnt.lsim(g_sample34, Usim[3, :], Time)
Ytot1 = Yout11 + Yout12 + Yout13 + Yout14
Ytot2 = Yout21 + Yout22 + Yout23 + Yout24
Ytot3 = Yout31 + Yout32 + Yout33 + Yout34
Ytot = np.zeros((3, npts)) #
Ytot[0, :] = (Ytot1 + err_outputH1).squeeze()
Ytot[1, :] = (Ytot2 + err_outputH2).squeeze()
Ytot[2, :] = (Ytot3 + err_outputH3).squeeze()
err_outputH3, Time, Xsim = cnt.lsim(H_sample3, err_inputH[2, :], Time)
Yout = np.zeros((3, npts))
Yout11, Time, Xsim = cnt.lsim(g_sample11, Usim[0, :], Time)
Yout12, Time, Xsim = cnt.lsim(g_sample12, Usim[1, :], Time)
Yout13, Time, Xsim = cnt.lsim(g_sample13, Usim[2, :], Time)
Yout14, Time, Xsim = cnt.lsim(g_sample14, Usim[3, :], Time)
Yout21, Time, Xsim = cnt.lsim(g_sample21, Usim[0, :], Time)
Yout22, Time, Xsim = cnt.lsim(g_sample22, Usim[1, :], Time)
Yout23, Time, Xsim = cnt.lsim(g_sample23, Usim[2, :], Time)
Yout24, Time, Xsim = cnt.lsim(g_sample24, Usim[3, :], Time)
Yout31, Time, Xsim = cnt.lsim(g_sample31, Usim[0, :], Time)
Yout32, Time, Xsim = cnt.lsim(g_sample32, Usim[1, :], Time)
Yout33, Time, Xsim = cnt.lsim(g_sample33, Usim[2, :], Time)
Yout34, Time, Xsim = cnt.lsim(g_sample34, Usim[3, :], Time)
Ytot1 = Yout11 + Yout12 + Yout13 + Yout14
Ytot2 = Yout21 + Yout22 + Yout23 + Yout24
Ytot3 = Yout31 + Yout32 + Yout33 + Yout34
Ytot = np.zeros((3, npts)) #
Ytot[0, :] = (Ytot1 + err_outputH1).squeeze()
Ytot[1, :] = (Ytot2 + err_outputH2).squeeze()
Ytot[2, :] = (Ytot3 + err_outputH3).squeeze()
###############################################################################plot
Yout_id11, Time, Xsim = cnt.lsim(gid11, Usim[0, :], Time)
Yout_id12, Time, Xsim = cnt.lsim(gid12, Usim[1, :], Time)
gid21 = cnt.tf(Id_sys.NUMERATOR[1][0], Id_sys.DENOMINATOR[1][0], 1.)
gid22 = cnt.tf(Id_sys.NUMERATOR[1][1], Id_sys.DENOMINATOR[1][1], 1.)
gid23 = cnt.tf(Id_sys.NUMERATOR[1][2], Id_sys.DENOMINATOR[1][2], 1.)
gid14 = cnt.tf(Id_sys.NUMERATOR[0][3], Id_sys.DENOMINATOR[0][3], 1.)
gid24 = cnt.tf(Id_sys.NUMERATOR[1][3], Id_sys.DENOMINATOR[1][3], 1.)
gid31 = cnt.tf(Id_sys.NUMERATOR[2][0], Id_sys.DENOMINATOR[2][0], 1.)
gid32 = cnt.tf(Id_sys.NUMERATOR[2][1], Id_sys.DENOMINATOR[2][1], 1.)
gid33 = cnt.tf(Id_sys.NUMERATOR[2][2], Id_sys.DENOMINATOR[2][2], 1.)
gid34 = cnt.tf(Id_sys.NUMERATOR[2][3], Id_sys.DENOMINATOR[2][3], 1.)
hid1 = cnt.tf(Id_sys.NUMERATOR_H[0][0], Id_sys.DENOMINATOR_H[0][0], 1.)
hid2 = cnt.tf(Id_sys.NUMERATOR_H[1][0], Id_sys.DENOMINATOR_H[1][0], 1.)
hid3 = cnt.tf(Id_sys.NUMERATOR_H[2][0], Id_sys.DENOMINATOR_H[2][0], 1.)
# output of the identified model
Yout_id11, Time, Xsim = cnt.lsim(gid11, Usim[0, :], Time)
Yout_id12, Time, Xsim = cnt.lsim(gid12, Usim[1, :], Time)
Yout_id13, Time, Xsim = cnt.lsim(gid13, Usim[2, :], Time)
Yout_id14, Time, Xsim = cnt.lsim(gid14, Usim[3, :], Time)
Yout_id21, Time, Xsim = cnt.lsim(gid21, Usim[0, :], Time)
Yout_id22, Time, Xsim = cnt.lsim(gid22, Usim[1, :], Time)
Yout_id23, Time, Xsim = cnt.lsim(gid23, Usim[2, :], Time)
Yout_id24, Time, Xsim = cnt.lsim(gid24, Usim[3, :], Time)
Yout_id31, Time, Xsim = cnt.lsim(gid31, Usim[0, :], Time)
Yout_id32, Time, Xsim = cnt.lsim(gid32, Usim[1, :], Time)
Yout_id33, Time, Xsim = cnt.lsim(gid33, Usim[2, :], Time)
Yout_id34, Time, Xsim = cnt.lsim(gid34, Usim[3, :], Time)
Yerr1, Time, Xsim = cnt.lsim(hid1, err_inputH[0, :], Time)
Yerr2, Time, Xsim = cnt.lsim(hid2, err_inputH[1, :], Time)
Yerr3, Time, Xsim = cnt.lsim(hid3, err_inputH[2, :], Time)
######plot
plt.legend(['Original system', 'Identified system'])
plt.show()
# # Validation of the identified system:
# ## Generate new time series for input and noise
switch_probability = 0.07 # [0..1]
input_range = [0.5, 1.5]
U_valid = fset.GBN_seq(npts, switch_probability, input_range)
white_noise_variance = [0.01]
e_valid = fset.white_noise_var(U_valid.size, white_noise_variance)[0]
# ## Compute time responses for true system with new inputs
Yvalid1, Time, Xsim = cnt.lsim(g_sample, U_valid, Time)
Yvalid2, Time, Xsim = cnt.lsim(h_sample, e_valid, Time)
Ytotvalid = Yvalid1 + Yvalid2
# ## Compute time responses for identified system with new inputs
Yidvalid1, Time, Xsim = cnt.lsim(Id_sys.G, U_valid, Time)
Yidvalid2, Time, Xsim = cnt.lsim(Id_sys.H, e_valid, Time)
Yidtotvalid = Yidvalid1 + Yidvalid2
# ## Check responses are almost equal
plt.figure(7)
plt.plot(Time, U_valid)
plt.ylabel("Input GBN")
plt.xlabel("Time")
plt.title("Input, validation data (Switch probability=0.07)")
plt.grid()
Ytot[2, :] = (Ytot3 + err_outputH3).squeeze()
###############################################################################plot
Yout_id11, Time, Xsim = cnt.lsim(gid11, Usim[0, :], Time)
Yout_id12, Time, Xsim = cnt.lsim(gid12, Usim[1, :], Time)
Yout_id13, Time, Xsim = cnt.lsim(gid13, Usim[2, :], Time)
Yout_id14, Time, Xsim = cnt.lsim(gid14, Usim[3, :], Time)
Yout_id21, Time, Xsim = cnt.lsim(gid21, Usim[0, :], Time)
Yout_id22, Time, Xsim = cnt.lsim(gid22, Usim[1, :], Time)
Yout_id23, Time, Xsim = cnt.lsim(gid23, Usim[2, :], Time)
Yout_id24, Time, Xsim = cnt.lsim(gid24, Usim[3, :], Time)
Yout_id31, Time, Xsim = cnt.lsim(gid31, Usim[0, :], Time)
Yout_id32, Time, Xsim = cnt.lsim(gid32, Usim[1, :], Time)
Yout_id33, Time, Xsim = cnt.lsim(gid33, Usim[2, :], Time)
Yout_id34, Time, Xsim = cnt.lsim(gid34, Usim[3, :], Time)
Yerr1, Time, Xsim = cnt.lsim(hid1, err_inputH[0, :], Time)
Yerr2, Time, Xsim = cnt.lsim(hid2, err_inputH[1, :], Time)
Yerr3, Time, Xsim = cnt.lsim(hid3, err_inputH[2, :], Time)
plt.figure(7)
plt.subplot(3, 1, 1)
plt.plot(Time, Ytot1)
plt.plot(Time, Yout_id11 + Yout_id12 + Yout_id13 + Yout_id14)
plt.ylabel("y_1,out")
plt.grid()
plt.xlabel("Time")
plt.title("Gu (validation data)")
plt.legend(['Original system', 'Identified system'])
plt.subplot(3, 1, 2)
hid1 = cnt.tf(Id_sys.NUMERATOR_H[0][0], Id_sys.DENOMINATOR_H[0][0], 1.)
hid2 = cnt.tf(Id_sys.NUMERATOR_H[1][0], Id_sys.DENOMINATOR_H[1][0], 1.)
hid3 = cnt.tf(Id_sys.NUMERATOR_H[2][0], Id_sys.DENOMINATOR_H[2][0], 1.)
# output of the identified model
Yout_id11, Time, Xsim = cnt.lsim(gid11, Usim[0, :], Time)
Yout_id12, Time, Xsim = cnt.lsim(gid12, Usim[1, :], Time)
Yout_id13, Time, Xsim = cnt.lsim(gid13, Usim[2, :], Time)
Yout_id14, Time, Xsim = cnt.lsim(gid14, Usim[3, :], Time)
Yout_id21, Time, Xsim = cnt.lsim(gid21, Usim[0, :], Time)
Yout_id22, Time, Xsim = cnt.lsim(gid22, Usim[1, :], Time)
Yout_id23, Time, Xsim = cnt.lsim(gid23, Usim[2, :], Time)
Yout_id24, Time, Xsim = cnt.lsim(gid24, Usim[3, :], Time)
Yout_id31, Time, Xsim = cnt.lsim(gid31, Usim[0, :], Time)
Yout_id32, Time, Xsim = cnt.lsim(gid32, Usim[1, :], Time)
Yout_id33, Time, Xsim = cnt.lsim(gid33, Usim[2, :], Time)
Yout_id34, Time, Xsim = cnt.lsim(gid34, Usim[3, :], Time)
Yerr1, Time, Xsim = cnt.lsim(hid1, err_inputH[0, :], Time)
Yerr2, Time, Xsim = cnt.lsim(hid2, err_inputH[1, :], Time)
Yerr3, Time, Xsim = cnt.lsim(hid3, err_inputH[2, :], Time)
######plot
#
import matplotlib.pyplot as plt
plt.close('all')
plt.figure(0)
plt.subplot(4, 1, 1)
plt.plot(Time, Usim[0, :])
plt.grid()
plt.ylabel("Input 1 GBN")
Time = np.linspace(0, tfin, npts)
# #INPUT#
Usim = np.zeros((4, npts))
Usim_noise = np.zeros((4, npts))
Usim[0, :] = fset.GBN_seq(npts, 0.03, [-0.33, 0.1])
Usim[1, :] = fset.GBN_seq(npts, 0.03)
Usim[2, :] = fset.GBN_seq(npts, 0.03, [2.3, 5.7])
Usim[3, :] = fset.GBN_seq(npts, 0.03, [8., 11.5])
# Adding noise
err_inputH = np.zeros((4, npts))
err_inputH = fset.white_noise_var(npts, var_list)
err_outputH1, Time, Xsim = cnt.lsim(H_sample1, err_inputH[0, :], Time)
err_outputH2, Time, Xsim = cnt.lsim(H_sample2, err_inputH[1, :], Time)
err_outputH3, Time, Xsim = cnt.lsim(H_sample3, err_inputH[2, :], Time)
# OUTPUTS
Yout = np.zeros((3, npts))
Yout11, Time, Xsim = cnt.lsim(g_sample11, Usim[0, :], Time)
Yout12, Time, Xsim = cnt.lsim(g_sample12, Usim[1, :], Time)
Yout13, Time, Xsim = cnt.lsim(g_sample13, Usim[2, :], Time)
Yout14, Time, Xsim = cnt.lsim(g_sample14, Usim[3, :], Time)
Yout21, Time, Xsim = cnt.lsim(g_sample21, Usim[0, :], Time)
Yout22, Time, Xsim = cnt.lsim(g_sample22, Usim[1, :], Time)
Yout23, Time, Xsim = cnt.lsim(g_sample23, Usim[2, :], Time)
Yout24, Time, Xsim = cnt.lsim(g_sample24, Usim[3, :], Time)
Yout31, Time, Xsim = cnt.lsim(g_sample31, Usim[0, :], Time)
Yout32, Time, Xsim = cnt.lsim(g_sample32, Usim[1, :], Time)
Yout33, Time, Xsim = cnt.lsim(g_sample33, Usim[2, :], Time)