0

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.

Fabich
  • 2,768
  • 3
  • 30
  • 44
MartinS
  • 11
  • 5

1 Answers1

1

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
)
  • Thanks alot it works now. Though I still don't fully understand, so what does this measurement martix do? Is it like telling how to get the speeds from the koordinates? Also is case the timesteps are not always one second but variable, how would that influence this? – MartinS Feb 02 '21 at 10:43
  • @MartinS if it works, please mark the answer as accepted and upvote it. The measurement matrix tells how your measurements and true state are connected. Check here: https://en.wikipedia.org/wiki/Kalman_filter. In case your time deltas are not static, you might want to update the transition matrix before each iteration. Check here: https://stackoverflow.com/questions/43377626/how-to-use-kalman-filter-in-python-for-location-data. – Razmik Melikbekyan Feb 02 '21 at 11:32