-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathIMMUKFUpdate.m
executable file
·131 lines (104 loc) · 3.48 KB
/
IMMUKFUpdate.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
function [X_Filter,P_Filter, innovation_store, S_store, X_IMM, P_IMM, modeProbs]= IMMUKFUpdate(X_IMM,P_IMM,IMU_meas,IMU_noise,GPS_meas,GPS_noise,transMatrix,modeProbs,UKF_update,dt,Q)
%Determine number of models
m = size(X_IMM,2);
%Number of states
n = size(X_IMM,1);
%Number of measurements
if ~isempty(GPS_meas)
measurement = GPS_meas(2:3);
end
innovation_store = [];
S_store = [];
%*****Interacting/Mixing*****
c_j = zeros(1,m);
for j = 1:m
for i = 1:m
c_j(j) = c_j(j) + transMatrix(i,j)*modeProbs(i);
end
end
modeProbs_ij = zeros(m,m);
for i = 1:m
for j = 1:m
modeProbs_ij(i,j) = 1/c_j(j)*transMatrix(i,j)*modeProbs(i);
end
end
%Calculate mixed state and covariance
X0j = zeros(n,m);
for j = 1:m
for i = 1:m
X0j(:,j) = X0j(:,j) + X_IMM(:,i)*modeProbs_ij(i,j);
end
end
P0j = zeros(n,n,m);
for j = 1:m
for i = 1:m
P0j(:,:,j) = P0j(:,:,j) + modeProbs_ij(i,j)*(P_IMM(:,:,i) + (X_IMM(:,i)-X0j(:,j))*(X_IMM(:,i)-X0j(:,j))');
end
end
for i = 1:m
[W_UKF,sigma] = unscentedTransform(X0j(:,i),P0j(:,:,i));
% sigma_meas = zeros(noMeas,2*na+1,noGSFmodels);
for j = 1:2*n+1
[sigma(:,j)] = UKFpredict(sigma(:,j),dt,IMU_meas(1,:),IMU_meas(2,:),IMU_noise);
sigma_meas(:,j,i) = sigma(1:2,j);
end
x_UKF = zeros(n,1);
P_UKF = zeros(n,n);
for j = 1:2*n+1
x_UKF = x_UKF + W_UKF(j)*sigma(:,j);
end
for j = 1:2*n+1
P_UKF = P_UKF + W_UKF(j)*[sigma(:,j) - x_UKF]*[sigma(:,j) - x_UKF]';
end
P_UKF = P_UKF + Q;
X0j(:,i) = x_UKF;
P0j(:,:,i) = P_UKF;
if UKF_update == 1
%Perform Kalman filter updates on each filter and calculate likelihoods
obsStates = length(measurement);
S_store = zeros(obsStates,m);
innovation_store = zeros(obsStates,m);
Pxz = zeros(n,obsStates);
Pzz = zeros(obsStates,obsStates);
ymeas = zeros(obsStates,1);
for j = 1:2*n+1
ymeas = ymeas + W_UKF(j)*sigma_meas(:,j);
end
for j = 1:2*n+1
Pxz = Pxz + W_UKF(j)*[sigma(:,j)- x_UKF]*[sigma_meas(:,j)-ymeas]';
Pzz = Pzz + W_UKF(j)*[sigma_meas(:,j)-ymeas]*[sigma_meas(:,j)-ymeas]';
end
R = diag(GPS_noise);
Scov = R + Pzz;
k = Pxz*inv(Scov);
measurement = GPS_meas(2:3)';
innovation = measurement - ymeas;
correction = k*innovation;
X0j(:,i) = X0j(:,i) + correction;
P0j(:,:,i) = P0j(:,:,i) - k*Scov*k';
lamda(i) = GaussianDensity(measurement, ymeas, Scov);
S_store(:,i) = diag(Scov);
innovation_store(:,i) = innovation;
end
end
if UKF_update == 1
%Mode probability update
c = 0;
for j = 1:m
c = c + lamda(j) * c_j(j);
end
for j = 1:m
modeProbs(j) = 1/c * lamda(j) * c_j(j);
end
end
X_IMM = X0j;
P_IMM = P0j;
%Estimate and Covariance Combination
X_Filter = zeros(n,1);
for j = 1:m
X_Filter = X_Filter + X_IMM(:,j) * modeProbs(j);
end
P_Filter = zeros(n,n);
for j = 1:m
P_Filter = P_Filter + modeProbs(j) * (P_IMM(:,:,j) + (X_IMM(:,j)-X_Filter)*(X_IMM(:,j)-X_Filter)');
end