This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbatch.go
79 lines (73 loc) · 2.54 KB
/
batch.go
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
package gokalman
import "github.com/gonum/matrix/mat64"
type measurementInfo struct {
RealObs *mat64.Vector
ComputedObs *mat64.Vector
ObservationDev *mat64.Vector
Φ, H *mat64.Dense
}
// BatchKF defines a vanilla kalman filter. Use NewVanilla to initialize.
type BatchKF struct {
Λ *mat64.Dense
N *mat64.Vector
Measurements []measurementInfo
noise Noise
locked bool // Locks the KF to prevent adding more measurements when iterating
step int
}
// NewBatchKF returns a new hybrid Kalman Filter which can be used both as a CKF and EKF.
// Warning: there is a failsafe preventing any update prior to updating the matrices.
// Usage:
// ```
/// kf.Prepare(Φ, Htilde)
// estimate, err := kf.Update(realObs, computedObs)
/// ```
// Parameters:
// - x0: initial state estimate
// - P0: initial covariance symmetric matrix
// - noise: Noise
// - measSize: number of rows of the measurement vector (not actually important)
func NewBatchKF(numMeasurements int, noise Noise) *BatchKF {
meas := make([]measurementInfo, numMeasurements)
// Note that we will create the Λ matrix and N vector on the first call to SetNextMeasurement.
return &BatchKF{nil, nil, meas, noise, false, 0}
}
// SetNextMeasurement sets the next sequential measurement to the list of measurements to be taken into account for the filter.
func (kf *BatchKF) SetNextMeasurement(realObs, computedObs *mat64.Vector, Φ, H *mat64.Dense) {
if kf.N == nil || kf.Λ == nil {
// There is no reason for both to *not* happen at the same time, but whatevs
_, cH := H.Dims()
kf.N = mat64.NewVector(cH, nil)
kf.Λ = mat64.NewDense(cH, cH, nil)
}
// And compute the current Λ and N.
var HtR, HtRH mat64.Dense
HtR.Mul(H.T(), kf.noise.MeasurementMatrix())
HtRH.Mul(&HtR, H)
kf.Λ.Add(kf.Λ, &HtRH)
// Compute observation deviation y
var y, HtRY mat64.Vector
y.SubVec(realObs, computedObs)
// Store the measurement
kf.Measurements[kf.step] = measurementInfo{realObs, computedObs, &y, Φ, H}
HtRY.MulVec(&HtR, &y)
kf.N.AddVec(kf.N, &HtRY)
kf.step++
}
// Solve will solve the Batch Kalman filter once and return xHat0 and P0, or an error
func (kf *BatchKF) Solve() (xHat0 *mat64.Vector, P0 *mat64.SymDense, err error) {
var Λinv mat64.Dense
if err = Λinv.Inverse(kf.Λ); err != nil {
return nil, nil, err
}
// Make Λinv symmetric and store in P0
P0, err = AsSymDense(&Λinv)
if err != nil {
return nil, nil, err
}
// Compute xHat0
rΛ, _ := kf.Λ.Dims()
xHat0 = mat64.NewVector(rΛ, nil)
xHat0.MulVec(P0, kf.N)
return xHat0, P0, nil
}