import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
L = 100 #序列长度
Y = np.zeros(L)
phi = np.zeros((L,4))
u = np.random.randn(L,1)
xi = np.sqrt(0) * np.random.randn(L,1)
alpha = 1 #
c = 0.1 # 修正因子
y1 = y2 =0
u4 = u3 = u2 =u1 =0
theta = np.array([1.5,-0.7,1,0.5])
P = 1e6 * np.eye(4)
theta1 = np.zeros((L,4))
theta1_1 = np.zeros(4)
for i in np.arange(L):
phi[i,:] = np.array([y1 , y2 ,u3 ,u4])
#Y[i] = 1.5*y1 -0.7*y2 + u3 + 0.5*u4 + xi[i]
Y[i] = np.dot(theta,phi[i,:]) + xi[i]
# RGC
K = (alpha*phi[i,:])/(c+np.dot(phi[i,:],phi[i,:]))
theta1[i,:] = theta1_1 + K*(Y[i]-np.dot(phi[i,:],theta1_1))
# 数据更新
theta1_1 = theta1[i,:]
y2 = y1
y1 = Y[i]
u4 = u3
u3 = u2
u2 = u1
u1 = u[i]
plt.subplot(3,1,1)
plt.title('输入-白噪声')
plt.step(np.arange(L),u)
plt.subplot(3,1,2)
plt.title('输出-Y')
plt.plot(np.arange(L),Y)
plt.subplot(3,1,3)
plt.title('系数Theta')
plt.plot(theta1)
plt.subplots_adjust(hspace = 0.45)
plt.show()