I am streaming data into a C# application from an inertial sensor. The data is a bit noisy, and so I need to add a filter to smooth it. I have a kalman filter implementation that works great when given an array, but I cannot get my head around how to use it on a constant datastream.
I have:
double sensorData; //the noisy value, constantly updating from another class.
The filter:
public static double[] noisySine = new double[20] { 40, 41, 38, 40, 45, 42, 43, 44, 40, 38, 44, 45, 40, 39, 37, 41, 42, 70, 44, 42 };
public static double[] clean = new double[20];
public static void KalmanFilter(double[] noisy)
{
double A = double.Parse("1"); //factor of real value to previous real value
// double B = 0; //factor of real value to real control signal
double H = double.Parse("1");
double P = double.Parse("0.1");
double Q = double.Parse("0.125"); //Process noise.
double R = double.Parse("1"); //assumed environment noise.
double K;
double z;
double x;
//assign to first measured value
x = noisy[0];
for (int i = 0; i < noisy.Length; i++)
{
//get current measured value
z = noisy[i];
//time update - prediction
x = A * x;
P = A * P * A + Q;
//measurement update - correction
K = P * H / (H * P * H + R);
x = x + K * (z - H * x);
P = (1 - K * H) * P;
//estimated value
clean[i] = x;
Console.WriteLine(noisy[i] + " " + clean[i]);
}
}
How can i stream a double in, instead of an array, and return a (filtered) double?
Thank you.