forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalman filter.c
57 lines (44 loc) · 1.6 KB
/
Kalman filter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
// Kalman filter variables
double x; // Estimated state
double P; // Estimated error covariance
double A; // State transition model
double H; // Measurement model
double Q; // Process noise
double R; // Measurement noise
// Kalman filter prediction step
void predict() {
x = A * x; // Predict the next state
P = A * P * A + Q; // Predict the error covariance
}
// Kalman filter correction step
void correct(double z) {
double K = P * H / (H * P * H + R); // Calculate Kalman gain
x = x + K * (z - H * x); // Correct the state estimate
P = (1 - K * H) * P; // Correct the error covariance
}
int main() {
//Give your own kalman variables as input.
printf("Enter the initial state (X): ");
scanf("%lf", &x);
printf("Enter the initial error covariance (P): ");
scanf("%lf", &P);
printf("Enter the state transition model (A): ");
scanf("%lf", &A);
printf("Enter the measurement model (H): ");
scanf("%lf", &H);
printf("Enter the process noise (Q): ");
scanf("%lf", &Q);
printf("Enter the measurement noise (R): ");
scanf("%lf", &R);
// Simulated measurements
double measurements[] = {1.1, 1.2, 1.3, 1.4, 1.5};
int num_measurements = sizeof(measurements) / sizeof(measurements[0]);
// Apply the Kalman filter to each measurement
for (int i = 0; i < num_measurements; i++) {
predict(); // Prediction step
correct(measurements[i]); // Correction step
printf("Measurement: %.2f, Estimated State: %.2f\n", measurements[i], x);
}
return 0;
}