Kalman Filter in Javascript

Overview

Kalman

Kalman filter for Javascript.

Dependencies

The module requires a sylvester.js (https://github.com/jcoglan/sylvester) compatible matrix and vector manipulation module.

Usage

Using the Kalman Filter module is very simple:

">
<script type="text/javascript" src="sylvester.src.js">script>
<script type="text/javascript" src="../kalman.js">script>

<script type="text/javascript">
var x_0 = $V([-10]);
var P_0 = $M([[1]]);
var F_k=$M([[1]]);
var Q_k=$M([[0]]);
var KM = new KalmanModel(x_0,P_0,F_k,Q_k);

var z_k = $V([1]);
var H_k = $M([[1]]);
var R_k = $M([[4]]);
var KO = new KalmanObservation(z_k,H_k,R_k);

for (var i=0;i<200;i++){
  z_k = $V([0.5+Math.random()]);
  KO.z_k=z_k;
  KM.update(KO);
  console.log(JSON.stringify(KM.x_k.elements));
}
script>

License

(The MIT License)

Copyright (c) 2007-2012 James Coglan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments
  • Example not working

    Example not working

    Hi,

    I get two errors when trying the example, first is: Uncaught ReferenceError: R_k is not defined

    That one was easy enough to fix by changing line 59 to: function KalmanObservation(z_k,H_k,R_k){

    But then there is the following error: Uncaught TypeError: Cannot read property '1' of undefined

    it traces to line 49: this.K_k = this.P_k_k_.x(o.H_k.transpose().x(this.S_k.inverse()));//Optimal Kalman gain

    I tried to trace it back but no luck yet, thoughts?

    opened by imp-guru 3
  • Q (process noise covariance matrix) not used in prediction

    Q (process noise covariance matrix) not used in prediction

    In the KalmanModel.update() Predict part, the calculation for P (P = FPF(T) + Q) misses the Q part (which is not used anywhere else). I think this.P_k_k_ = this.F_k.x(this.P_k_.x(this.F_k.transpose())); should be this.P_k_k_ = this.F_k.x(this.P_k_.x(this.F_k.transpose())).add(this.Q_k);

    In my tests this modification fixed the problem that new measurements did not really have much effect.

    opened by willemvdg 2
  • Fixed references

    Fixed references

    Fixing several references in the Kalman Model. (mostly adding a this. or an o. in front of the variables.) Also, fixed a vector addition to use the add method.

    opened by cbupp 2
  • Can I simply split update func to a predicate func and a update func?

    Can I simply split update func to a predicate func and a update func?

    like below, for predicate <--> measurement loop: KalmanModel.prototype.predicate = function()
    { //init
    this.x_k_ = this.x_k;
    this.P_k_ = this.P_k;
    //Predict
    this.x_k_k_ = this.F_k.x(this.x_k_);
    this.P_k_k_ = this.F_k.x(this.P_k_.x(this.F_k.transpose())).add(this.Q_k);
    } KalmanModel.prototype.update = function(o)
    { this.I = Matrix.I(this.P_k.rows());
    this.y_k = o.z_k.subtract(o.H_k.x(this.x_k_k_)); //observation residual
    this.S_k = o.H_k.x(this.P_k_k_.x(o.H_k.transpose())).add(o.R_k); //residual covariance
    this.K_k = this.P_k_k_.x(o.H_k.transpose().x(this.S_k.inverse())); //Optimal Kalman gain
    this.x_k = this.x_k_k_.add(this.K_k.x(this.y_k));
    this.P_k = this.I.subtract(this.K_k.x(o.H_k)).x(this.P_k_k_);
    }

    opened by navigator117 0
  • Converting to bower component

    Converting to bower component

    I've made your library into a bower component so that its easier for other people to install and would like to make a merge request. At the moment, though, your test isn't passing.

    https://github.com/htmlfusion/kalman

    Despite adding the same version of sylvester (0.1.3) as is used in your test, there appears to be a slight different between them. The difference lies in the determinant function.

    The version installed in your test directory looks like so:

      // Returns the determinant for square matrices
      determinant: function() {
        if (this.elements.length === 0) { return 1; }
        if (!this.isSquare()) { return null; }
        var M = this.toRightTriangular();
        var det = M.elements[0][0], n = M.elements.length;
        for (var i = 1; i < n; i++) {
          det = det * M.elements[i][i];
        }
        return det;
      },
    

    While the version installed by bower looks like:

      // Returns the determinant for square matrices
      determinant: function() {
        if (!this.isSquare()) { return null; }
        var M = this.toRightTriangular();
        var det = M.elements[0][0], n = M.elements.length - 1, k = n, i;
        do { i = k - n + 1;
          det = det * M.elements[i][i];
        } while (--n);
        return det;
      },
    

    This is the only difference between the two files and results in the following error.

     Uncaught TypeError: Cannot read property '1' of undefined
    sylvester.src.js:586
    Matrix.determinantsylvester.src.js:595 
    Matrix.isSingularsylvester.src.js:643
    Matrix.inversekalman.js:49
    KalmanModel.updatetest.html:23 (anonymous function)
    
    opened by FreakTheMighty 2
  • Determine matrices to be used as arguments

    Determine matrices to be used as arguments

    I can see both the model and update functions require a Vector and a set of Matrices as parameters.

    Would it be possible to get a brief explanation of how one would derive these values? I am using the .alpha, .beta and .gamma readings from the deviceOrientation API which come out as floating point numbers. Am I right in thinking these form the 3 parts of the first Vector argument? What is the purpose of the remaining Matrices.

    I put a question up on StackOverflow hoping someone might be able to explain. Any help would be gratefully received

    https://stackoverflow.com/questions/25744984/implement-a-kalman-filter-to-smooth-data-from-deviceorientation-api

    opened by mairead 4
Owner
Itamar Weiss
Itamar Weiss
A javascript plugin to filter elements from a "masonry" grid.

Isolde Isolde is a lightweight, flexible, and responsive javascript plugin allow you to filter elements from a "masonry" grid. Quick start Install Thi

Tristan BOULANGER 24 Oct 13, 2022
A simple Todo App with check complete function and filter todo. Made with React and TypeScript.

A simple Todo App with check complete function and filter todo. Made with React and TypeScript. The app can Add, Delete and Edit todo. Todo can be sorted by Complete or Active.

Bao Nguyen 9 Dec 6, 2022
Filter sale 1K gần bạn

Filter sale 1K gần bạn Tại sao lại có cái này? Đôi khi bạn sẽ nhận được (hoặc săn được) voucher freeship đơn 0đ của shopee (10k hoặc 25k), và phí ship

Kang Hidro 46 Nov 25, 2022
List all browsers compat data from MDN and filter with browserlist.

mdn-compat-browserlist List all browsers compat data from MDN and filter with browserlist. Features Support filter all browserlist queries List all br

SerKo 2 Apr 17, 2022
Search/Filter beer for beerpong

Beer for Beerpong ?? You can search beer to play beer pong ?? ?? You sink it, they drink it LINK Technologies ?? React React Router Dom Beer Animation

Merve Karabulut 11 Feb 24, 2022
💻 Countries Web is a web application that lets you view data for all the countries in the world and filter them by country name and continent.

?? Countries Web View Demo This is the Countries Web, a web application that lets you view data for all the countries in the world and filter them by

João Gabriel 5 Jun 23, 2022
jQuery filter for all purposes.

jQuery EasyFilter The easiest way to filter anything! How to use 1. Include jQuery (ignore this if you have already included on the page). <script src

Rafael F. da Silva 2 Jun 17, 2022
Easy view and filter all follows and following. Auto update by GitHub Action.

?? List All Followers And Following Easy view and filter all follows and following. Auto update by GitHub Action. Since GitHub's default follows and f

Yuri 10 Dec 28, 2022
This is my to-do list website built with html, css and JavaScript. In this project I used Webpack to bundle JavaScript and ES6 modules to write modular JavaScript.

To-Do-List App This is my to-do list website built with html, css and JavaScript. In this project I used Webpack to bundle JavaScript and ES6 modules

Samuel Mwape 18 Sep 20, 2022
Reference for How to Write an Open Source JavaScript Library - https://egghead.io/series/how-to-write-an-open-source-javascript-library

Reference for How to Write an Open Source JavaScript Library The purpose of this document is to serve as a reference for: How to Write an Open Source

Sarbbottam Bandyopadhyay 175 Dec 24, 2022
Open Source projects are a project to improve your JavaScript knowledge with JavaScript documentation, design patterns, books, playlists.

It is a project I am trying to list the repos that have received thousands of stars on Github and deemed useful by the JavaScript community. It's a gi

Cihat Salik 22 Aug 14, 2022
Javascript-testing-practical-approach-2021-course-v3 - Javascript Testing, a Practical Approach (v3)

Javascript Testing, a Practical Approach Description This is the reference repository with all the contents and the examples of the "Javascript Testin

Stefano Magni 2 Nov 14, 2022
Navigation-Menu-Javascript - A simple Navbar navigation using vanilla javascript, to change links to the active link when clicked.

Navigation-Menu-Javascript A simple Navbar navigation using vanilla javascript, to change links to the active link when clicked. Desktop view Mobile v

Ellis 2 Feb 16, 2021
Ping.js is a small and simple Javascript library for the browser to "ping" response times to web servers in Javascript

Ping.js Ping.js is a small and simple Javascript library for the browser to "ping" response times to web servers in Javascript! This is useful for whe

Alfred Gutierrez 353 Dec 27, 2022
MenuSlider-Javascript - How to create a menu slider with vanilla javascript

MenuSlider-Javascript How to create a menu slider with vanilla javascript Instal

Tarokh Mohammadi 1 Feb 8, 2022
Simple Library implemented using HTML, CSS and JavaScript. This is a simple implementation of JavaScript Modules of ES6.

Awesome-books A single page project with the porpuse of storing books' titles and authors. Built With CSS, HTML & Javascript. How to run in your local

Saadat Ali 7 Feb 21, 2022
This is a project that allows users to add/remove books from a list. we accomplish this by using a JavaScript object. Built with JavaScript, Html and CSS.

Awesome-book This is a project that allows users to add/remove book from a list. we accomplish this by usig javascript oject. Built With HTML5 CSS3 Ja

Juan Fco. Rosario Suli 6 May 27, 2022
JavaScript project for the Leaderboard list app, using Webpack and ES6 features, notably modules. this app consume the Leaderboard API using JavaScript async and await and add some styling.

Leaderboard Project JavaScript project for the Leaderboard list app, using Webpack and ES6 features, notably modules. this app consume the Leaderboard

bizimungu pascal 4 May 20, 2022
Custom alert box using javaScript and css. This plugin will provide the functionality to customize the default JavaScript alert box.

customAlertBoxPlugin Custom Alert Box Plugin Using JavaScript and CSS Author: Suraj Aswal Must Include CSS Code/Default Custom Alert Box Class: /* mus

Suraj Aswal 17 Sep 10, 2022