Giter Site home page Giter Site logo

kalman's Introduction

Kalman Filter for Geographical Coordinates in Go

Kalman filter is an algorithm that uses a series of measurements observed over time, containing statistical noise and other inaccuracies, and produces estimates of unknown variables that tend to be more accurate than those based on a single measurement alone, by estimating a joint probability distribution over the variables for each timeframe.

https://en.wikipedia.org/wiki/Kalman_filter

This repo contains a Kalman filter implementation for motion in three dimensions, which takes speed into account but not acceleration. It also contains a specialized implementation to work with geographical coordinates (lattitude, longitude and altitude). This implementation may be used to process GPS measurements from mobile devices to produce a better estimate of the user's position.

How to Use

See the example here:

https://github.com/regnull/kalman/blob/master/example/main.go

Basically, using Kalman filter involves the following steps:

  • Estimate the process noise
  • Create Kalman filter
  • Feed observed values to the filter, one by one
  • Get the estimated location, hopefully with better precision than any of the observed points

Estimate the process noise

Process noise tells the filter how much do we expect the user to move, per second. Knowing this value allows the algorithm to estimate how much of the difference between expected and actual values can be attributed to the actual user motion.

Here, we create the process noise struct:

// Estimate process noise.
processNoise := &kalman.GeoProcessNoise{
    // We assume the measurements will take place at the approximately the
    // same location, so that we can disregard the earth's curvature.
    BaseLat: point.Lat,
    // How much do we expect the user to move, meters per second.
    DistancePerSecond: 1.0,
    // How much do we expect the user's speed to change, meters per second squared.
    SpeedPerSecond: 0.1,
}

You must assign non-zero values to DistancePerSecond and SpeedPerSecond, otherwise you will get an error when creating an instance of the filter.

Create Kalman filter

After you create process noise struct, pass it to NetGeoFilter:

// Initialize Kalman filter.
filter, err = kalman.NewGeoFilter(processNoise)
if err != nil {
    fmt.Printf("failed to initialize Kalman filter: %s\n", err)
    os.Exit(1)
}

You might get an error back if the filter doesn't like your process noise values.

Feed observed values to the filter

GeoFilter.Observe() takes two arguments, the time passed since the last measurement and the new observed value. Easy enough.

// Observe the next data point.
err = filter.Observe(timeDelta, &point)
// Sometimes the filter may return error, although this should not happen under any
// realistic curcumstances.
if err != nil {
    fmt.Printf("error observing data: %s\n", err)
}

Where point contains data like this:

kalman.GeoObserved { 
    Lat:                41.154874,
    Lng:                -73.773139,
    Altitude:           105.0,
    Speed:              0.0,
    SpeedAccuracy:      0.1,
    HorizontalAccuracy: 100.0,
    VerticalAccuracy:   10.0,
}

Get the estimated values

Finally, get the estimated values obtained by processing the observed values.

estimated := filter.Estimate()
fmt.Printf("Estimated lat: %f\n", estimated.Lat)
fmt.Printf("Estimated lng: %f\n", estimated.Lng)
fmt.Printf("Estimated alt: %f\n", estimated.Altitude)
fmt.Printf("Estimated speed: %f\n", estimated.Speed)
fmt.Printf("Estimated horizontal accuracy: %f\n", estimated.HorizontalAccuracy)

License

This library is published under MIT license:

Copyright 2020 Leonid Gorkin

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.

Status

Work in progress.

kalman's People

Contributors

regnull avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.