找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2509|回复: 3
收起左侧

C语言编写卡尔曼滤波算法的C语言程序 大家自行移植到各平台

  [复制链接]
ID:776760 发表于 2021-6-30 14:40 | 显示全部楼层 |阅读模式
  1. /*  kalman.c

  2.     This file contains the code for a kalman filter, an
  3.     extended kalman filter, and an iterated extended kalman filter.

  4.     For ready extensibility, the apply_measurement() and apply_system()
  5.     functions are located in a separate file: kalman_cam.c is an example.

  6.     It uses the matmath functions provided in an accompanying file
  7.     to perform matrix and quaternion manipulation.


  8.     J. Watlington, 11/15/95

  9.     Modified:
  10.     11/30/95  wad  The extended kalman filter section seems to be
  11.                    working now.
  12. */

  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include "kalman.h"

  17. #define ITERATION_THRESHOLD      2.0
  18. #define ITERATION_DIVERGENCE     20

  19. /*  The following are the global variables of the Kalman filters,
  20.     used to point to data structures used throughout.     */

  21. static m_elem  *state_pre;         /* ptr to apriori state vectors, x(-)     */
  22. static m_elem  *state_post;        /* ptr to aposteriori state vectors, x(+) */

  23. static m_elem  *iter_state0;
  24. static m_elem  *iter_state1;

  25. static m_elem  **cov_pre;          /* ptr to apriori covariance matrix, P(-) */
  26. static m_elem  **cov_post;         /* ptr to apriori covariance matrix, P(-) */
  27. static m_elem  **sys_noise_cov;    /* system noise covariance matrix (GQGt)  */
  28. static m_elem  **mea_noise_cov;    /* measurement noise variance vector (R)  */

  29. static m_elem  **sys_transfer;     /* system transfer function (Phi)    */
  30. static m_elem  **mea_transfer;     /* measurement transfer function (H) */

  31. static m_elem  **kalman_gain;      /* The Kalman Gain matrix (K) */

  32. int            global_step = 0;    /* the current step number (k) */
  33. int            measurement_size;   /* number of elems in measurement */
  34. int            state_size;         /* number of elements in state    */

  35. /*  Temporary variables, declared statically to avoid lots of run-time
  36.     memory allocation.      */

  37. static m_elem  *z_estimate;        /* a measurement_size x 1 vector */
  38. static m_elem  **temp_state_state; /* a state_size x state_size matrix */
  39. static m_elem  **temp_meas_state;  /* a measurement_size x state_size matrix */
  40. static m_elem  **temp_meas_meas;   /* a measurement_size squared matrix */
  41. static m_elem  **temp_meas_2;      /* another one ! */

  42. /*  Prototypes of internal functions  */

  43. static void alloc_globals( int num_state,
  44.                           int num_measurement );
  45. static void update_system( m_elem *z, m_elem *x_minus,
  46.                          m_elem **kalman_gain, m_elem *x_plus );
  47. static void estimate_prob( m_elem **P_post, m_elem **Phi, m_elem **GQGt,
  48.                           m_elem **P_pre );
  49. static void update_prob( m_elem **P_pre, m_elem **R, m_elem **H,
  50.                         m_elem **P_post, m_elem **K );
  51. static void take_inverse( m_elem **in, m_elem **out, int n );
  52. static m_elem calc_state_change( m_elem *a, m_elem *b );


  53. /******************************************************************

  54.   Linear Kalman Filtering

  55.   kalman_init()
  56.   This function initializes the kalman filter.  Note that for a
  57.   straight-forward (linear) Kalman filter, this is the only place that
  58.   K and P are computed...      */

  59. void kalman_init( m_elem **GQGt, m_elem **Phi, m_elem **H, m_elem **R,
  60.                 m_elem **P, m_elem *x, int num_state, int num_measurement )
  61. {
  62.   alloc_globals( num_state, num_measurement );

  63.   /*  Init the global variables using the arguments.  */

  64.   vec_copy( x, state_post, state_size );
  65.   mat_copy( P, cov_post, state_size, state_size );

  66.   sys_noise_cov = GQGt;
  67.   mea_noise_cov = R;

  68.   sys_transfer = Phi;
  69.   mea_transfer = H;

  70.   /*****************  Gain Loop  *****************/

  71.   estimate_prob( cov_post, sys_transfer, sys_noise_cov, cov_pre );
  72.   update_prob( cov_pre, mea_noise_cov, mea_transfer, cov_post, kalman_gain );
  73. }


  74. /*  kalman_step()
  75.     This function takes a set of measurements, and performs a single
  76.     recursion of the straight-forward kalman filter.
  77. */

  78. void kalman_step( m_elem *z_in )
  79. {
  80.   /**************  Estimation Loop  ***************/

  81.   apply_system( state_post, state_pre );
  82.   update_system( z_in, state_pre, kalman_gain, state_post );

  83.   global_step++;
  84. }

  85. /*  kalman_get_state
  86.     This function returns a pointer to the current estimate (a posteriori)
  87.     of the system state.         */

  88. m_elem *kalman_get_state( void )
  89. {
  90.   return( state_post );
  91. }

  92. /******************************************************************

  93.   Non-linear Kalman Filtering

  94.   extended_kalman_init()
  95.   This function initializes the extended kalman filter.
  96. */

  97. void extended_kalman_init( m_elem **GQGt, m_elem **R, m_elem **P, m_elem *x,
  98.                           int num_state, int num_measurement )
  99. {
  100. #ifdef PRINT_DEBUG
  101.   printf( "ekf: Initializing filter\n" );
  102. #endif

  103.   alloc_globals( num_state, num_measurement );

  104.   sys_transfer = matrix( 1, num_state, 1, num_state );
  105.   mea_transfer = matrix( 1, num_measurement, 1, num_state );

  106.   /*  Init the global variables using the arguments.  */

  107.   vec_copy( x, state_post, state_size );
  108.   vec_copy( x, state_pre, state_size );
  109.   mat_copy( P, cov_post, state_size, state_size );
  110.   mat_copy( P, cov_pre, state_size, state_size );

  111.   sys_noise_cov = GQGt;
  112.   mea_noise_cov = R;
  113. }


  114. /*  extended_kalman_step()
  115.     This function takes a set of measurements, and performs a single
  116.     recursion of the extended kalman filter.
  117. */

  118. void extended_kalman_step( m_elem *z_in )
  119. {
  120. #ifdef PRINT_DEBUG
  121.   printf( "ekf: step %d\n", global_step );
  122. #endif
  123.   /*****************  Gain Loop  *****************
  124.     First, linearize locally, then do normal gain loop    */

  125.   generate_system_transfer( state_pre, sys_transfer );
  126.   generate_measurement_transfer( state_pre, mea_transfer );

  127.   estimate_prob( cov_post, sys_transfer, sys_noise_cov, cov_pre );
  128.   update_prob( cov_pre, mea_noise_cov, mea_transfer, cov_post, kalman_gain );

  129.   /**************  Estimation Loop  ***************/

  130.   apply_system( state_post, state_pre );
  131.   update_system( z_in, state_pre, kalman_gain, state_post );

  132.   global_step++;
  133. }


  134. /* iter_ext_kalman_init()
  135.    This function initializes the iterated extended kalman filter
  136. */

  137. void iter_ext_kalman_init( m_elem **GQGt, m_elem **R, m_elem **P, m_elem *x,
  138.                           int num_state, int num_measurement )
  139. {
  140. #ifdef PRINT_DEBUG
  141.   printf( "iekf: Initializing filter\n" );
  142. #endif

  143.   alloc_globals( num_state, num_measurement );

  144.   iter_state0  = vector( 1, num_state );
  145.   iter_state1  = vector( 1, num_state );
  146.   sys_transfer = matrix( 1, num_state, 1, num_state );
  147.   mea_transfer = matrix( 1, num_measurement, 1, num_state );

  148.   /*  Init the global variables using the arguments.  */

  149.   vec_copy( x, state_post, state_size );
  150.   vec_copy( x, state_pre, state_size );
  151.   mat_copy( P, cov_post, state_size, state_size );
  152.   mat_copy( P, cov_pre, state_size, state_size );

  153.   sys_noise_cov = GQGt;
  154.   mea_noise_cov = R;
  155. }

  156. /*  iter_ext_kalman_step()
  157.     This function takes a set of measurements, and iterates over a single
  158.     recursion of the extended kalman filter.
  159. */

  160. void iter_ext_kalman_step( m_elem *z_in )
  161. {
  162.   int     iteration = 1;
  163.   m_elem  est_change;
  164.   m_elem  *prev_state;
  165.   m_elem  *new_state;
  166.   m_elem  *temp;

  167.   generate_system_transfer( state_pre, sys_transfer );
  168.   estimate_prob( cov_post, sys_transfer, sys_noise_cov, cov_pre );
  169.   apply_system( state_post, state_pre );

  170.   /*  Now iterate, updating the probability and the system model
  171.       until no change is noticed between iteration steps      */

  172.   prev_state = iter_state0;
  173.   new_state  = iter_state1;

  174.   generate_measurement_transfer( state_pre, mea_transfer );
  175.   update_prob( cov_pre, mea_noise_cov, mea_transfer,
  176.               cov_post, kalman_gain );
  177.   update_system( z_in, state_pre, kalman_gain, prev_state );
  178.   est_change = calc_state_change( state_pre, prev_state );

  179.   while( (est_change < ITERATION_THRESHOLD) &&

复制代码
51hei.png

全部代码51hei下载地址:
C语言卡尔曼滤波.zip (11.45 KB, 下载次数: 56)

评分

参与人数 1黑币 +30 收起 理由
admin + 30 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:101990 发表于 2021-7-26 22:00 | 显示全部楼层
学习学习啦!!效果不知道如何呢!收藏啦~~~
回复

使用道具 举报

ID:258676 发表于 2021-8-25 16:23 | 显示全部楼层
好东西 用在哪里?
回复

使用道具 举报

ID:481966 发表于 2021-9-5 22:07 | 显示全部楼层
好东西,学习了,有没有c语言写的低通,带通滤波器呢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表