-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathParticleEx5.m
More file actions
216 lines (200 loc) · 7.59 KB
/
ParticleEx5.m
File metadata and controls
216 lines (200 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function [StdErr, EKFErr] = ParticleEx5
% EKF Particle filter example.
% Track a body falling through the atmosphere.
% This system is taken from [Jul00], which was based on [Ath68].
% Compare the particle filter with the EKF particle filter.
global rho0 g k dt
rho0 = 2; % lb-sec^2/ft^4
g = 32.2; % ft/sec^2
k = 2e4; % ft
R = 10^4; % measurement noise variance (ft^2)
Q = diag([0 0 0]); % process noise covariance
M = 10^5; % horizontal range of position sensor
a = 10^5; % altitude of position sensor
P = diag([1e6 4e6 10]); % initial estimation error covariance
x = [3e5; -2e4; 1e-3]; % initial state
xhat = [3e5; -2e4; 1e-3]; % initial state estimate
N = 200; % number of particles
% Initialize the particle filter.
for i = 1 : N
xhatplus(:,i) = x + sqrt(P) * [randn; randn; randn]; % standard particle filter
xhatplusEKF(:,i) = xhatplus(:,i); % EKF particle filter
Pplus(:,:,i) = P; % initial EKF particle filter estimation error covariance
end
T = 0.5; % measurement time step
randn('state',sum(100*clock)); % random number generator seed
tf = 30; % simulation length (seconds)
dt = 0.5; % time step for integration (seconds)
xArray = x;
xhatArray = xhat;
xhatEKFArray = xhat;
for t = T : T : tf
fprintf('.');
% Simulate the system.
for tau = dt : dt : T
% Fourth order Runge Kutta ingegration
[dx1, dx2, dx3, dx4] = RungeKutta(x);
x = x + (dx1 + 2 * dx2 + 2 * dx3 + dx4) / 6;
x = x + sqrt(dt * Q) * [randn; randn; randn] * dt;
end
% Simulate the noisy measurement.
z = sqrt(M^2 + (x(1)-a)^2) + sqrt(R) * randn;
% Simulate the continuous-time part of the particle filters (time update).
xhatminus = xhatplus;
xhatminusEKF = xhatplusEKF;
for i = 1 : N
for tau = dt : dt : T
% Fourth order Runge Kutta ingegration
% standard particle filter
[dx1, dx2, dx3, dx4] = RungeKutta(xhatminus(:,i));
xhatminus(:,i) = xhatminus(:,i) + (dx1 + 2 * dx2 + 2 * dx3 + dx4) / 6;
xhatminus(:,i) = xhatminus(:,i) + sqrt(dt * Q) * [randn; randn; randn] * dt;
xhatminus(3,i) = max(0, xhatminus(3,i)); % the ballistic coefficient cannot be negative
% EKF particle filter
[dx1, dx2, dx3, dx4] = RungeKutta(xhatminusEKF(:,i));
xhatminusEKF(:,i) = xhatminusEKF(:,i) + (dx1 + 2 * dx2 + 2 * dx3 + dx4) / 6;
xhatminusEKF(:,i) = xhatminusEKF(:,i) + sqrt(dt * Q) * [randn; randn; randn] * dt;
xhatminusEKF(3,i) = max(0, xhatminusEKF(3,i)); % the ballistic coefficient cannot be negative
end
% standard particle filter
zhat = sqrt(M^2 + (xhatminus(1,i)-a)^2);
vhat(i) = z - zhat;
% EKF particle filter
zhatEKF = sqrt(M^2 + (xhatminusEKF(1,i)-a)^2);
F = [0 1 0; -rho0 * exp(-xhatminusEKF(1,i)/k) * xhatminusEKF(2,i)^2 / 2 / k * xhatminusEKF(3,i) ...
rho0 * exp(-xhatminusEKF(1,i)/k) * xhatminusEKF(2,i) * xhatminusEKF(3,i) ...
rho0 * exp(-xhatminusEKF(1,i)/k) * xhatminusEKF(2,i)^2 / 2; ...
0 0 0];
H = [(xhatminusEKF(1,i) - a) / sqrt(M^2 + (xhatminusEKF(1,i)-a)^2) 0 0];
Pminus(:,:,i) = F * Pplus(:,:,i) * F' + Q;
K = Pminus(:,:,i) * H' * inv(H * Pminus(:,:,i) * H' + R);
xhatminusEKF(:,i) = xhatminusEKF(:,i) + K * (z - zhatEKF);
zhatEKF = sqrt(M^2 + (xhatminusEKF(1,i)-a)^2);
vhatEKF(i) = z - zhatEKF;
end
% Note that we need to scale all of the q(i) probabilities in a way
% that does not change their relative magnitudes.
% Otherwise all of the q(i) elements will be zero because of the
% large value of the exponential.
% standard particle filter
vhatscale = max(abs(vhat)) / 4;
qsum = 0;
for i = 1 : N
q(i) = exp(-(vhat(i)/vhatscale)^2);
qsum = qsum + q(i);
end
% Normalize the likelihood of each a priori estimate.
for i = 1 : N
q(i) = q(i) / qsum;
end
% EKF particle filter
vhatscaleEKF = max(abs(vhatEKF)) / 4;
qsumEKF = 0;
for i = 1 : N
qEKF(i) = exp(-(vhatEKF(i)/vhatscaleEKF)^2);
qsumEKF = qsumEKF + qEKF(i);
end
% Normalize the likelihood of each a priori estimate.
for i = 1 : N
qEKF(i) = qEKF(i) / qsumEKF;
end
% Resample the standard particle filter
for i = 1 : N
u = rand; % uniform random number between 0 and 1
qtempsum = 0;
for j = 1 : N
qtempsum = qtempsum + q(j);
if qtempsum >= u
xhatplus(:,i) = xhatminus(:,j);
xhatplus(3,i) = max(0,xhatplus(3,i)); % the ballistic coefficient cannot be negative
break;
end
end
end
% The standard particle filter estimate is the mean of the particles.
xhat = mean(xhatplus')';
% Resample the EKF particle filter
Ptemp = Pplus;
for i = 1 : N
u = rand; % uniform random number between 0 and 1
qtempsum = 0;
for j = 1 : N
qtempsum = qtempsum + qEKF(j);
if qtempsum >= u
xhatplusEKF(:,i) = xhatminusEKF(:,j);
xhatplusEKF(3,i) = max(0,xhatplusEKF(3,i)); % the ballistic coefficient cannot be negative
Pplus(:,:,i) = Ptemp(:,:,j);
break;
end
end
end
% The EKF particle filter estimate is the mean of the particles.
xhatEKF = mean(xhatplusEKF')';
% Save data for plotting.
xArray = [xArray x];
xhatArray = [xhatArray xhat];
xhatEKFArray = [xhatEKFArray xhatEKF];
end
close all;
t = 0 : T : tf;
figure;
semilogy(t, abs(xArray(1,:) - xhatArray(1,:)), 'b-'); hold;
semilogy(t, abs(xArray(1,:) - xhatEKFArray(1,:)), 'r:');
set(gca,'FontSize',12); set(gcf,'Color','White');
xlabel('Seconds');
ylabel('Altitude Estimation Error');
legend('Standard particle filter', 'EKF particle filter');
figure;
semilogy(t, abs(xArray(2,:) - xhatArray(2,:)), 'b-'); hold;
semilogy(t, abs(xArray(2,:) - xhatEKFArray(2,:)), 'r:');
set(gca,'FontSize',12); set(gcf,'Color','White');
xlabel('Seconds');
ylabel('Velocity Estimation Error');
legend('Standard particle filter', 'EKF particle filter');
figure;
semilogy(t, abs(xArray(3,:) - xhatArray(3,:)), 'b-'); hold;
semilogy(t, abs(xArray(3,:) - xhatEKFArray(3,:)), 'r:');
set(gca,'FontSize',12); set(gcf,'Color','White');
xlabel('Seconds');
ylabel('Ballistic Coefficient Estimation Error');
legend('Standard particle filter', 'EKF particle filter');
figure;
plot(t, xArray(1,:));
set(gca,'FontSize',12); set(gcf,'Color','White');
xlabel('Seconds');
ylabel('True Position');
figure;
plot(t, xArray(2,:));
title('Falling Body Simulation', 'FontSize', 12);
set(gca,'FontSize',12); set(gcf,'Color','White');
xlabel('Seconds');
ylabel('True Velocity');
for i = 1 : 3
StdErr(i) = sqrt((norm(xArray(i,:) - xhatArray(i,:)))^2 / tf / dt);
EKFErr(i) = sqrt((norm(xArray(i,:) - xhatEKFArray(i,:)))^2 / tf / dt);
end
disp(['Standard particle filter RMS error = ', num2str(StdErr)]);
disp(['EKF particle filter RMS error = ', num2str(EKFErr)]);
function [dx1, dx2, dx3, dx4] = RungeKutta(x)
% Fourth order Runge Kutta integration for the falling body system.
global rho0 g k dt
dx1(1,1) = x(2);
dx1(2,1) = rho0 * exp(-x(1)/k) * x(2)^2 / 2 * x(3) - g;
dx1(3,1) = 0;
dx1 = dx1 * dt;
xtemp = x + dx1 / 2;
dx2(1,1) = xtemp(2);
dx2(2,1) = rho0 * exp(-xtemp(1)/k) * xtemp(2)^2 / 2 * xtemp(3) - g;
dx2(3,1) = 0;
dx2 = dx2 * dt;
xtemp = x + dx2 / 2;
dx3(1,1) = xtemp(2);
dx3(2,1) = rho0 * exp(-xtemp(1)/k) * xtemp(2)^2 / 2 * xtemp(3) - g;
dx3(3,1) = 0;
dx3 = dx3 * dt;
xtemp = x + dx3;
dx4(1,1) = xtemp(2);
dx4(2,1) = rho0 * exp(-xtemp(1)/k) * xtemp(2)^2 / 2 * xtemp(3) - g;
dx4(3,1) = 0;
dx4 = dx4 * dt;
return;