?? bootstrap.m
字號:
function [X,q] = bootstrap(actualX,Z,R,Q,initVar,numSamples);%function:加入新函數
% PURPOSE : This m file performs the bootstrap algorithm (a.k.a. SIR,
% particle filter, etc.) for the model specified in the
% file sirdemo1.m.
% INPUTS : - actualx = The true hidden state.
% - y = The observation.
% - R = The measurement noise variance parameter.
% - Q = The process noise variance parameter.
% - initVar = The initial variance of the state estimate.
% - numSamples = The number of samples.
% OUTPUTS : - x = The estimated state samples.
% - q = The normalised importance ratios.
% AUTHOR : Nando de Freitas - Thanks for the acknowledgement :-)
% DATE : 08-09-98
if nargin < 6, error('Not enough input arguments.'); end
[rows,cols,page] = size(actualX); % rows = Max number of time steps.4*1*N
S = numSamples; % Number of samples;500
Nstd = 2; % No of standard deviations for error bars;
X=zeros(S,rows,page); %zeros:構成全0矩陣,S行,rows列500*4*50
XU=zeros(S,rows,page);
q=zeros(S,rows,page);
% SAMPLE FROM THE PRIOR:
% =====================
X(:,:,1) = randn(S,4)*sqrt(initVar); %x(:,1):?;sqrt:對矩陣的每一個元素分別開方
for i=1:4
mean(X(:,i,1)); %mean:求矩陣各列平均值
cov(X(:,i,1)); %cov:協方差矩陣 ?
end
% figure(1)%figure:創建圖形窗
% clf; %clf:清除當前圖形窗
% subplot(221)
% plot(actualx) %繪制二維坐標系
% ylabel('State x','fontsize',15); %Y軸標注(適用于三維圖形)
% xlabel('Time','fontsize',15);
% UPDATE AND PREDICTION STAGES:
% ============================
for t=1:page-1,
% figure(1) %figure(i):打開第i個圖形
% subplot(222) %在平鋪位置建立圖形軸系 subplot(n,m,p):將圖形窗口分為n*m個子圖,在第p個子圖處繪制圖形
% plot(y) %plot(y):繪制線性直角坐標的二維圖,以y中元素的下標作為X坐標,y中元素的值作為Y坐標 ,其中y是一個數組
% ylabel('Output y','fontsize',15);
% xlabel('Time','fontsize',15);
% hold on %保持
% plot(t*ones(1,49),[-19:1:29],'r'); % r代表紅色
% hold off %解除保持狀態
% subplot(223)
% hold on
% plot(t,mean(x(1:S,t,1)),'ro',t,actualx(t,1),'go'); % 繪制兩條圖線
% hold on
% errorbar(t,mean(x(1:S,t,1)),Nstd*std(x(1:S,t,1)),Nstd*std(x(1:S,t,1)),'k')
% legend('Posterior mean estimate','True value');
% ylabel('Sequential state estimate','fontsize',15)
% xlabel('Time','fontsize',15)
XU(:,:,t) = predictstates(X(:,:,t),t,Q); %自建子函數
q(:,:,t+1) = importanceweights(XU(:,:,t),Z(2,1,t+1),R); %自建子函數
X(:,:,t+1) = updatestates(XU(:,:,t),q(:,:,t+1));
end;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -