-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathParticleEx3.m
More file actions
173 lines (159 loc) · 5.8 KB
/
ParticleEx3.m
File metadata and controls
173 lines (159 loc) · 5.8 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
function [xhatRMS, xhatPartRMS, xhatPartRegRMS] = ParticleEx3
% Particle filter example, adapted from Gordon, Salmond, and Smith paper.
x = 0.1; % initial state
Q = 0.001; % process noise covariance
R = 1; % measurement noise covariance
tf = 50; % simulation length
N = 3; % number of particles in the particle filter
NReg = 5 * N; % number of probability bins in the regularized particle filter
xhat = x;
P = 2;
xhatPart = x;
xhatPartReg = x;
% Initialize the particle filter.
for i = 1 : N
xpart(i) = x + sqrt(P) * randn; % normal particle filter
xpartReg(i) = xpart(i); % regularized particle filter
end
% Initialization for the regularized particle filter.
d = length(x); % dimension of the state vector
c = 2; % volume of unit hypersphere in d-dimensional space
h = (8 * c^(-1) * (d + 4) * (2 * sqrt(pi))^d)^(1 / (d + 4)) * N^(-1 / (d + 4)); % bandwidth of regularized filter
% Initialize arrays.
xArr = [x];
yArr = [x^2 / 20 + sqrt(R) * randn];
xhatArr = [x];
PArr = [P];
xhatPartArr = [xhatPart];
xhatPartRegArr = [xhatPartReg];
close all; % close all open figures
for k = 1 : tf
% System simulation
x = 0.5 * x + 25 * x / (1 + x^2) + 8 * cos(1.2*(k-1)) + sqrt(Q) * randn;
y = x^2 / 20 + sqrt(R) * randn;
% Extended Kalman filter
F = 0.5 + 25 * (1 - xhat^2) / (1 + xhat^2)^2;
P = F * P * F' + Q;
H = xhat / 10;
K = P * H' * (H * P * H' + R)^(-1);
xhat = 0.5 * xhat + 25 * xhat / (1 + xhat^2) + 8 * cos(1.2*(k-1));
xhat = xhat + K * (y - xhat^2 / 20);
P = (1 - K * H) * P;
% Particle filter
for i = 1 : N
xpartminus(i) = 0.5 * xpart(i) + 25 * xpart(i) / (1 + xpart(i)^2) + 8 * cos(1.2*(k-1)) + sqrt(Q) * randn;
ypart = xpartminus(i)^2 / 20;
vhat = y - ypart;
q(i) = (1 / sqrt(R) / sqrt(2*pi)) * exp(-vhat^2 / 2 / R);
end
% Normalize the likelihood of each a priori estimate.
qsum = sum(q);
for i = 1 : N
q(i) = q(i) / qsum;
end
% Resample.
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
xpart(i) = xpartminus(j);
break;
end
end
end
% The particle filter estimate is the mean of the particles.
xhatPart = mean(xpart);
% Now run the regularized particle filter.
% Perform the time update to the get the a priori regularized particles.
for i = 1 : N
xpartminusReg(i) = 0.5 * xpartReg(i) + 25 * xpartReg(i) / (1 + xpartReg(i)^2) + 8 * cos(1.2*(k-1)) + sqrt(Q) * randn;
ypart = xpartminusReg(i)^2 / 20;
vhat = y - ypart;
q(i) = (1 / sqrt(R) / sqrt(2*pi)) * exp(-vhat^2 / 2 / R);
end
% Normalize the probabilities of the a priori particles.
q = q / sum(q);
% Compute the covariance of the a priori particles.
S = cov(xpartminusReg');
A = chol(S)';
% Define the domain from which we will choose a posteriori particles for
% the regularized particle filter.
xreg(1) = min(xpartminusReg) - std(xpartminusReg);
xreg(NReg) = max(xpartminusReg) + std(xpartminusReg);
dx = (xreg(NReg) - xreg(1)) / (NReg - 1);
for i = 2 : NReg - 1
xreg(i) = xreg(i-1) + dx;
end
% Create the pdf approximation that is required for the regularized
% particle filter.
for j = 1 : NReg
qreg(j) = 0;
for i = 1 : N
normx = norm(inv(A) * (xreg(j) - xpartminusReg(i)));
if normx < h
qreg(j) = qreg(j) + q(i) * (d + 2) * (1 - normx^2 / h^2) / 2 / c / h^d / det(A);
end
end
end
% Normalize the likelihood of each state estimate for the regularized particle filter.
qsum = sum(qreg);
for j = 1 : NReg
qreg(j) = qreg(j) / qsum;
end
% Resample for the regularized particle filter.
for i = 1 : N
u = rand; % uniform random number between 0 and 1
qtempsum = 0;
for j = 1 : NReg
qtempsum = qtempsum + qreg(j);
if qtempsum >= u
xpartReg(i) = xreg(j);
break;
end
end
end
% The regularized particle filter estimate is the mean of the regularized particles.
xhatPartReg = mean(xpartReg);
% Plot the discrete pdf and the continuous pdf at a specific time.
if k == 5
figure; hold on;
for i = 1 : N
plot([xpartminusReg(i) xpartminusReg(i)], [0 q(i)], 'k-');
plot(xpartminusReg(i), q(i), 'ko');
end
plot(xreg, qreg, 'r:');
set(gca,'FontSize',12); set(gcf,'Color','White');
set(gca,'box','on');
xlabel('state estimate'); ylabel('pdf');
end
% Save data in arrays for later plotting
xArr = [xArr x];
yArr = [yArr y];
xhatArr = [xhatArr xhat];
PArr = [PArr P];
xhatPartArr = [xhatPartArr xhatPart];
xhatPartRegArr = [xhatPartRegArr xhatPartReg];
end
t = 0 : tf;
%figure;
%plot(t, xArr);
%ylabel('true state');
figure;
plot(t, xArr, 'b.', t, xhatArr, 'k-', t, xhatArr-2*sqrt(PArr), 'r:', t, xhatArr+2*sqrt(PArr), 'r:');
axis([0 tf -40 40]);
set(gca,'FontSize',12); set(gcf,'Color','White');
xlabel('time step'); ylabel('state');
legend('True state', 'EKF estimate', '95% confidence region');
figure;
plot(t, xArr, 'b.', t, xhatPartArr, 'k-', t, xhatPartRegArr, 'r:');
set(gca,'FontSize',12); set(gcf,'Color','White');
xlabel('time step'); ylabel('state');
legend('True state', 'Particle filter', 'Regularized particle filter');
xhatRMS = sqrt((norm(xArr - xhatArr))^2 / tf);
xhatPartRMS = sqrt((norm(xArr - xhatPartArr))^2 / tf);
xhatPartRegRMS = sqrt((norm(xArr - xhatPartRegArr))^2 / tf);
disp(['Kalman filter RMS error = ', num2str(xhatRMS)]);
disp(['Particle filter RMS error = ', num2str(xhatPartRMS)]);
disp(['Regularized particle filter RMS error = ', num2str(xhatPartRegRMS)]);