function [] = demo_KF2(dim_s,dim_o,dim_plot,tau,scale_W,scale_V,scale_Y,ok_load,plot_N), %%%%%%%%%%%%%%%%%%%%%%%% kalman filter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% KalmanFilter(dim_s,dim_o,dim_plot,tau,scale_W, %% scale_V,scale_Y,ok_load,plot_N) %% %% input parameters: %% ------------------ %% dim_s = dimension of the state (X) [def:3] %% dim_o = dimension of the observation (Y) [def:3] %% dim_plot = dimension you may want to analyze [def:1] %% tau = number of observations [def:50] %% scale_W = magnitude of the noise attached to the state [def:5] %% scale_V = magnitude of the noise attached to the the obs. [def:1] %% scale_Y = scale parameter for B [def:1] %% ok_load = 1 (0) -> load data from file (random data) [def:0] %% plot_N = 1 (0) -> 3D plot (2D plot) [def:0] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%% by S.Savarese - M.Welling - May 2000 %%%%%%%%%%%%% Vision lab - Caltech %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% variables: %% ---------- %% X = state vector (dim_s x N_istant ) %% Y = observation vector (dim_o x N_istant x N_obs ) %% W = noise attached to state (dim_s x 1) %% V = noise attached to observation (dim_o x 1) %% N_obs = number of observed sequences %% N_istant = max number of istant (=tau=number of obs.) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%% initialization close all; colordef black; if (nargin==0) dim_s= 3; dim_o = 3; dim_plot =1; scale_V = 10; scale_W = 1; tau = 100; ok_load = 0; plot_N=0; scale_Y=1; end; N_count=50; %N_istant=tau; N_obs=10; %% number of observed sequences obs_jj =1; %% obs. seq. we deal with N_istant =tau; %% max number of istant %dim_s=3; %% dimension of the state vector (X) %dim_o=3; %% dimension of the observation vector (Y) %scale_V=10; %scale_W=5; %scale_V=1; %scale_W=1; %scale_Y=1; %%%%%%%%%% load data if (ok_load==1), load KFdata, Y_copy=X; dim_s=size(Y_copy,1); dim_o=size(Y_copy,1); N_istant=size(Y_copy,2) clear X; end; %A=[1.2 1;0 1.3]; %% transformation from previous to current state A=randCovariance(dim_s,1); [U S V]=svd(A); A=U*V'; A=eye(dim_s); %B=scale_Y*randn(dim_o,dim_s); %% transformation from state to observation B=eye(dim_o); mu_W = zeros(dim_s,1); mu_V = zeros(dim_o,1); cov_W = randCovariance(dim_s,scale_W); cov_V = randCovariance(dim_o,scale_V); X=zeros(dim_s,N_istant); Y=zeros(dim_o,N_istant,N_obs); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % generate synthetic data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% fprintf('Generating data...\n'); mu_X = randMean(dim_s,1); cov_X = randCovariance(dim_s,1); X1 = randvec(1,mu_X,cov_X); X(:,1)=X1; %if (ok_load==1), % Y=Y_copy; for i=1:N_istant, W=randvec(1,mu_W,cov_W); X(:,i+1) = A*X(:,i) + W; for j=1:N_obs, V=randvec(1,mu_V,cov_V); Y(:,i,j) = B*X(:,i) + V; end; end; if (ok_load==1), Y=Y_copy; end; %figure(1); %hold on; %plot(X(1,1:N_istant),X(2,1:N_istant),'c+'); %plot(Y(1,:,1),Y(2,:,1),'r+'); %plot(X(1,1),X(2,1),'b+'); %axis([-20 20 -20 20]); %title('Initial state (blu dot); States (cyan dots);Obs. (red dots);KF (green line); K smoother (magenta line)'); %hold off; if (dim_s==2)&(dim_o==2), figure(2); hold on; plot(X(1,1:N_istant),X(2,1:N_istant),'c+'); plot(Y(1,:,1),Y(2,:,1),'r+'); plot(X(1,1),X(2,1),'b+'); %axis([-20 20 -20 20]); title('States=cyan dots; Observations 2 = red dots'); drawnow; hold off; end; %%%%%%%%%%%%%%%%%%%%%%%%%%%5 cov_w = eye(dim_s); mu_Xo = mu_X; cov_Xo = cov_X; cov_Wo = cov_W; cov_Vo = cov_V; Ao = A; Bo = B; cov_W=randCovariance(dim_s,1); A=randCovariance(dim_s,1); B=randCovariance(dim_o,1); cov_V=randCovariance(dim_o,1); mu_X=randMean(dim_s,1); cov_X=randCovariance(dim_s,1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Kalman filtering over the computed synthetic data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % mu_X_t_tp mean of X at istant t with t-1 observations % mu_X_t_t mean of X at istant t with t observations % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% count=0; while (count