ALGO_Kalman_ATY.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @file ALGO_Kalman_ATY.c
  3. *
  4. * @param Project ALGO_Algorithm_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of Kalman
  20. *
  21. * @version
  22. * - 1_01_220605 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_230108 > ATY
  25. * -# Finish and test 2D
  26. * @see https://www.kalmanfilter.net/
  27. * @see https://github.com/xiahouzuoxin/kalman_filter
  28. * @see https://blog.csdn.net/whereismatrix/article/details/79920748
  29. ********************************************************************************
  30. */
  31. #ifndef __ALGO_Kalman_ATY_C
  32. #define __ALGO_Kalman_ATY_C
  33. #include "ALGO_Kalman_ATY.h"
  34. /******************************* For user *************************************/
  35. /******************************************************************************/
  36. #if !Kalman1D_TYPE // use below instead
  37. /**
  38. * @brief Kalman filter one-dimensional with A = H = 1
  39. * @param kfp kalman struct init value
  40. * @param input filter data(series)
  41. * @return data after filter
  42. * @note Origin equations
  43. * - State Equation
  44. * x(k) = A * x(k-1) + B * u(k) + w(k-1)
  45. * (if no controlled quantity then: B * u(k)=0, like detect Tem/Hum)
  46. * - Observations Equation
  47. * z(k) = H * x(k) + y(k)
  48. * - Prediction Equations
  49. * x(k|k-1) = A * x(k-1|k-1) + B * u(k)
  50. * P(k|k-1) = A * P(k-1|k-1) * A^T + Q
  51. * - Correction Equations
  52. * K(k) = P(k|k-1) * H^T * (H * P(k|k-1) * H^T + R)^(-1)
  53. * x(k|k) = x(k|k-1) + K(k) * (z(k) - H * x(k|k-1))
  54. * P(k|k) = (I - K(k) * H) * P(k|k-1)
  55. * @note Equations Note:
  56. * x and P only need to be assigned initial values, each iteration will produce new values; K does not need to be assigned an initial value
  57. * Q and R assignments can also be changed in subsequent iterations
  58. * The initial values of x and P can be arbitrarily set, and the powerful Kalman filter will immediately erase the irrdibility
  59. * But notice that the initial value of P cannot be 0, otherwise the filter will think that there is no error
  60. * The larger R is, the smoother the curve is, but the filter becomes insensitive and lags exist
  61. * (The value of Q and R can also be time-varying, can recognize jump changes, can be adaptive)
  62. * Q: process noise, Q increases, dynamic response becomes faster, convergence stability becomes worse
  63. * R: measurement noise, R increases, the dynamic response becomes slower, and the convergence stability becomes better
  64. */
  65. float ALGO_KalmanFilter1D(ALGO_Kalman1D_S* kfp, float input)
  66. {
  67. // Prediction covariance:
  68. // estimated system covariance at time k
  69. // = system covariance at time k-1 + process noise covariance
  70. kfp->P = kfp->L_P + kfp->Q;
  71. // Kalman gain:
  72. // Kalman gain
  73. // = system estimated covariance at time k
  74. kfp->G = kfp->P / (kfp->P + kfp->R);
  75. // Update the optimal value:
  76. // Optimal value of state variable at time k
  77. // = predicted value of state variable + Kalman gain
  78. // * (Measured value - predicted value of state variable)
  79. // / (system estimated covariance at time k + observation noise covariance)
  80. kfp->O = kfp->O + kfp->G * (input - kfp->O);
  81. // Update the covariance:
  82. // this time the system covariance is paid to kfp->LastP for the next operation
  83. kfp->L_P = (1 - kfp->G) * kfp->P;
  84. return kfp->O;
  85. }
  86. #else
  87. /**
  88. * @brief Kalman filter one-dimensional
  89. * @param kfp kalman struct init value
  90. * @param input filter data(series)
  91. * @return data after filter
  92. * @note Origin equations
  93. * - State Equation
  94. * x(k) = A * x(k-1) + B * u(k) + w(k-1)
  95. * (if no controlled quantity then: B * u(k)=0, like detect Tem/Hum)
  96. * - Observations Equation
  97. * z(k) = H * x(k) + y(k)
  98. * - Prediction Equations
  99. * x(k|k-1) = A * x(k-1|k-1) + B * u(k)
  100. * P(k|k-1) = A * P(k-1|k-1) * A^T + Q
  101. * - Correction Equations
  102. * K(k) = P(k|k-1) * H^T * (H * P(k|k-1) * H^T + R)^(-1)
  103. * x(k|k) = x(k|k-1) + K(k) * (z(k) - H * x(k|k-1))
  104. * P(k|k) = (I - K(k) * H) * P(k|k-1)
  105. * @note Equations Note:
  106. * x and P only need to be assigned initial values, each iteration will produce new values; K does not need to be assigned an initial value
  107. * Q and R assignments can also be changed in subsequent iterations
  108. * The initial values of x and P can be arbitrarily set, and the powerful Kalman filter will immediately erase the irrdibility
  109. * But notice that the initial value of P cannot be 0, otherwise the filter will think that there is no error
  110. * The larger R is, the smoother the curve is, but the filter becomes insensitive and lags exist
  111. * (The value of Q and R can also be time-varying, can recognize jump changes, can be adaptive)
  112. * Q: process noise, Q increases, dynamic response becomes faster, convergence stability becomes worse
  113. * R: measurement noise, R increases, the dynamic response becomes slower, and the convergence stability becomes better
  114. * @note A = H = 1 in always use
  115. */
  116. float ALGO_KalmanFilter1D(ALGO_Kalman1D_S* kfp, float input)
  117. {
  118. // Prediction covariance:
  119. // estimated system covariance at time k
  120. // = system covariance at time k-1 + process noise covariance
  121. kfp->X = kfp->A * kfp->X;
  122. kfp->P = kfp->A * kfp->A * kfp->P + kfp->Q;
  123. // Kalman gain:
  124. // Kalman gain
  125. // = system estimated covariance at time k
  126. kfp->G = kfp->P * kfp->H / (kfp->P * kfp->H * kfp->H + kfp->R);
  127. // Update the optimal value:
  128. // Optimal value of state variable at time k
  129. // = predicted value of state variable + Kalman gain
  130. // * (Measured value - predicted value of state variable)
  131. // / (system estimated covariance at time k + observation noise covariance)
  132. kfp->X = kfp->X + kfp->G * (input - kfp->H * kfp->X);
  133. // Update the covariance:
  134. // this time the system covariance is paid to kfp->LastP for the next operation
  135. kfp->P = (1 - kfp->G * kfp->H) * kfp->P;
  136. return kfp->X;
  137. }
  138. #endif /* 01 */
  139. #ifdef Kalman_2D
  140. /**
  141. * @brief Kalman filter two-dimensional
  142. * @param kfp kalman struct init value
  143. * @param input filter data(series)
  144. * @return data after filter
  145. * @note allways in default:
  146. * A = {{1, 0.1}, {0, 1}};
  147. * H = {1, 0};
  148. */
  149. float ALGO_KalmanFilter2D(ALGO_Kalman2D_S* kfp, float input)
  150. {
  151. float temp[3] = {0.0f};
  152. /* Step1: Predict */
  153. kfp->X[0] = kfp->A[0][0] * kfp->X[0] + kfp->A[0][1] * kfp->X[1];
  154. kfp->X[1] = kfp->A[1][0] * kfp->X[0] + kfp->A[1][1] * kfp->X[1];
  155. /* P(n|n-1)=A^2*P(n-1|n-1)+Q */
  156. kfp->P[0][0] = kfp->A[0][0] * kfp->P[0][0] + kfp->A[0][1] * kfp->P[1][0] + kfp->Q[0][0];
  157. kfp->P[0][1] = kfp->A[0][0] * kfp->P[0][1] + kfp->A[1][1] * kfp->P[1][1] + kfp->Q[0][1];
  158. kfp->P[1][0] = kfp->A[1][0] * kfp->P[0][0] + kfp->A[0][1] * kfp->P[1][0] + kfp->Q[1][0];
  159. // kfp->P[0][1] = kfp->A[0][0] * kfp->P[0][1] + kfp->A[1][1] * kfp->P[1][1];
  160. // kfp->P[1][0] = kfp->A[1][0] * kfp->P[0][0] + kfp->A[0][1] * kfp->P[1][0];
  161. kfp->P[1][1] = kfp->A[1][0] * kfp->P[0][1] + kfp->A[1][1] * kfp->P[1][1] + kfp->Q[1][1];
  162. /* Step2: Measurement */
  163. /* G = P * H^T * [R + H * P * H^T]^(-1), H^T means transpose. */
  164. temp[0] = kfp->P[0][0] * kfp->H[0] + kfp->P[0][1] * kfp->H[1];
  165. temp[1] = kfp->P[1][0] * kfp->H[0] + kfp->P[1][1] * kfp->H[1];
  166. temp[2] = kfp->R + kfp->H[0] * temp[0] + kfp->H[1] * temp[1];
  167. kfp->G[0] = temp[0] / temp[2];
  168. kfp->G[1] = temp[1] / temp[2];
  169. /* x(n|n) = x(n|n-1) + G(n) * [input - H(n)*x(n|n-1)]*/
  170. temp[2] = kfp->H[0] * kfp->X[0] + kfp->H[1] * kfp->X[1];
  171. kfp->X[0] = kfp->X[0] + kfp->G[0] * (input - temp[2]);
  172. kfp->X[1] = kfp->X[1] + kfp->G[1] * (input - temp[2]);
  173. /* Update @P: P(n|n) = [I - G * H] * P(n|n-1) */
  174. kfp->P[0][0] = (1 - kfp->G[0] * kfp->H[0]) * kfp->P[0][0];
  175. kfp->P[0][1] = (1 - kfp->G[0] * kfp->H[1]) * kfp->P[0][1];
  176. kfp->P[1][0] = (1 - kfp->G[1] * kfp->H[0]) * kfp->P[1][0];
  177. kfp->P[1][1] = (1 - kfp->G[1] * kfp->H[1]) * kfp->P[1][1];
  178. return kfp->X[0];
  179. }
  180. #endif /* Kalman_2D */
  181. #ifdef __DEBUG_ALGO_Kalman_ATY
  182. #if !Kalman1D_TYPE
  183. ALGO_Kalman1D_S ALGO_kfp1D = {1, 0, 0, 0, 1e-9, 1e-6};
  184. #else
  185. ALGO_Kalman1D_S ALGO_kfp1D = {0, 0, 1, 1, 1, 1e-9, 1e-6};
  186. #endif /* 01 */
  187. ALGO_Kalman2D_S ALGO_kfp2D =
  188. {
  189. {0, 0},
  190. {0, 0},
  191. {{1, 0.1}, {0, 1}},
  192. {1, 0},
  193. {{1, 0}, {0, 1}},
  194. {{1e-9, 0}, {0, 1e-9}},
  195. 10e-6
  196. };
  197. uint16_t tempSaveOW = 0;
  198. float tempSaveO = 0;
  199. float tempSaveK = 0;
  200. float tempSaveK2 = 0;
  201. void ALGO_Kalman_Test(void)
  202. {
  203. uint16_t i = 0;
  204. uint8_t testNum = 3;
  205. if(testNum == 1)
  206. {
  207. ALGO_kfp1D.Q = 1e-6;
  208. ALGO_kfp1D.R = 1e-4;
  209. for(i = 0; i < 120; i++)
  210. {
  211. tempSaveO = testSignalSigned[i];
  212. tempSaveK = ALGO_KalmanFilter1D(&ALGO_kfp1D, testSignalSigned[i]);
  213. tempSaveK2 = ALGO_KalmanFilter2D(&ALGO_kfp2D, testSignalSigned[i]);
  214. // printf("$");
  215. // printf("%f ", testSignalSigned[i]);
  216. // printf("%f", ALGO_KalmanFilter1D(&ALGO_kfp, testSignalSigned[i]));
  217. // printf(";");
  218. }
  219. }
  220. else if(testNum == 2)
  221. {
  222. ALGO_kfp1D.Q = 1e-6;
  223. ALGO_kfp1D.R = 1e-4;
  224. for(i = 0; i < 120; i++)
  225. {
  226. tempSaveO = testSignalUnsigned[i];
  227. tempSaveK = ALGO_KalmanFilter1D(&ALGO_kfp1D, testSignalUnsigned[i]);
  228. tempSaveK2 = ALGO_KalmanFilter2D(&ALGO_kfp2D, testSignalUnsigned[i]);
  229. // printf("$");
  230. // printf("%f ", testSignalUnsigned[i]);
  231. // printf("%f", ALGO_KalmanFilter1D(&ALGO_kfp, testSignalUnsigned[i]));
  232. // printf(";");
  233. }
  234. }
  235. else if(testNum == 3)
  236. {
  237. ALGO_kfp1D.Q = 1e-9;
  238. ALGO_kfp1D.R = 1e-6;
  239. for(i = 0; i < 3000; i++)
  240. {
  241. tempSaveO = (float)testSignalUint16[i];
  242. tempSaveK = ALGO_KalmanFilter1D(&ALGO_kfp1D, testSignalUint16[i]);
  243. tempSaveK2 = ALGO_KalmanFilter2D(&ALGO_kfp2D, testSignalUint16[i]);
  244. // printf("$");
  245. // printf("%d ", testSignalUint16[i]);
  246. // printf("%d", (uint16_t)ALGO_KalmanFilter1D(&ALGO_kfp, (float)testSignalUint16[i]));
  247. // printf(";");
  248. }
  249. }
  250. else if(testNum == 4)
  251. {
  252. ALGO_kfp1D.Q = 1e-9;
  253. ALGO_kfp1D.R = 1e-6;
  254. // ALGO_kfp2D.X[0] = testSignalFloat[0];
  255. // ALGO_kfp2D.X[1] = testSignalFloat[1] - testSignalFloat[0];
  256. for(i = 0; i < 620; i++)
  257. {
  258. tempSaveO = testSignalFloat[i];
  259. tempSaveK = ALGO_KalmanFilter1D(&ALGO_kfp1D, testSignalFloat[i]);
  260. tempSaveK2 = ALGO_KalmanFilter2D(&ALGO_kfp2D, testSignalFloat[i]);
  261. // printf("$");
  262. // printf("%d ", testSignalUint16[i]);
  263. // printf("%d", (uint16_t)ALGO_KalmanFilter1D(&ALGO_kfp, (float)testSignalUint16[i]));
  264. // printf(";");
  265. }
  266. }
  267. }
  268. #endif /* __DEBUG_ALGO_Kalman_ATY */
  269. #endif /* __ALGO_Kalman_ATY_C */
  270. /******************************** End Of File *********************************/