kalman-filter
Version:
Kalman filter (and Extended Kalman Filter) Multi-dimensional implementation in Javascript
26 lines (21 loc) • 902 B
JavaScript
/**
* @param {Object} opts
* @param {Array.<Array.<Number>>} opts.measures a list of measure, size is LxN L the number of sample, N the dimension
* @param {Array.<Array.<Number>>} opts.averages a list of averages, size is LxN L the number of sample, N the dimension
* @returns {Array.<Array.<Number>>} covariance matrix size is NxN
*/
module.exports = function ({measures, averages}) {
const l = measures.length;
const n = measures[0].length;
if (l === 0) {
throw (new Error('Cannot find covariance for empty sample'));
}
return (new Array(n).fill(1)).map((_, rowIndex) => (new Array(n).fill(1)).map((_, colIndex) => {
const stds = measures.map((m, i) => (m[rowIndex] - averages[i][rowIndex]) * (m[colIndex] - averages[i][colIndex]));
const result = stds.reduce((a, b) => a + b) / l;
if (Number.isNaN(result)) {
throw (new TypeError('result is NaN'));
}
return result;
}));
};