i want to use kalman filter to estimate my phone position, the measurments data is at this point just the accelerometer and the sampling rate is 3ms, i used the library pykalman, i have also wrote my own implementation of kalman filters and they both return the same results. transition matrix :
dt = 0.003
F = np.matrix([[1.0, 0.0, 0.0,dt,0.0,0.0,0.5*(dt)*(dt), 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0,dt,0.0,0.0, 0.5*(dt)*(dt), 0.0],
[0.0, 0.0, 1.0, 0.0,0.0,dt,0.0, 0.0, 0.5*(dt)*(dt)],
[0.0, 0.0, 0.0,1.0,0.0,0.0,dt, 0.0, 0.0],
[0.0, 0.0, 0.0,0.0,1.0,0.0,0.0, dt, 0.0],
[0.0, 0.0, 0.0,0.0,0.0,1.0,0.0, 0.0, dt],
[0.0, 0.0, 0.0,0.0,0.0,0.0,1.0, 0.0, 0.0],
[0.0, 0.0, 0.0,0.0,0.0,0.0,0.0, 1.0, 0.0],
[0.0, 0.0, 0.0,0.0,0.0,0.0,0.0, 0.0, 1.0]])
measurment matrix
H =np.matrix([[0.0, 0.0, 0.0,0.0,0.0,0.0,1.0, 0.0, 0.0],
[0.0, 0.0, 0.0,0.0,0.0,0.0,0.0, 1.0, 0.0],
[0.0, 0.0, 0.0,0.0,0.0,0.0,0.0, 0.0, 1.0]])
measurment noise
rp = 0.02 # Noise of Position Measurement
R = np.matrix([[rp, 0.0, 0.0],
[0.0, rp, 0.0],
[0.0, 0.0, rp]])
initial state vector
X0 = np.matrix([0,0,0,0,0,0,measurments[0][0],measurments[0][1],measurments[0][2]])
state covariance matrix
P0 =np.matrix([[1, 0, 0, 0, 0, 0,0,0,0],
[0, 1, 0,0, 0, 0,0,0,0],
[0, 0, 1, 0, 0, 0,0,0,0],
[0, 0, 0, 1, 0, 0,0,0,0],
[0, 0, 0, 0, 1, 0,0,0,0],
[0, 0, 0, 0, 0, 1,0,0,0],
[0, 0, 0, 0, 0, 0,1,0,0],
[0, 0, 0, 0, 0, 0,0,1,0],
[0, 0, 0, 0, 0, 0,0,0,1]])
i load the accelerometer data, and then use pykalman to find x,y and z.
measurments = []
for i in range(len(df_acc)):
measurments.append([df_acc.iloc[i]['x'],df_acc.iloc[i]['y'],df_acc.iloc[i]['z']-9.82])
n_timesteps = len(measurments)
n_dim_state = 9
filtered_state_means = np.zeros((n_timesteps, n_dim_state))
filtered_state_covariances = np.zeros((n_timesteps, n_dim_state, n_dim_state))
kf = KalmanFilter(transition_matrices = F,
observation_matrices = H,
observation_covariance = R,
initial_state_mean = X0,
initial_state_covariance = P0)
for t in range(n_timesteps):
if t == 0:
filtered_state_means[t] = X0
filtered_state_covariances[t] = P0
else:
filtered_state_means[t], filtered_state_covariances[t] = (
kf.filter_update(
filtered_state_means[t-1],
filtered_state_covariances[t-1],
measurments[t]))
the results are shown in the picture, the data i captured with my phone while walking in a small appartment i can't have walked 1 mile lol, plus i've been walking in a straight line the other axis claim that ive walked 200 meters along x and z which is impossible. I can' t find where i made a mistake.