@@ -50,30 +50,30 @@ type EKF struct {
50
50
// - invalid state or output noise is given: noise covariance must either be nil or match the model dimensions
51
51
func New (m filter.Model , init filter.InitCond , q , r filter.Noise ) (* EKF , error ) {
52
52
// size of the input and output vectors
53
- in , out := m .Dims ()
54
- if in <= 0 || out <= 0 {
55
- return nil , fmt .Errorf ("Invalid model dimensions: [%d x %d]" , in , out )
53
+ nx , _ , ny , _ := m .SystemDims ()
54
+ if nx <= 0 || ny <= 0 {
55
+ return nil , fmt .Errorf ("invalid model dimensions: [%d x %d]" , nx , ny )
56
56
}
57
57
58
58
if q != nil {
59
- if q .Cov ().Symmetric () != in {
60
- return nil , fmt .Errorf ("Invalid state noise dimension: %d" , q .Cov ().Symmetric ())
59
+ if q .Cov ().Symmetric () != nx {
60
+ return nil , fmt .Errorf ("invalid state noise dimension: %d" , q .Cov ().Symmetric ())
61
61
}
62
62
} else {
63
63
q , _ = noise .NewNone ()
64
64
}
65
65
66
66
if r != nil {
67
- if r .Cov ().Symmetric () != out {
68
- return nil , fmt .Errorf ("Invalid output noise dimension: %d" , r .Cov ().Symmetric ())
67
+ if r .Cov ().Symmetric () != ny {
68
+ return nil , fmt .Errorf ("invalid output noise dimension: %d" , r .Cov ().Symmetric ())
69
69
}
70
70
} else {
71
71
r , _ = noise .NewNone ()
72
72
}
73
73
74
74
// propagation Jacobian
75
75
fJacFn := func (u mat.Vector ) func ([]float64 , []float64 ) {
76
- q , _ := noise .NewZero (in )
76
+ q , _ := noise .NewZero (nx )
77
77
78
78
return func (xOut , xNow []float64 ) {
79
79
x := mat .NewVecDense (len (xNow ), xNow )
@@ -87,11 +87,11 @@ func New(m filter.Model, init filter.InitCond, q, r filter.Noise) (*EKF, error)
87
87
}
88
88
}
89
89
}
90
- f := mat .NewDense (in , in , nil )
90
+ f := mat .NewDense (nx , nx , nil )
91
91
92
92
// observation Jacobian
93
93
hJacFn := func (u mat.Vector ) func ([]float64 , []float64 ) {
94
- r , _ := noise .NewZero (out )
94
+ r , _ := noise .NewZero (ny )
95
95
96
96
return func (y , xNow []float64 ) {
97
97
x := mat .NewVecDense (len (xNow ), xNow )
@@ -106,7 +106,7 @@ func New(m filter.Model, init filter.InitCond, q, r filter.Noise) (*EKF, error)
106
106
}
107
107
}
108
108
}
109
- h := mat .NewDense (out , in , nil )
109
+ h := mat .NewDense (ny , nx , nil )
110
110
111
111
// initialize covariance matrix to initial condition covariance
112
112
p := mat .NewSymDense (init .Cov ().Symmetric (), nil )
@@ -116,10 +116,10 @@ func New(m filter.Model, init filter.InitCond, q, r filter.Noise) (*EKF, error)
116
116
pNext := mat .NewSymDense (init .Cov ().Symmetric (), nil )
117
117
118
118
// innovation vector
119
- inn := mat .NewVecDense (out , nil )
119
+ inn := mat .NewVecDense (ny , nil )
120
120
121
121
// kalman gain
122
- k := mat .NewDense (in , out , nil )
122
+ k := mat .NewDense (nx , ny , nil )
123
123
124
124
return & EKF {
125
125
m : m ,
@@ -143,7 +143,7 @@ func (k *EKF) Predict(x, u mat.Vector) (filter.Estimate, error) {
143
143
// propagate input state to the next step
144
144
xNext , err := k .m .Propagate (x , u , k .q .Sample ())
145
145
if err != nil {
146
- return nil , fmt .Errorf ("System state propagation failed: %v" , err )
146
+ return nil , fmt .Errorf ("system state propagation failed: %v" , err )
147
147
}
148
148
149
149
// calculate propagation Jacobian matrix
@@ -174,16 +174,16 @@ func (k *EKF) Predict(x, u mat.Vector) (filter.Estimate, error) {
174
174
// Update corrects state x using the measurement z, given control intput u and returns corrected estimate.
175
175
// It returns error if either invalid state was supplied or if it fails to calculate system output estimate.
176
176
func (k * EKF ) Update (x , u , z mat.Vector ) (filter.Estimate , error ) {
177
- in , out := k .m .Dims ()
177
+ nx , _ , ny , _ := k .m .SystemDims ()
178
178
179
- if z .Len () != out {
180
- return nil , fmt .Errorf ("Invalid measurement supplied: %v" , z )
179
+ if z .Len () != ny {
180
+ return nil , fmt .Errorf ("invalid measurement supplied: %v" , z )
181
181
}
182
182
183
183
// observe system output in the next step
184
184
y , err := k .m .Observe (x , u , k .r .Sample ())
185
185
if err != nil {
186
- return nil , fmt .Errorf ("Failed to observe system output: %v" , err )
186
+ return nil , fmt .Errorf ("failed to observe system output: %v" , err )
187
187
}
188
188
189
189
// calculate observation Jacobian matrix
@@ -192,8 +192,8 @@ func (k *EKF) Update(x, u, z mat.Vector) (filter.Estimate, error) {
192
192
Concurrent : true ,
193
193
})
194
194
195
- pxy := mat .NewDense (in , out , nil )
196
- pyy := mat .NewDense (out , out , nil )
195
+ pxy := mat .NewDense (nx , ny , nil )
196
+ pyy := mat .NewDense (ny , ny , nil )
197
197
198
198
// P*H'
199
199
pxy .Mul (k .pNext , k .h .T ())
@@ -209,7 +209,7 @@ func (k *EKF) Update(x, u, z mat.Vector) (filter.Estimate, error) {
209
209
// calculate Kalman gain
210
210
pyyInv := & mat.Dense {}
211
211
if err := pyyInv .Inverse (pyy ); err != nil {
212
- return nil , fmt .Errorf ("Failed to calculat Pyy inverse: %v" , err )
212
+ return nil , fmt .Errorf ("failed to calculat Pyy inverse: %v" , err )
213
213
}
214
214
gain := & mat.Dense {}
215
215
gain .Mul (pxy , pyyInv )
@@ -257,8 +257,8 @@ func (k *EKF) Update(x, u, z mat.Vector) (filter.Estimate, error) {
257
257
k .inn .CopyVec (inn )
258
258
k .k .Copy (gain )
259
259
// update EKF covariance matrix
260
- for i := 0 ; i < in ; i ++ {
261
- for j := i ; j < in ; j ++ {
260
+ for i := 0 ; i < nx ; i ++ {
261
+ for j := i ; j < nx ; j ++ {
262
262
k .p .SetSym (i , j , pCorr .At (i , j ))
263
263
}
264
264
}
@@ -310,11 +310,11 @@ func (k *EKF) Cov() mat.Symmetric {
310
310
// It returns error if either cov is nil or its dimensions are not the same as EKF covariance dimensions.
311
311
func (k * EKF ) SetCov (cov mat.Symmetric ) error {
312
312
if cov == nil {
313
- return fmt .Errorf ("Invalid covariance matrix: %v" , cov )
313
+ return fmt .Errorf ("invalid covariance matrix: %v" , cov )
314
314
}
315
315
316
316
if cov .Symmetric () != k .p .Symmetric () {
317
- return fmt .Errorf ("Invalid covariance matrix dims: [%d x %d]" , cov .Symmetric (), cov .Symmetric ())
317
+ return fmt .Errorf ("invalid covariance matrix dims: [%d x %d]" , cov .Symmetric (), cov .Symmetric ())
318
318
}
319
319
320
320
k .p .CopySym (cov )
0 commit comments