Skip to content

Commit e982f8a

Browse files
committed
add the example2
1 parent 5a32bec commit e982f8a

7 files changed

Lines changed: 490 additions & 0 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
L = 30 # the len of sequence
6+
x1 = 1; x2 = 1; x3 = 1; x4 = 0 # the init_valce of register
7+
#print("the x4 :" , x4)
8+
S = 1
9+
U = [] # Inverse_M_sequence
10+
M = [] # M_sequence
11+
for i in range(1, L + 1, 1):
12+
IM = S ^ x4
13+
if IM == 0:
14+
U.append(-1) # Inverse_M_sequence
15+
else:
16+
U.append(1) # Inverse_M_sequence
17+
18+
S = not(S)
19+
m = x3 ^ x4
20+
M.append( m ) # M_sequence
21+
x4 = x3; x3 = x2; x2 = x1 ; x1 = m
22+
23+
print("the M_sequence:", M)
24+
print("the_inverse_M_sequence:", U)
25+
#figure
26+
plt.figure(1)
27+
ax = plt.subplot(2, 1, 1)
28+
ax1 = plt.subplot(2, 1, 2)
29+
30+
plt.sca(ax)
31+
ax.plot(M, 'Red', label = 'M_sequence')
32+
plt.title('M_sequence')
33+
#plt.xlabel("k")
34+
plt.ylabel("amplitude")
35+
ax.legend()
36+
37+
plt.sca(ax1)
38+
ax1.plot(U, 'Blue', label = 'Inverse_M_sequence')
39+
plt.title('Inverse_M_sequence')
40+
plt.xlabel("k")
41+
plt.ylabel("amplitude")
42+
plt.legend()
43+
44+
45+
plt.show()
46+
47+
exit()
48+
49+
50+
51+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
a = np.array([1, -0.4]); b = np.array([1]); c = np.array([1, -0.4])
6+
na = len(a) -1; nb = len(b) - 1; nc = len(c) - 1
7+
#print("the na: ", na)
8+
9+
n = max(na, nb, nc)
10+
print("the max of (na, nb, nc):", n)
11+
a0 = a; b0 = b; c0 = c
12+
for i in range(na, n, 1):
13+
a0 = np.append(a0, 0)
14+
15+
for i in range(nb, n, 1):
16+
b0 = np.append(b0, 0)
17+
18+
for i in range(nc, n, 1):
19+
c0 = np.append(c0, 0)
20+
21+
print("the a0: ", b0)
22+
23+
p = []; qg = []; qh = []
24+
deltau2 = 1; deltav2 = 1
25+
for i in range(n):
26+
p.append(a0[i])
27+
qg.append(b0[i])
28+
qh.append(c0[i])
29+
30+
for i in range(n - 1, 0 , -1):
31+
for j in range(0, 2, 1):
32+
p_2[j, i] = p[0] * p[i]
33+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import os
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
L = 500 # the len of noise
6+
7+
# e(k) = c/d * f(k) f(k) is white_noise and e(k) is colored_noise
8+
d = np.array([1, -1.5, 0.7, 0.1]) # the c
9+
c = np.array([1, 0.5, 0.2]) # the d
10+
11+
nd = len(d) - 1 #
12+
nc = len(c) - 1 #
13+
14+
white_noise_init = np.zeros(nc) # the init_value of white_noise
15+
16+
colored_noise_init = np.zeros(nd) # the init_valuw of colored_noise
17+
18+
white_noise = np.random.normal(0, 1, L) ## the normal distribution
19+
20+
colored_noise = [] # colored_noise
21+
random_walk = [0.0]
22+
for i in range(L):
23+
24+
old_total_noise = random_walk[i]
25+
new_noise = white_noise[i]
26+
random_walk.append(old_total_noise + new_noise)
27+
28+
noise_1 = np.dot(-d[1 : nd + 1], colored_noise_init.transpose()) ##d * colored_noise_init
29+
white_noise_init_array = np.insert(white_noise_init, 0, white_noise[i])
30+
noise_2 = np.dot(white_noise_init_array, c.transpose()) # c * white_noise
31+
32+
colored_noise.append(noise_1 + noise_2) ## save the colored_noise
33+
#update the data
34+
for j in range(nd - 1, 0, -1):
35+
colored_noise_init[j] = colored_noise_init[j - 1]
36+
37+
colored_noise_init[0] = noise_2 + noise_1
38+
39+
for k in range(nc-1, 0, -1):
40+
white_noise_init[k] = white_noise_init[k - 1]
41+
42+
white_noise_init[0] = white_noise[i]
43+
44+
45+
random_walk = np.array(random_walk)
46+
47+
#figure
48+
plt.figure(1)
49+
ax = plt.subplot(2, 1, 1)
50+
ax1 = plt.subplot(2, 1, 2)
51+
52+
plt.sca(ax)
53+
ax.plot(white_noise, 'Red', label = 'white_noise')
54+
plt.title('white_noise')
55+
#plt.xlabel("k")
56+
plt.ylabel("amplitude")
57+
ax.legend()
58+
59+
plt.sca(ax1)
60+
ax1.plot(colored_noise, 'Blue', label = 'colored_noise')
61+
plt.title('colored_noise')
62+
plt.xlabel("k")
63+
plt.ylabel("amplitude")
64+
plt.legend()
65+
66+
plt.figure(2)
67+
68+
ax3 = plt.subplot(1, 1, 1)
69+
ax3.plot(random_walk, 'Green' ,label = "random_walk")
70+
plt.title('random_walk')
71+
plt.xlabel("k")
72+
plt.ylabel("amplitude")
73+
plt.legend()
74+
75+
plt.show()
76+
77+
exit()
78+
79+
80+
81+
82+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import numpy.matlib
5+
#the least_square
6+
a = np.array([1, -1.5, 0.7]); b = np.array([1, 0.5]); d = 3 #
7+
8+
len_a = len(a) -1; len_b = len(b) - 1 ; L = 1500
9+
10+
Input_init = np.zeros(d + len_b) #the init_value of input
11+
Output_init = np.zeros(len_a) # the init_value of ouput
12+
13+
14+
white_noise = np.random.normal(0, 1, L) # the white_noise
15+
white_noise_1 = np.sqrt(0.1) * np.random.normal(0, 1, L)
16+
17+
#theta = np.append(a[1: len_a + 1], b) #the permeter of object
18+
19+
theta_1 = np.zeros(len_a + len_b + 1)
20+
P = np.eye(len_a + len_b + 1, len_a + len_b + 1)
21+
22+
forgetting_factor = 0.98 #the forgetting_factor
23+
phi = np.array([]) # the
24+
thetae_1 = []
25+
thetae_2 = []
26+
thetae_3 = []
27+
thetae_4 = []
28+
29+
theta_real_1 = []
30+
theta_real_2 = []
31+
theta_real_3 = []
32+
theta_real_4 = []
33+
34+
for i in range(L):
35+
if i == 500:
36+
a = [1, -1, 0.4]; b = [1.5, 0.2]
37+
38+
theta = np.append(a[1: len_a + 1], b) #the permeter of object
39+
#print("the theta: ", theta)
40+
theta_real_1.append(theta[0])
41+
theta_real_2.append(theta[1])
42+
theta_real_3.append(theta[2])
43+
theta_real_4.append(theta[3])
44+
45+
temp = np.append(-Output_init, Input_init[d - 1: d + len_b]) # X
46+
temp_y = np.dot(temp, theta.transpose()) + white_noise_1[i] # Y
47+
48+
phi = np.concatenate((phi, temp))
49+
temp_matrix = np.matrix(temp)
50+
P_matrix = np.matrix(P)
51+
52+
K = (P_matrix * temp_matrix.transpose()) /(forgetting_factor + temp_matrix * P_matrix *temp_matrix.transpose())
53+
thetae_temp = theta_1 + K * (temp_y - temp * theta_1)
54+
55+
thetae_1.append(thetae_temp[0,0])
56+
thetae_2.append(thetae_temp[1,1])
57+
thetae_3.append(thetae_temp[2,2])
58+
thetae_4.append(thetae_temp[3,3])
59+
P = (np.eye(len_a + len_b + 1) - K * temp) * P /forgetting_factor
60+
61+
#update the data
62+
theta_1 = thetae_temp
63+
64+
for k in range(d + len_b - 1, 0, -1):
65+
Input_init[k] = Input_init[k - 1]
66+
Input_init[0] = white_noise[i]
67+
68+
for j in range(len_a - 1, 0, -1):
69+
Output_init[j] = Output_init[j - 1]
70+
Output_init[0] = temp_y
71+
72+
73+
plt.figure(1)
74+
ax = plt.subplot(1, 1, 1)
75+
76+
plt.sca(ax)
77+
ax.plot(thetae_1, 'Red', label = 'thetae_1')
78+
ax.plot(thetae_2, 'Blue', label = 'thetae_2')
79+
ax.plot(thetae_3, 'Green', label = 'thetae_3')
80+
ax.plot(thetae_4, 'Yellow', label = 'thetae_4')
81+
ax.plot(theta_real_1, 'brown', label = 'theta_real_1', linestyle ="-" )
82+
ax.plot(theta_real_2, 'cyan', label = 'theta_real_2', linestyle="-" )
83+
ax.plot(theta_real_3, 'burlywood', label = 'theta_real_3', linestyle="-" )
84+
ax.plot(theta_real_4, 'coral', label = 'theta_real_4', linestyle="-" )
85+
plt.title('thetae')
86+
plt.xlabel("k")
87+
plt.ylabel("value")
88+
89+
ax.legend()
90+
plt.show()

