Skip to content

Instantly share code, notes, and snippets.

@NiKoTron
Created August 4, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiKoTron/95bd6563d072a1a3ee23fbde41d93db5 to your computer and use it in GitHub Desktop.
Save NiKoTron/95bd6563d072a1a3ee23fbde41d93db5 to your computer and use it in GitHub Desktop.
simple kalman filter
/**
* Kalman filter implementation
*/
public class KalmanFilterSimple {
private float X0;
private float P0;
private float F;
private float Q;
private float H;
private float R;
private float state;
private float covariance;
/**
* Initializes a new instance of the KalmanFilterSimple
*/
public KalmanFilterSimple(float q, float r) {
this(q,r,1,1);
}
/**
* Initializes a new instance of the KalmanFilterSimple
* @param q - Measurement noise
* @param r - Environment noise
* @param f - Factor of real value to previous real value
* @param h - Factor of measured value to real value
*/
public KalmanFilterSimple(float q, float r, float f, float h)
{
Q = q;
R = r;
F = f;
H = h;
}
public void setState(float state, float covariance)
{
this.state = state;
this.covariance = covariance;
}
public void correct(float data)
{
X0 = F*state;
P0 = F*covariance*F + Q;
float K = H * P0 / (H * P0 * H + R);
state = X0 + K*(data - H*X0);
covariance = (1 - K*H)*P0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment