-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkalman_plots.py
68 lines (52 loc) · 1.53 KB
/
kalman_plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import matplotlib.pyplot as plt
from kalman_filter import kalman_filter
from parameters import *
def generate_obs_seq():
"""
Generates a realization of the WSS observation
"""
x = np.zeros(N)
variance_x = sigma_w ** 2 / (1 - alpha ** 2)
sigma_x = np.sqrt(variance_x)
x_0 = sigma_x * np.random.randn(1)
x[0] = x_0
for n in range(1, N):
w = sigma_w * np.random.randn(1)
x[n] = alpha * x[n - 1] + w
return x
def main():
"""
For the Kalman filter, find and plot the true and estimated signal
and the true and predicted error
"""
y = generate_obs_seq()
x_true = np.zeros(n0)
x_pred = np.zeros(n0)
P_true = np.zeros(n0)
P_pred = np.zeros(n0)
x_true[0] = y[0]
x_pred[0] = y[0]
P_true[0] = 1
P_pred[0] = 1
for i in range(1, n0):
x_true[i], x_pred[i], P_true[i], P_pred[i] = kalman_filter(y[i], sigma_v, sigma_w, x_true[i - 1], P_true[i - 1])
# Plot and save the required plots
plt.figure()
plt.plot(x_true, label="True x[n]")
plt.plot(x_pred, label="Estimated x[n]")
plt.xlabel("n")
plt.ylabel("x[n]")
plt.title("True and Estimated signal")
plt.legend()
plt.savefig('./results/Kalman_1.png')
plt.figure()
plt.plot(P_true, label="True Error")
plt.plot(P_pred, label="Estimated Error")
plt.xlabel("n")
plt.ylabel("Error")
plt.title("True and Predicted error")
plt.legend()
plt.savefig('./results/Kalman_2.png')
if __name__ == '__main__':
main()