example2/least_square.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
#the least_square
6+
a = np.array([1, -1.5, 0.7]); b = np.array([1, 0.5]); d = 3 #
7+
8+
len_a = len(a) -1; len_b = len(b) - 1 ; L =500
9+
Input_init = np.zeros(d + len_b) #the init_value of input
10+
Output_init = np.zeros(len_a) # the init_value of ouput
11+
12+
x1 = 1; x2 = 1; x3 = 1; x4 = 0; S = 1 # the init_value of move_register and square_wave
13+
14+
white_noise = np.random.normal(0, 1, L) # the white_noise
15+
16+
phi = np.array([]) # the
17+
y = np.array([])
18+
U = np.array([]) #the inverse_M_sequence
19+
theta = np.append(a[1: len_a + 1], b) #the permeter of object
20+
for i in range(L):
21+
# Y = AX + xi(noise)
22+
temp = np.append(-Output_init, Input_init[d - 1: d + len_b]) # X
23+
phi = np.concatenate((phi, temp))
24+
#print("the phi: ", phi)
25+
26+
temp_y = np.dot(temp, theta.transpose()) + white_noise[i] # Y
27+
#save the y
28+
y = np.append(y, temp_y)
29+
30+
#the inverse_M_sequence
31+
IM = S ^ x4
32+
if IM == 0:
33+
U = -1
34+
else:
35+
U = 1
36+
S = not(S); M = x3 ^ x4
37+
38+
#update the data
39+
x4 = x3; x3 = x2; x2 = x1; x1 = M
40+
41+
for k in range(d + len_b - 1, 0, -1):
42+
Input_init[k] = Input_init[k - 1]
43+
Input_init[0] = U
44+
45+
for j in range(len_a - 1, 0, -1):
46+
Output_init[j] = Output_init[j - 1]
47+
Output_init[0] = temp_y
48+
49+
# the np.narray to np.matrix
50+
y_matrix = np.matrix(y.reshape(len(y), 1))
51+
52+
phi_matrix = np.matrix(phi.reshape((L, 4)))
53+
54+
55+
print("the phi_matrix_transpose*phi_matrix:", np.matmul(phi_matrix.transpose(), phi_matrix))
56+
57+
58+
temp_phi_phi = np.linalg.inv(np.matmul(phi_matrix.transpose(), phi_matrix))
59+
temp_phi_phi_phi = np.dot(temp_phi_phi, phi_matrix.transpose())
60+
61+
theta_estimate = np.dot(temp_phi_phi_phi, y)
62+
63+
print("the theta_estimate: ", theta_estimate)
64+
65+

0 commit comments

Comments
 (0)