I'm trying to use the kalman filter on a dataset of GPS data for noise reduction. For this I checked if there already is an online implementation and found pykalman. I'm trying to use it, but for some reason I'm not getting how i'm supposed to correctly assign the matrixes. When i try to run it, it tells me i have a dimension error. So first things first, what I'm trying to do/get: I want the kalman filter to estimate the postion of the next time step with the old positon + velocity * t. The Velocity for the next step is simply the old velocity. Each time step is excatly 1 second. I have measurments in x and y direction and with x_t,y_t,vx_t,vy_t the Transition matrix should look something like this (I think):
transition_matrix = np.array([[1, 0,1,0],
[0, 1,0,1],
[0,0,1,0],
[0,0,0,1]])
My measurements look like this:
[[ 7.616984 47.53661 ]
[ 7.616999 47.536629]
[ 7.616997 47.536635]
...
[ 7.617117 47.536999]
[ 7.617117 47.536999]
[ 7.617117 47.536999]]
What i tried so far: I have tried to piece together, how it works from variuos online sources and came up with this:
import numpy as np
import pykalman
import geopandas
measurments= np.asarray(gdf[["Longitude_deg", "Latitude_deg"]])
#gdf is a geopandas dataframe, but no i'm not currently using the geometry of it.
transition_matrix = np.array([[1, 0,1,0],
[0, 1,0,1],
[0,0,1,0],
[0,0,0,1]])
#the pykalman documentation says the model parameter can but don't have to be specified and it will simply use defaults for unspecified parameters:
kf = pykalman.KalmanFilter(
transition_matrices =transition_matrix
)
(smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurments)
Trying to run the last part will give me the following error:
shapes (2,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)
Which I understand as far as the Matrices used don't have the correct sizes to be used with each other. In general my understanding of matrices is very limited. I hope someone can help me.
Based on your model your state vector is the following: [x, y, v_x, v_y]
and you are observing (measuring) only [x, y]
. So you need to properly define also measurement matrix H
, which maps the true state space into the observed space: z=Hx + noise
. So in your case, it is very simple:
observation_matrix = np.array(
[[1, 0, 0, 0],
[0, 1, 0, 0]]
)
This will work properly:
kf = pykalman.KalmanFilter(
transition_matrices=transition_matrix,
observation_matrices=observation_matrix
)