C#实现一阶卡尔曼滤波算法的示例代码

作者:Lxk- 时间:2022-12-23 05:28:45 

C#实现一阶卡尔曼滤波算法的示例代码


//FilterKalman.cs

namespace FusionFiltering
{

public class FilterKalman
   {
       private double A = 1;
       private double B = 0;
       private double H = 1;

private double R;
       private double Q;

private double cov = double.NaN;
       private double x = double.NaN;

public FilterKalman(double R, double Q, double A, double B, double H)
       {
           this.R = R;  //过程噪声
           this.Q = Q;  //测量噪声

this.A = A;  //状态转移矩阵
           this.B = B;  //控制矩阵  u为控制向量
           this.H = H;  //将估计范围与单位转化为与系统变量(或者说测量值)一致的范围与单位

this.cov = double.NaN;
           this.x = double.NaN; // estimated signal without noise
       }

public FilterKalman(double R, double Q)
       {
           this.R = R;
           this.Q = Q;
       }

public double filter(double measurement, double u)
       {
           if (double.IsNaN(this.x)) {
               this.x = (1 / this.H) * measurement;
               this.cov = (1 / this.H) * this.Q * (1 / this.H);
           } else {
               double predX = (this.A * this.x) + (this.B * u);
               double predCov = ((this.A * this.cov) * this.A) + this.Q;

// Kalman gain
               double K = predCov * this.H * (1 / ((this.H * predCov * this.H) + this.Q));

// Correction
               this.x = predX + K * (measurement - (this.H * predX));
               this.cov = predCov - (K * this.H * predCov);
           }
           return this.x;
       }

public double filter(double measurement)
       {
           double u = 0;
           if (double.IsNaN(this.x)) {
               this.x = (1 / this.H) * measurement;
               this.cov = (1 / this.H) * this.Q * (1 / this.H);
           } else {
               double predX = (this.A * this.x) + (this.B * u);
               double predCov = ((this.A * this.cov) * this.A) + this.R;

// Kalman gain
               double K = predCov * this.H * (1 / ((this.H * predCov * this.H) + this.Q));

// Correction
               this.x = predX + K * (measurement - (this.H * predX));
               this.cov = predCov - (K * this.H * predCov);
           }
           return this.x;
       }

public double lastMeasurement()
       {
           return this.x;
       }

public void setMeasurementNoise(double noise)
       {
           this.Q = noise;
       }

public void setProcessNoise(double noise)
       {
           this.R = noise;
       }
   }
}

//ProgramTestData.cs

using System;
using System.Linq;

namespace FusionFiltering
{
   public class ProgramTest
   {
       /// <summary>
       /// kalman滤波测试1
       /// </summary>
       [System.Diagnostics.Conditional("DEBUG")]
       public static void TestKalmanFilter1()
       {
           Console.WriteLine("FilterKalman Usage");

FilterKalman test = new FilterKalman(0.008, 0.1);
           double[] testData = { 66, 64, 63, 63, 63, 66, 65, 67, 58 };
           foreach (var x in testData) {
               Console.WriteLine("Input data: {0:#,##0.00}, Filtered data:{1:#,##0.000}", x, test.filter(x));
           }
       }

/// <summary>
       /// Example Usage with controlled input
       /// </summary>
       [System.Diagnostics.Conditional("DEBUG")]
       public static void TestKalmanFilterWithControlled()
       {
           Console.WriteLine("FilterKalman Usage with controlled input");

FilterKalman test = new FilterKalman(0.008, 0.1, 1, 1, 1);
           double[] testData = { 66, 64, 63, 63, 63, 66, 65, 67, 58 };
           double u = 0.2;
           foreach (var x in testData) {
               Console.WriteLine("Input data: {0:#,##0.00}, Filtered data:{1:#,##0.000}", x, test.filter(x, u));
           }

}
   }
}

//Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FusionFiltering;
namespace ConsoleApplication1
{
   class Program
   {
       static void Main(string[] args)
       {

ProgramTest.TestKalmanFilter1();
           Console.ReadKey();
           Console.WriteLine();
           ProgramTest.TestKalmanFilterWithControlled();
           Console.ReadKey();

}
}
}

效果:

C#实现一阶卡尔曼滤波算法的示例代码

来源:https://www.cnblogs.com/Lxk0825/p/13899521.html

标签:c#,卡尔曼滤波,算法
0
投稿

猜你喜欢

  • C#操作config文件的具体方法

    2023-09-03 12:18:07
  • java多线程编程之慎重使用volatile关键字

    2022-09-08 00:36:41
  • SpringBoot ApplicationListener事件监听接口使用问题探究

    2023-03-04 22:44:13
  • 详解在spring boot中消息推送系统设计与实现

    2023-05-28 04:17:00
  • JavaWeb利用struts实现文件下载时改变文件名称

    2023-10-24 11:26:53
  • hashCode方法的使用讲解

    2022-11-12 15:29:37
  • 基于spring @Cacheable 注解的spel表达式解析执行逻辑

    2023-07-03 19:46:45
  • Java项目有中多个线程如何查找死锁

    2023-01-06 06:17:35
  • C#中正则表达式(Regex)过滤内容的基本使用方法

    2023-11-26 12:51:00
  • Spring拦截器HandlerInterceptor接口代码解析

    2022-09-05 10:51:04
  • JDK源码中一些实用的“小技巧”总结

    2022-10-21 08:17:50
  • CAD2008+VS2008开发ObjectARX加载失败问题(推荐)

    2023-11-06 00:36:46
  • java高并发锁的3种实现示例代码

    2022-05-23 16:45:47
  • c# 实现语音合成

    2021-06-16 00:17:47
  • Java 发送http请求上传文件功能实例

    2021-07-04 22:30:53
  • 一文详解C# Chart控件

    2023-01-20 03:50:04
  • 面试必时必问的JVM 类加载机制详解

    2022-06-22 20:37:52
  • java反射技术与类使用示例

    2021-09-14 06:34:34
  • C#将布尔类型转换成字节数组的方法

    2023-06-21 15:30:16
  • native.js获取手机硬件基本信息实例代码android版

    2023-10-19 13:33:09
  • asp之家 软件编程 m.aspxhome.com