Skip to content

Instantly share code, notes, and snippets.

@satoruhiga
Last active September 24, 2015 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoruhiga/777748 to your computer and use it in GitHub Desktop.
Save satoruhiga/777748 to your computer and use it in GitHub Desktop.
SimpleKalmanFilter
class SimpleKalmanFilter
{
public:
float xhat, xhatminus;
float P, Pminus;
float _x, _p;
float Q, R, K;
SimpleKalmanFilter()
{
_x = xhat = 0;
_p = Pminus = 1;
}
void init(float Q=1e-5, float R=0.01)
{
this->Q = Q;
this->R = R;
}
float filter(float z)
{
xhatminus = _x;
Pminus = _p + Q;
K = Pminus / (Pminus + R);
xhat = xhatminus + K * (z - xhatminus);
P = (1 - K) * Pminus;
_x = xhat;
_p = P;
return xhat;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment