Not getting expected performance from kalman filter+mahalanobis distance

Robotics Asked on December 14, 2020

I am using a 1D lidar in one of my projects and it returns the distance it measures, in millimeters (mm). At some point in time, it gives garbage values that go as high as 10,000 or higher, when the value is expected to be under 200.

To mitigate such such outliers, I though of using a Kalman filter. Although it is something new to me, I used this MEDIUM blog as a start and tried modifying his code to suit my case.

I have the following data
enter image description here. The visible peaks are the noise I added.

I create a live kalman filter as:

import numpy as np
import math
from numpy import  dot
from numpy.linalg import inv

class kf():
    def __init__(self):
        ### Defining filter
        #Transition Matrix
        self.F = np.array([[1.0]])
        #Observation Matrix
        self.H = np.array([[1.0]])
        #Process Noise Covariance
        self.Q = np.array([[0.09]])  # how much I trust the model
        #Measurement Noise Covariance
        self.R = np.array([[0.2*0.2]])
        # Control action Matrix
        self.B = np.array([1])
        #Control input
        self.U = np.array([1])

        #Covariance Matrix
        self.P = 1.0

        self.x_old = 100.0

    def filter(self, y):
        x_old = self.x_old
        F = self.F
        B = self.B
        U = self.U
        P = self.P
        Q = self.Q
        H = self.H
        R = self.R
        I = np.identity(1)
        x = dot(F, x_old) + dot(B, U)
        P = dot(F, dot(P, F.T)) + Q

        e = y - H*x
        S = dot(H, dot(P, H.T)) + R

        Mahalanobis distance approximation
        MD = math.sqrt(e*e)/S
        #Weighted MD
        MDw = 1/(1+(math.exp(-MD) + 0.9))
        #New Measurement Noise Covariance
        R = np.array([[4*MDw]]) # bigger the R, the lesser the trust the system will have on the sensor measurement.
        #Kalman gain
        K = dot(P, dot(H.T, inv(S)))
        x_new = x + dot(K, dot(e,K))
        self.x_old = x_new
        self.P = dot(I - dot(K,H), P)

        return x_new, x

And run as instance of this class each time I read the lidar value. The results are, (ignore the blue plot)
ignore the blue plot

I expected the filter to not be so severly affected by the outliers, I am new to this field and hence tried playing with the tunable parameters but to no gain :/

Where am I going wrong ? Is there any other non-computation-intense method for filtering data in real time ?

One Answer

You are calculating the new R, but you're not using it. You just replace the new R with the line R = self.R.

You are not removing the outliers, because you are ditching that result!

Correct answer by Bruno Pinto on December 14, 2020

Add your own answers!

Related Questions

What does the normalized image coordinates imply?

0  Asked on October 3, 2021 by lzx071021


Rapid code pick and place

1  Asked on October 3, 2021 by ramil-aliyev


How can I get the equations of motion used in webots?

1  Asked on October 3, 2021 by q-than-a


Handling Inverse Kinematics error on a UR Robot

1  Asked on October 3, 2021 by user24261


ROS2 and TypeError when publishing custom message to Topic

1  Asked on October 3, 2021 by anthares


BLDC Stuttering when attached to Arduino Nano

1  Asked on October 3, 2021 by moazzam-salman


Determining required torque for DC motor

0  Asked on October 3, 2021 by timetraveller


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP