0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 print_figures = 1;
0016 save_figures = 0;
0017
0018
0019 F = [0 0 1 0;
0020 0 0 0 1;
0021 0 0 0 0;
0022 0 0 0 0];
0023 m = size(F,2);
0024
0025
0026 L = [0 0;
0027 0 0;
0028 1 0;
0029 0 1];
0030
0031
0032 dt = 0.1;
0033
0034
0035 q = .1;
0036 Qc = diag([q q]);
0037
0038
0039 [A,Q] = lti_disc(F,L,Qc,dt);
0040
0041
0042 H = [1 0 0 0;
0043 0 1 0 0];
0044
0045
0046 r1 = 0.05;
0047 r2 = 0.05;
0048 R = diag([r1 r1]);
0049
0050
0051 n = 240;
0052
0053 X_r = zeros(size(F,1),n);
0054
0055
0056 a = zeros(1,n);
0057 a(1,10:30) = pi/42/dt + 0.01*randn(1,21);
0058 a(1,35:55) = -pi/42/dt + 0.01*randn(1,21);
0059 a(1,80:105) = -pi/52/dt + 0.01*randn(1,26);
0060 a(1,140:165) = -pi/52/dt + 0.01*randn(1,26);
0061 a(1,190:210) = -pi/42/dt + 0.01*randn(1,21);
0062 a(1,215:235) = pi/42/dt + 0.01*randn(1,21);
0063
0064
0065 x = [-4;-0.2;1;0];
0066
0067
0068 for i=1:n
0069 F = [0 0 1 0;...
0070 0 0 0 1;...
0071 0 0 0 a(i);...
0072 0 0 -a(i) 0];
0073 x = expm(F*dt)*x;
0074 X_r(1:4,i) = x;
0075 end
0076
0077
0078 Y = zeros(size(H,1),n);
0079
0080 for i = 1:n
0081 Y(:,i) = H*X_r(:,i) + gauss_rnd(zeros(size(Y,1),1), R);
0082 end
0083
0084
0085
0086
0087
0088 N1 = 10;
0089 NT = 1;
0090 TP = 1;
0091 CP = 0.5;
0092
0093
0094 cx_min = -5;
0095 cx_max = 5;
0096 cy_min = -4;
0097 cy_max = 4;
0098 c_width = cx_max - cx_min;
0099 c_height = cy_max - cy_min;
0100
0101
0102 CD = 1/(c_width*c_height);
0103
0104
0105
0106 TC = ones(1,size(X_r,2));
0107
0108
0109 for i=1:size(X_r,2)
0110 if rand < CP
0111 TC(i) = 0;
0112 Y(1,i) = c_width*rand(1,1) + cx_min;
0113 Y(2,i) = c_height*rand(1,1) + cy_min;
0114 end
0115 end
0116
0117
0118 if print_figures
0119 set(gcf,'PaperType','a4');
0120 set(gcf,'PaperPosition',[0.25 2.5 3.8 3]);
0121 set(gca,'Position',[0.25 2.5 3.8 3]);
0122
0123
0124 I1 = find(TC);
0125 I2 = find(TC == 0);
0126
0127 clf;
0128 h=plot(X_r(1,:),X_r(2,:),'-',...
0129 Y(1,I1),Y(2,I1),'ko',...
0130 Y(1,I2),Y(2,I2),'kx');
0131 set(h(1),'color',[0.3 0.3 0.3]);
0132 set(h,'markersize',4);
0133 legend('True trajectory',...
0134 'Measurement',...
0135 'Clutter Measurement')
0136 set(h,'markersize',4);
0137 set(h,'linewidth',0.5);
0138 set(gca,'FontSize',4);
0139 xlim([cx_min cx_max]);
0140 ylim([cy_min cy_max]);
0141 if save_figures
0142 print('-dpsc','clutter_trajectory.ps');
0143 end
0144 fprintf('Plotting trajectory and measurements. Press any key to continue.\n');
0145 pause
0146 end
0147
0148
0149 MM1 = cell(1,NT);
0150 PP1 = cell(1,NT);
0151
0152 MM1{1} = X_r(:,1);
0153 PP1{1} = diag([0.1 0.1 0.1 0.1]);
0154
0155
0156 S = mcda_init(N1,MM1,PP1);
0157
0158
0159 MM = zeros(m,n);
0160 SS = cell(n,N1);
0161
0162
0163
0164 M0 = X_r(:,1);
0165 P0 = diag([0.1 0.1 0.1 0.1]);
0166
0167
0168 MM_kf0 = zeros(m,n);
0169 PP_kf0 = zeros(m,m,n);
0170
0171 clf;
0172 fprintf('Filtering...\n');
0173 for k=1:size(Y,2)
0174
0175 [M0,P0] = kf_predict(M0,P0,A,Q);
0176 [M0,P0] = kf_update(M0,P0,Y(:,k),H,R);
0177 MM_kf0(:,k) = M0;
0178 PP_kf0(:,:,k) = P0;
0179
0180
0181 S = kf_mcda_predict(S,A,Q);
0182 [S,C] = kf_mcda_update(S,Y(:,k),H,R,TP,CP,CD);
0183
0184 M = calculate_mean(S,1);
0185 MM(:,k) = M;
0186
0187
0188
0189
0190 n_eff = eff_particles(S);
0191 if n_eff < N1/4
0192 ind = resample(S);
0193 S = S(:,ind);
0194
0195 W = ones(1,N1)/N1;
0196 S = set_weights(S,W);
0197 fprintf('Resampling 1 done on time step %d\n',k);
0198 end
0199
0200 SS(k,:) = S;
0201
0202
0203
0204
0205 if print_figures
0206 hold off;
0207 nvis = 100;
0208 MV = zeros(m,N1);
0209 PV = zeros(m,m,N1);
0210 for j=1:N1
0211 MV(:,j) = S{j}.M{1};
0212 PV(:,:,j) = S{j}.P{1};
0213 end
0214 W = get_weights(S);
0215 samp = gmm_rnd(MV,PV,W,nvis);
0216 h = plot(X_r(1,1:k),X_r(2,1:k),'g-.',...
0217 Y(1,1:k),Y(2,1:k),'rx',...
0218 samp(1,:),samp(2,:),'b.',...
0219 MM(1,1:k),MM(2,1:k),'k-');
0220 set(h(3),'markersize',2);
0221
0222 xlim([cx_min cx_max]);
0223 ylim([cy_min cy_max]);
0224
0225 drawnow;
0226 end
0227 end
0228
0229 fprintf('Smoothing...');
0230 [SM,S_EST1] = kf_mcda_smooth(SS,A,Q);
0231 fprintf('Done!\n');
0232
0233
0234 rmse_mc1 = sqrt(mean(mean((MM(1:2,:)-X_r(1:2,:)).^2)));
0235 rmse_smc1 = sqrt(mean(mean((S_EST1{1}(1:2,:)-X_r(1:2,:)).^2)));
0236
0237
0238 fprintf('RMSE_mc1: %.3f\n',rmse_mc1);
0239 fprintf('RMSE_smc1: %.3f\n',rmse_smc1);
0240
0241
0242
0243 if print_figures
0244 clutter_plot1;
0245 if save_figures
0246 print('-dpsc','clutter1.ps');
0247 end
0248 fprintf('Plotting filtering results. Press any key to continue.\n');
0249 pause
0250
0251
0252 clutter_plot2;
0253 if save_figures
0254 print('-dpsc','clutter2.ps');
0255 end
0256 fprintf('Plotting smoothing results.\n');
0257 end