forked from salmoshu/location
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkf1d.py
64 lines (57 loc) · 1.41 KB
/
kf1d.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
import random
import numpy as np
import matplotlib.pyplot as plt
'''
一维线性情况:
假设一个人做步长恒定为1m的直线运动
指纹库也集中在一条直线上面
'''
Z = []
noise = []
# 对输入数据加入gauss噪声
# 定义gauss噪声的均值和方差
for v in range (100):
n = random.gauss(0, 3) # (mu, sigma)
v += n
noise.append(n)
Z.append(v)
Z # WiFi指纹定位值
T = [] # PDR预测位置
S = [] # 融合估计位置
X = np.matrix('10; 1') # 状态
P = np.matrix('0.8, 0; 0, 0.8') # 状态协方差矩阵
F = np.matrix('1, 1; 0, 1') # 状态转移矩阵
Q = np.matrix('0.01, 0; 0, 0.01') # 状态转移协方差矩阵
H = np.matrix('1, 0') # 观测矩阵
R = 3 # 观测噪声方差
# PDR
T.append(X)
X_temp = X
for i in range(99):
X_temp = F * X_temp
T.append(X_temp)
# KF fusion
S.append(X)
for i in range(99):
X_ = F * X
P_ = F * P * F.T + Q
K = P_ * H.T / (H * P_ * H.T + R)
X = X_ + K * (Z[i] - H * X_)
P = (np.eye(2) - K * H) * P_
S.append(X)
X0 = []
X1 = []
for i1, i2 in zip(S, T):
X0.append(i1[0:][0])
X1.append(i2[0:][0])
index = range(100)
plt.scatter(index, np.array(X1)+80, label='pdr')
plt.plot(index, np.array(index)+80)
plt.scatter(index, np.array(Z)+40, label='wifi')
plt.plot(index, np.array(index)+40)
plt.scatter(index, X0, label='fusion')
plt.plot(index, index)
plt.xlabel('status')
plt.ylabel('distance')
plt.legend()
plt.show()