C#實(shí)現(xiàn)一階卡爾曼濾波算法的示例代碼

//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; //過(guò)程噪聲
this.Q = Q; //測(cè)量噪聲
this.A = A; //狀態(tài)轉(zhuǎn)移矩陣
this.B = B; //控制矩陣 u為控制向量
this.H = H; //將估計(jì)范圍與單位轉(zhuǎn)化為與系統(tǒng)變量(或者說(shuō)測(cè)量值)一致的范圍與單位
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濾波測(cè)試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#實(shí)現(xiàn)一階卡爾曼濾波算法的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#實(shí)現(xiàn)一階卡爾曼濾波算法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于C#實(shí)現(xiàn)手機(jī)號(hào)碼歸屬地接口調(diào)用
這篇文章主要介紹了基于C#實(shí)現(xiàn)手機(jī)號(hào)碼歸屬地接口調(diào)用的相關(guān)資料,需要的朋友可以參考下2016-02-02
Unity 實(shí)現(xiàn)給物體動(dòng)態(tài)添加事件
這篇文章主要介紹了Unity 實(shí)現(xiàn)給物體動(dòng)態(tài)添加事件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
基于數(shù)據(jù)類型轉(zhuǎn)換(裝箱與拆箱)與常量詳解
下面小編就為大家分享一篇基于數(shù)據(jù)類型轉(zhuǎn)換(裝箱與拆箱)與常量詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
C#在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件
這篇文章主要為大家詳細(xì)介紹了C#如何在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下2023-10-10
C#實(shí)現(xiàn)簡(jiǎn)單超市收銀系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單超市收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
C#使用channel實(shí)現(xiàn)Plc異步任務(wù)之間的通信
在C#的并發(fā)編程中,Channel是一種非常強(qiáng)大的數(shù)據(jù)結(jié)構(gòu),用于在生產(chǎn)者和消費(fèi)者之間進(jìn)行通信,本文將給大家介紹C#使用channel實(shí)現(xiàn)Plc異步任務(wù)之間的通信,文中有相關(guān)的代碼示例供大家參考,感興趣的朋友跟著小編一起來(lái)看看吧2024-05-05

