-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathekf_ukf_pf.m
366 lines (184 loc) · 6.78 KB
/
ekf_ukf_pf.m
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
%EKF UKF PF 的三个算法
clear;
%tic;
x =0.1; %初始状态
x_estimate=1;%状态的估计
e_x_estimate=x_estimate;%EKF的初始估计
u_x_estimate=x_estimate;%UKF的初始估计
p_x_estimate=x_estimate;%PF的初始估计
Q =10;%input('请输入过程噪声方差 Q 的值 :'); %过程状态协方差
R =1;%input('请输入测量噪声方差 R 的值 :'); %测量噪声协方差
P =5;%初始估计方差
e_P=P; %UKF方差
u_P=P;%UKF方差
pf_P=P;%PF方差
tf =50; %模拟长度
x_array=[x];%真实值数组
e_x_estimate_array=[e_x_estimate];%EKF最优估计值数组
u_x_estimate_array=[u_x_estimate];%UKF最优估计值数组
p_x_estimate_array=[p_x_estimate];%PF最优估计值数组
u_k=1; %微调参数
u_symmetry_number=4; %对称的点的个数
u_total_number=2*u_symmetry_number+1; %总的采样点的个数
linear =0.5;
N =500; %粒子滤波的粒子数
close all;
%粒子滤波初始 N 个粒子
for i =1:N
p_xpart(i)=p_x_estimate+sqrt(pf_P)*randn;
end
for k =1:tf
%模拟系统
x =linear *x +(25*x /(1+x^2))+8*cos(1.2*(k-1))+sqrt(Q)*randn; %状态值
y =(x^2/20) +sqrt(R)*randn; %观测值
%扩展卡尔曼滤波器
%进行估计 第一阶段的估计
e_x_estimate_1=linear *e_x_estimate+25*e_x_estimate/(1+e_x_estimate^2)+8*cos(1.2*(k-1));
e_y_estimate=(e_x_estimate_1)^2/20;%这是根据 k=1时估计值为 1得到的观测值;只是这个由 我估计得到的 第 24行的 y 也是观测值 不过是由加了噪声的真实值得到的
%相关矩阵
e_A=linear +25*(1-e_x_estimate^2)/((1+e_x_estimate^2)^2);%传递矩阵
e_H=e_x_estimate_1/10;%观测矩阵
%估计的误差
e_p_estimate=e_A*e_P*e_A'+Q;
%扩展卡尔曼增益
e_K=e_p_estimate*e_H'/(e_H*e_p_estimate*e_H'+R);
%进行估计值的更新 第二阶段
e_x_estimate_2=e_x_estimate_1+e_K*(y-e_y_estimate);
%更新后的估计值的误差
e_p_estimate_update=e_p_estimate-e_K*e_H*e_p_estimate;
%进入下一次迭代的参数变化
e_P=e_p_estimate_update;
e_x_estimate=e_x_estimate_2;
%粒子滤波器
%粒子滤波器
for i =1:N
p_xpartminus(i)=0.5*p_xpart(i)+25*p_xpart(i)/(1+p_xpart(i)^2)+8*cos(1.2*(k-1))+ sqrt(Q)*randn; %这个式子比下面一行的效果好
%xpartminus(i)=0.5*xpart(i)+25*xpart(i)/(1+xpart(i)^2)+8*cos(1.2*(k-1));
p_ypart=p_xpartminus(i)^2/20; %预测值
p_vhat=y -p_ypart;%观测和预测的差
p_q(i)=(1/sqrt(R)/sqrt(2*pi))*exp(-p_vhat^2/2/R); %各个粒子的权值
end
%平均每一个估计的可能性
p_qsum=sum(p_q);
for i =1:N
p_q(i)=p_q(i)/p_qsum;%各个粒子进行权值归一化
end
%重采样 权重大的粒子多采点,权重小的粒子少采点 , 相当于每一次都进行重采样;
for i =1:N
p_u=rand;
p_qtempsum=0;
for j =1:N
p_qtempsum=p_qtempsum+p_q(j);
if p_qtempsum>=p_u
p_xpart(i)=p_xpartminus(j);%在这里 xpart(i)实现循环赋值;终于找到了这里! ! ! break;
end
end
end
p_x_estimate=mean(p_xpart);
%p_x_estimate=0;
%for i =1:N
%p_x_estimate=p_x_estimate+p_q(i)*p_xpart(i);
%end
%不敏卡尔曼滤波器
%采样点的选取 存在 x(i)
u_x_par=u_x_estimate;
for i =2:(u_symmetry_number+1)
u_x_par(i,:)=u_x_estimate+sqrt((u_symmetry_number+u_k)*u_P);
end
for i =(u_symmetry_number+2):u_total_number
u_x_par(i,:)=u_x_estimate-sqrt((u_symmetry_number+u_k)*u_P);
end
%计算权值
u_w_1=u_k/(u_symmetry_number+u_k);
u_w_N1=1/(2*(u_symmetry_number+u_k));
%把这些粒子通过传递方程 得到下一个状态
for i =1:u_total_number
u_x_par(i)=0.5*u_x_par(i)+25*u_x_par(i)/(1+u_x_par(i)^2)+8*cos(1.2*(k-1));
end
%传递后的均值和方差
u_x_next=u_w_1*u_x_par(1);
for i =2:u_total_number
u_x_next=u_x_next+u_w_N1*u_x_par(i);
end
u_p_next=Q +u_w_1*(u_x_par(1)-u_x_next)*(u_x_par(1)-u_x_next)';
for i =2:u_total_number
u_p_next=u_p_next+u_w_N1*(u_x_par(i)-u_x_next)*(u_x_par(i)-u_x_next)';
end
%%对传递后的均值和方差进行采样 产生粒子 存在 y(i)
%u_y_2obser(1)=u_x_next;
%for i =2:(u_symmetry_number+1)
%u_y_2obser(i,:)=u_x_next+sqrt((u_symmetry_number+k)*u_p_next); %end
%for i =(u_symmetry_number+2) :u_total_number
%u_y_2obser(i,:)=u_x_next-sqrt((u_symmetry_number+u_k)*u_p_next); %end
%另外存在 y_2obser(i)中;
for i =1:u_total_number
u_y_2obser(i,:)=u_x_par(i);
end
%通过观测方程 得到一系列的粒子
for i =1:u_total_number
u_y_2obser(i)=u_y_2obser(i)^2/20;
end
%通过观测方程后的均值 y_obse
u_y_obse=u_w_1*u_y_2obser(1);
for i =2:u_total_number
u_y_obse=u_y_obse+u_w_N1*u_y_2obser(i);
end
%Pzz测量方差矩阵
u_pzz=R +u_w_1*(u_y_2obser(1)-u_y_obse)*(u_y_2obser(1)-u_y_obse)';
for i =2:u_total_number
u_pzz=u_pzz+u_w_N1*(u_y_2obser(i)-u_y_obse)*(u_y_2obser(i)-u_y_obse)';
end
%Pxz状态向量与测量值的协方差矩阵
u_pxz=u_w_1*(u_x_par(1)-u_x_next)*(u_y_2obser(1)-u_y_obse)';
for i =2:u_total_number
u_pxz=u_pxz+u_w_N1*(u_x_par(i)-u_x_next)*(u_y_2obser(i)-u_y_obse)';
end
%卡尔曼增益
u_K=u_pxz/u_pzz;
%估计量的更新
u_x_next_optimal=u_x_next+u_K*(y-u_y_obse);%第一步的估计值 +修正值; u_x_estimate=u_x_next_optimal;
%方差的更新
u_p_next_update=u_p_next-u_K*u_pzz*u_K';
u_P=u_p_next_update;
%进行画图程序
x_array=[x_array,x];
e_x_estimate_array=[e_x_estimate_array,e_x_estimate];
p_x_estimate_array=[p_x_estimate_array,p_x_estimate];
u_x_estimate_array=[u_x_estimate_array,u_x_estimate];
e_error(k,:)=abs(x_array(k)-e_x_estimate_array(k));
p_error(k,:)=abs(x_array(k)-p_x_estimate_array(k));
u_error(k,:)=abs(x_array(k)-u_x_estimate_array(k));
end
t =0:tf;
figure;
plot(t,x_array,'k.',t,e_x_estimate_array,'r-',t,p_x_estimate_array,'g--',t,u_x_estimate_array,'b:');
set(gca,'FontSize',10);
set(gcf,'color','White');
xlabel('时间步长 ');%lable --->label我的神
ylabel('状态 ');
legend('真实值 ','EKF 估计值 ','PF 估计值 ','UKF 估计值 ');
figure;
plot(t,x_array,'k.',t,p_x_estimate_array,'g--',t, p_x_estimate_array-1.96*sqrt(P),'r:',t, p_x_estimate_array+1.96*sqrt(P),'r:');
set(gca,'FontSize',10);
set(gcf,'color','White');
xlabel('时间步长 ');%lable --->label我的神
ylabel('状态 ');
legend('真实值 ','PF 估计值 ', '95%置信区间 ');
%rootmean square 平均值的平方根
e_xrms=sqrt((norm(x_array-e_x_estimate_array)^2)/tf);
disp(['EKF估计误差均方值 =',num2str(e_xrms)]);
p_xrms=sqrt((norm(x_array-p_x_estimate_array)^2)/tf);
disp(['PF估计误差均方值 =',num2str(p_xrms)]);
u_xrms=sqrt((norm(x_array-u_x_estimate_array)^2)/tf);
disp(['UKF估计误差均方值 =',num2str(u_xrms)]);
%plot(t,e_error,'r-',t,p_error,'g--',t,u_error,'b:');
%legend('EKF估计值误差 ','PF 估计值误差 ','UKF 估计值误差 ');
t =1:tf;
figure;
plot(t,e_error,'r-',t,p_error,'g--',t,u_error,'b:');
set(gca,'FontSize',10);
set(gcf,'color','White');
xlabel('时间步长 ');%lable --->label我的神
ylabel('状态 ');
legend('EKF估计值误差 ','PF 估计值误差 ','UKF 估计值误差 ');
%toc;