Skip to content

Commit 54ff6ad

Browse files
authored
Update Kalman3D_Wave.h
1 parent 1e16807 commit 54ff6ad

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

bbn_wave_freq_m5atomS3/Kalman3D_Wave.h

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ class EIGEN_ALIGN_MAX Kalman3D_Wave {
395395
const Eigen::Matrix<T,3,1>& r,
396396
const Eigen::Matrix<T,3,3>& R)
397397
{
398-
// Innovation covariance
399398
Eigen::Matrix<T,3,3> S = C * P * C.transpose() + R;
400399

401400
Eigen::LDLT<Eigen::Matrix<T,3,3>> ldlt(S);
@@ -406,22 +405,17 @@ class EIGEN_ALIGN_MAX Kalman3D_Wave {
406405
if (ldlt.info() != Eigen::Success) return;
407406
}
408407

409-
// Kalman gain
410408
Eigen::Matrix<T,NX,3> K = P * C.transpose() *
411409
ldlt.solve(Eigen::Matrix<T,3,3>::Identity());
412410

413-
// State update
411+
// state update
414412
x.noalias() += K * r;
415413

416-
// Covariance update (Joseph form, numerically stable)
417-
const Eigen::Matrix<T,3,NX> CP = C * P;
418-
const Eigen::Matrix<T,NX,NX> KCP = K * CP;
419-
const Eigen::Matrix<T,NX,NX> KSKt = K * R * K.transpose();
420-
421-
P.noalias() -= KCP;
422-
P.noalias() -= KCP.transpose();
423-
P.noalias() += KSKt;
414+
// covariance update (safe Joseph form)
415+
Eigen::Matrix<T,NX,NX> I_KC = MatrixNX::Identity() - K * C;
416+
P.noalias() = I_KC * P * I_KC.transpose() + K * R * K.transpose();
424417

418+
// enforce symmetry
425419
P = T(0.5) * (P + P.transpose());
426420
}
427421

@@ -433,13 +427,11 @@ class EIGEN_ALIGN_MAX Kalman3D_Wave {
433427
const Eigen::Matrix<T,3,1>& inno,
434428
const Eigen::Matrix<T,3,3>& R_S)
435429
{
436-
// Pull out S-block only
437-
Eigen::Matrix<T,NX,3> P_Scols = P.template block<NX,3>(0, off_S);
438-
Eigen::Matrix<T,3,NX> P_Srows = P.template block<3,NX>(off_S, 0);
439-
Eigen::Matrix<T,3,3> P_SS = P.template block<3,3>(off_S, off_S);
430+
// Measurement matrix for pseudo: H selects the S block
431+
Eigen::Matrix<T,3,NX> H = Eigen::Matrix<T,3,NX>::Zero();
432+
H.template block<3,3>(0, off_S) = Matrix3::Identity();
440433

441-
// Innovation covariance
442-
Eigen::Matrix<T,3,3> S_mat = P_SS + R_S;
434+
Eigen::Matrix<T,3,3> S_mat = H * P * H.transpose() + R_S;
443435
Eigen::LDLT<Eigen::Matrix<T,3,3>> ldlt(S_mat);
444436
if (ldlt.info() != Eigen::Success) {
445437
S_mat += Eigen::Matrix<T,3,3>::Identity() *
@@ -448,21 +440,17 @@ class EIGEN_ALIGN_MAX Kalman3D_Wave {
448440
if (ldlt.info() != Eigen::Success) return;
449441
}
450442

451-
// Kalman gain
452-
Eigen::Matrix<T,NX,3> K = P_Scols *
443+
Eigen::Matrix<T,NX,3> K = P * H.transpose() *
453444
ldlt.solve(Eigen::Matrix<T,3,3>::Identity());
454445

455-
// State update
446+
// state update
456447
x.noalias() += K * inno;
457448

458-
// Covariance update (Joseph form, consistent with accel/mag)
459-
const Eigen::Matrix<T,NX,NX> KCP = K * P_Srows;
460-
const Eigen::Matrix<T,NX,NX> KSKt = K * R_S * K.transpose();
461-
462-
P.noalias() -= KCP;
463-
P.noalias() -= KCP.transpose();
464-
P.noalias() += KSKt;
449+
// covariance update (safe Joseph form)
450+
Eigen::Matrix<T,NX,NX> I_KH = MatrixNX::Identity() - K * H;
451+
P.noalias() = I_KH * P * I_KH.transpose() + K * R_S * K.transpose();
465452

453+
// enforce symmetry
466454
P = T(0.5) * (P + P.transpose());
467455
}
468456
};

0 commit comments

Comments
 (0)