当前位置:网站首页>Kalman filter encapsulation function
Kalman filter encapsulation function
2022-06-12 07:46:00 【Jace Lee】
List of articles
background
I need to write several simulations recently , Local standards are heavily used Kalman wave filtering , So I'll just Kalman The filtering algorithm is encapsulated as a function , The subsequent use can be called directly .
Be careful : The encapsulated function is only the calculation of a single moment , The call needs to be made in the loop body .
function code
%Project: Local filter function ( Whether the input quantity is universal )
%Author: Jace
%Data: 2021/10/25
%==================== The body of the function ====================
function [P,xkf,K]=Lkf(Dim,A,H,Gamma,Q,R,Pp,xkfp,z,B,up)
if nargin==9 % Parameter number judgment
I=eye(Dim);
P_pre=A*Pp*A'+Gamma*Q*Gamma';
K=P_pre*H'/(H*P_pre*H'+R);
xkf=A*xkfp+K*(z-H*A*xkfp);
P=(I-K*H)*P_pre;
elseif nargin==11
I=eye(Dim);
P_pre=A*Pp*A'+Gamma*Q*Gamma';
K=P_pre*H'/(H*P_pre*H'+R);
x_pre=A*xkfp+B*up;
xkf=x_pre+K*(z-H*x_pre);
P=(I-K*H)*P_pre;
else
error(' Insufficient parameters ');
end
end
Calling method
[P,xkf,K]=Lkf(Dim,A,H,Gamma,Q,R,Pp,xkfp,z,B,up)
Corresponding
[ Estimate the covariance at the current time P, Current time estimate xkf, Current time gain matrix K]=Lkf( The state dimension , State transition matrix A, Measurement matrix H, State noise matrix , Gamma( If not, you can write directly 1), State noise covariance Q, Measurement noise covariance R, Estimated covariance at the last moment Pp, Last time estimate xkfp, Measured value at current time z, Control matrix B, Control quantity at the last moment up)
Be careful : Returns... In the result K And... In the input parameters B、up All are optional parameters , In actual use, you can omit .
Call the test function
%Project: Basic 2D Kalman Test functions
%Author: Jace
%Data: 2021/11/02
%==================== Get ready ====================
close all;
clear all;
clc;
%==================== Set global parameters ====================
%-------- Global parameter --------
N=100;% Set the number of sampling points , That is, the duration
%-------- Set dimension --------
Dim_n=2;% The state dimension
Dim_m=2;% Measurement dimension
%-------- System model parameters --------
A=[1.002,0;0,0.998];% State transition matrix
H=[1,0;0,1];% Measurement matrix
Gamma=1;
%-------- Noise related parameters --------
P0=0.01;% Initial state noise covariance
Q=0.01*eye(Dim_n);% Set the system noise
R=0.1*eye(Dim_m);% Set observation noise
w=sqrt(Q)*randn(Dim_n,N);
v=sqrt(R)*randn(Dim_m,N);
%==================== Allocate space ========================
%-------- system parameter --------
x=zeros(Dim_n,N);
z=zeros(Dim_m,N);% Measured value
%--------Kalman Process parameters --------
p=zeros(Dim_n,Dim_n,N);
xkf=zeros(Dim_n,N);% Estimated state
%==================== initialization ====================
%-------- System parameter initialization --------
x(:,1)=[10+P0*randn(1);20+P0*randn(1)];
z(:,1)=H*x(:,1)+v(:,1);
%-------- Estimate parameter initialization --------
p(:,:,1)=0.1*eye(Dim_n);
xkf(:,1)=x(:,1);
%-------- Specific matrix initialization --------
In=eye(Dim_n);
%==================== iterative process ====================
for k=2:N
x(:,k)=A*x(:,k-1)+w(:,k);
z(:,k)=H*x(:,k)+v(:,k);
%==================== standard Kalman The process ====================
[p(:,:,k),xkf(:,k)]=Lkf(2,A,H,Gamma,Q,R,p(:,:,k-1),xkf(:,k-1),z(:,k));
end
%====================MSE Calculation ====================
Step=10;
[MSE]=MSE(Dim_n,Step,N,xkf,x);
%==================== mapping ====================
% Local filter 1
figure;
hold on,box on;
plot(x(1,:),'-k.');
plot(xkf(1,:),'-r.');
legend(' The first 1 The status value ',' The first 1 State quantity Kalman Estimated value ');
xlabel(' Sampling time ');ylabel(' Location ');
title(' state 1 track ');
% Local filter 2
figure;
hold on,box on;
plot(x(2,:),'-k.');
plot(xkf(2,:),'-r.');
legend(' The first 2 The status value ',' The first 2 State quantity Kalman Estimated value ');
xlabel(' Sampling time ');ylabel(' Location ');
title(' state 2 track ');
边栏推荐
- LeetCode笔记:Biweekly Contest 79
- Topic 1 Single_Cell_analysis(3)
- Topic 1 Single_ Cell_ analysis(3)
- Voice assistant -- vertical class perpetual motion machine -- automated iteration framework
- Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water
- R language uses rstudio to save visualization results as PDF files (export--save as PDF)
- AcWing——4268. Sexy element
- 2022 G3 boiler water treatment recurrent training question bank and answers
- Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy
- R language uses neuralnet package to build neural network regression model (feedforward neural network regression model) and calculate MSE value (mean square error) of the model on the test set
猜你喜欢

Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine

Modelarts training task 1

Federated meta learning with fast convergence and effective communication

BI技巧丨当月期初

Unity uses shaders to highlight the edges of ugu I pictures

Machine learning from entry to re entry: re understanding of SVM

Multithread decompression of tar

Summary of machine learning + pattern recognition learning (I) -- k-nearest neighbor method

面试计算机网络-传输层

二、八、十、十六进制相互转换
随机推荐
The R language catools package divides the data, the scale function scales the data, the KNN function of the class package constructs a k-nearest neighbor classifier, and compares the model accuracy u
Voice assistant - Multi round conversation (process implementation)
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图、使用dot.col参数设置不同分组数据点的颜色
Xshell installation
2022 simulated test platform operation of hoisting machinery command test questions
经典论文回顾:Palette-based Photo Recoloring
Voice assistant - future trends
Voice assistant -- Qu -- query error correction and rewriting
Summary of machine learning + pattern recognition learning (IV) -- decision tree
‘CMRESHandler‘ object has no attribute ‘_ timer‘,socket. gaierror: [Errno 8] nodename nor servname pro
Preliminary knowledge next New concepts such as isr/rsc/edge runtime/streaming in JS
Topic 1 Single_Cell_analysis(2)
Voice assistant -- vertical class perpetual motion machine -- automated iteration framework
Voice assistant - Measurement Indicators
VS2019 MFC IP Address Control 控件繼承CIPAddressCtrl類重繪
20220526 yolov1-v5
Summary of machine learning + pattern recognition learning (VI) -- feature selection and feature extraction
[yolo-v5 learning notes]
R语言rnorm函数生成正太分布数据、使用epiDisplay包的summ函数计算向量数据的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
石油储运生产 2D 可视化,组态应用赋能工业智慧发展