Skip to content

Commit

Permalink
fix: pass input through the extended filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
piercus committed Oct 7, 2020
1 parent 9cc0dc1 commit 6a4b19a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = {
registerDynamic: modelCollection.registerDynamic,
KalmanFilter: require('./lib/kalman-filter'),
registerObservation: modelCollection.registerObservation,
buildObservation:modelCollection.buildObservation,
buildDynamic: modelCollection.buildDynamic,
getCovariance: require('./lib/utils/get-covariance'),
State: require('./lib/state')
};
26 changes: 16 additions & 10 deletions lib/core-kalman-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class CoreKalmanFilter {
dynCov,
covariancePrevious
);

return covariance;
}

Expand Down Expand Up @@ -128,6 +129,7 @@ class CoreKalmanFilter {

const predicted = new State({mean, covariance, index});
this.logger.debug('Prediction done', predicted);

return predicted;
}
/**
Expand Down Expand Up @@ -167,13 +169,17 @@ class CoreKalmanFilter {
*/

getCorrectedCovariance(options) {
const {predicted} = options;
const getValueOptions = Object.assign({}, {index: predicted.index}, options);
const identity = getIdentity(predicted.covariance.length);
const stateProj = this.getValue(this.observation.stateProjection, getValueOptions);
const optimalKalmanGain = this.getGain({predicted, stateProjection: stateProj});
const {predicted, optimalKalmanGain, stateProjection} = options;
const identity = getIdentity(predicted.covariance.length);
if(!stateProjection){
const getValueOptions = Object.assign({}, {index: predicted.index}, options);
stateProjection = this.getValue(this.observation.stateProjection, getValueOptions);
}
if(!optimalKalmanGain){
optimalKalmanGain = this.getGain(Object.assign({stateProjection}, options));
}
return matMul(
sub(identity, matMul(optimalKalmanGain, stateProj)),
sub(identity, matMul(optimalKalmanGain, stateProjection)),
predicted.covariance
);
}
Expand All @@ -194,13 +200,13 @@ class CoreKalmanFilter {
}

const getValueOptions = Object.assign({}, {observation, predicted, index: predicted.index}, options);
const stateProj = this.getValue(this.observation.stateProjection, getValueOptions);
const stateProjection = this.getValue(this.observation.stateProjection, getValueOptions);

const optimalKalmanGain = this.getGain({predicted, stateProjection: stateProj});
const optimalKalmanGain = this.getGain(Object.assign({predicted, stateProjection}, options));

const innovation = sub(
observation,
matMul(stateProj, predicted.mean)
matMul(stateProjection, predicted.mean)
);
const mean = add(
predicted.mean,
Expand All @@ -211,7 +217,7 @@ class CoreKalmanFilter {
throw (new TypeError('Mean is NaN after correction'));
}

const covariance = this.getCorrectedCovariance({predicted});
const covariance = this.getCorrectedCovariance(Object.assign({predicted, optimalKalmanGain, stateProjection}, options));
const corrected = new State({mean, covariance, index: predicted.index});
this.logger.debug('Correction done', corrected);
return corrected;
Expand Down
12 changes: 6 additions & 6 deletions lib/kalman-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ class KalmanFilter extends CoreKalmanFilter {
super(Object.assign({}, options, coreOptions));
}

correct({predicted, observation}) {
const coreObservation = arrayToMatrix({observation, dimension: this.observation.dimension});
return super.correct({predicted, observation: coreObservation});
correct(options) {
const coreObservation = arrayToMatrix({observation: options.observation, dimension: this.observation.dimension});
return super.correct(Object.assign({}, options, {observation: coreObservation}));
}

/**
Expand All @@ -106,9 +106,9 @@ class KalmanFilter extends CoreKalmanFilter {
*@returns {Array.<Number>} the mean of the corrections
*/

filter({previousCorrected, observation}) {
const predicted = super.predict({previousCorrected});
return this.correct({predicted, observation});
filter(options) {
const predicted = super.predict(options);
return this.correct(Object.assign({}, options, {predicted}));
}

/**
Expand Down

0 comments on commit 6a4b19a

Please sign in to comment.