Skip to content

Instantly share code, notes, and snippets.

@m1el
Last active January 4, 2016 17:19
Show Gist options
  • Save m1el/8652840 to your computer and use it in GitHub Desktop.
Save m1el/8652840 to your computer and use it in GitHub Desktop.
function Kallman(config) {
if (!(this instanceof Kallman)) {
return new Kallman(config);
}
for(var k in config) {
if (config.hasOwnProperty(k)) {
this[k] = config[k];
}
}
this.steps = 1;
this.err = this.sigmaModel;
return this;
}
Kallman.prototype = {
x: 0,
sigmaModel: 1,
sigmaSensor: 1,
ajdustSigma: false,
window: 200,
steps: null,
err: null,
model: function() {
return 0;
},
push: function(z) {
var pow = Math.pow,
min = Math.min;
this.err = 1 - pow(this.sigmaModel, 2) / (pow(this.err, 2) + this.sigmaModel + this.sigmaSensor);
var K = pow(this.err, 2) / this.sigmaModel;
this.x = z * K + (this.x + this.model()) * (1 - K);
if (this.adjustSigma) {
var steps = min(this.window, this.steps);
this.sigmaSensor = (this.sigmaSensor * steps + pow(this.x - z, 2)) / (steps + 1);
}
this.steps++;
return this.x;
}
};
var k = Kallman({x: 0, adjustSigma: true}),
a = [];
for(var i = 0; i < 100; i++) {
var z = i + (Math.random() * 10 - 5);
a.push([i, k.push(z)]);
}
console.log(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment