matlab下面的kalman滤波程序(1)
更新时间:2023-11-12 20:06:01 阅读量: 教育文库 文档下载
clear N=200; w(1)=0; w=randn(1,N) x(1)=0; a=1; for k=2:N;
x(k)=a*x(k-1)+w(k-1); end
V=randn(1,N); q1=std(V); Rvv=q1.^2; q2=std(x); Rxx=q2.^2; q3=std(w); Rww=q3.^2; c=0.2; Y=c*x+V; p(1)=0; s(1)=0; for t=2:N;
p1(t)=a.^2*p(t-1)+Rww; b(t)=c*p1(t)/(c.^2*p1(t)+Rvv); s(t)=a*s(t-1)+b(t)*(Y(t)-a*c*s(t-1)); p(t)=p1(t)-c*b(t)*p1(t); end t=1:N;
plot(t,s,'r',t,Y,'g',t,x,'b');
function [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, varargin) % Kalman filter.
% [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, ...) %
% INPUTS:
% y(:,t) - the observation at time t % A - the system matrix % C - the observation matrix % Q - the system covariance % R - the observation covariance
% init_x - the initial state (column) vector % init_V - the initial state covariance %
% OPTIONAL INPUTS (string/value pairs [default in brackets])
% 'model' - model(t)=m means use params from model m at time t [ones(1,T) ] % In this case, all the above matrices take an additional final dimension, % i.e., A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m).
% However, init_x and init_V are independent of model(1). % 'u' - u(:,t) the control signal at time t [ [] ]
% 'B' - B(:,:,m) the input regression matrix for model m %
% OUTPUTS (where X is the hidden state being estimated) % x(:,t) = E[X(:,t) | y(:,1:t)] % V(:,:,t) = Cov[X(:,t) | y(:,1:t)]
% VV(:,:,t) = Cov[X(:,t), X(:,t-1) | y(:,1:t)] t >= 2 % loglik = sum{t=1}^T log P(y(:,t)) %
% If an input signal is specified, we also condition on it: % e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t)]
% If a model sequence is specified, we also condition on it: % e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t), m(1:t)] [os T] = size(y);
ss = size(A,1); % size of state space % set default params model = ones(1,T); u = []; B = []; ndx = []; args = varargin; nargs = length(args); for i=1:2:nargs switch args
case 'model', model = args{i+1}; case 'u', u = args{i+1}; case 'B', B = args{i+1}; case 'ndx', ndx = args{i+1};
otherwise, error(['unrecognized argument ' args]) end
end
x = zeros(ss, T); V = zeros(ss, ss, T); VV = zeros(ss, ss, T); loglik = 0; for t=1:T m = model(t); if t==1
%prevx = init_x(:,m); %prevV = init_V(:,:,m); prevx = init_x; prevV = init_V; initial = 1; else
prevx = x(:,t-1); prevV = V(:,:,t-1); initial = 0; end if isempty(u)
[x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...
kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, 'initial', initial); else
if isempty(ndx)
[x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...
kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, ... 'initial', initial, 'u', u(:,t), 'B', B(:,:,m)); else i = ndx;
% copy over all elements; only some will get updated x(:,t) = prevx; prevP = inv(prevV); prevPsmall = prevP(i,i); prevVsmall = inv(prevPsmall); [x(i,t), smallV, LL, VV(i,i,t)] = ...
kalman_update(A(i,i,m), C(:,i,m), Q(i,i,m), R(:,:,m), y(:,t), prevx(i), prevVsmall, ... 'initial', initial, 'u', u(:,t), 'B', B(i,:,m)); smallP = inv(smallV); prevP(i,i) = smallP;
V(:,:,t) = inv(prevP); end end
loglik = loglik + LL; end
正在阅读:
行政诉讼案例10-14
自然辩证法导言(恩格斯)05-07
发展高速铁路是实现中国可持续发展的序言03-15
五年级数学复习资料整理(一)01-25
2020年市局党组书记组织落实市委巡察反馈意见整改工作报告08-27
最新精编 2016年人教版九年级上语文期末模拟卷(AB卷)03-25
2022年最新作风纪律教育整顿活动工作方案模版范文08-03
第0章 C语言基本语法505-20
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 滤波
- 面的
- 程序
- matlab
- kalman