kalman

package module
v0.0.0-...-10753ec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2020 License: MIT Imports: 4 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidProcNoise = fmt.Errorf("invalid process noise arguments")

ErrInvalidProcNoise is returned when we can't compute process noise.

Functions

This section is empty.

Types

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter is a Kalman filter.

func NewFilter

func NewFilter(d *ProcessNoise) (*Filter, error)

NewFilter creates and returns a new Kalman filter.

func (*Filter) Observe

func (f *Filter) Observe(td float64, ob *Observed) error

Observe processes a single act of observation, td is the time since last update.

type GeoEstimated

type GeoEstimated struct {
	Lat, Lng, Altitude float64
	Speed              float64
	Direction          float64
	HorizontalAccuracy float64
}

GeoEstimated contains estimated location, obtained by processing several observed locations.

type GeoFilter

type GeoFilter struct {
	// contains filtered or unexported fields
}

GeoFilter is a Kalman filter that deals with geographic coordinates and altitude.

func NewGeoFilter

func NewGeoFilter(d *GeoProcessNoise) (*GeoFilter, error)

NewGeoFilter creates and returns a new GeoFilter.

func (*GeoFilter) Estimate

func (g *GeoFilter) Estimate() *GeoEstimated

Estimate returns the best location estimate.

func (*GeoFilter) Observe

func (g *GeoFilter) Observe(td float64, ob *GeoObserved) error

type GeoObserved

type GeoObserved struct {
	Lat, Lng, Altitude float64 // Geographical coordinates (in degrees) and latitude.
	Speed              float64 // Speed, in meters per second.
	SpeedAccuracy      float64 // Speed accuracy, in meters per second.
	Direction          float64 // Travel direction, in degrees from North, 0 to 360 range.
	DirectionAccuracy  float64 // Direction accuracy, in degrees.
	HorizontalAccuracy float64 // Horizontal accuracy, in meters.
	VerticalAccuracy   float64 // Vertical accuracy, in meters.
}

GeoObserved represents a single observation, in geographical coordinates and altitude.

type GeoProcessNoise

type GeoProcessNoise struct {
	// Base latitude to use for computing distances.
	BaseLat float64
	// DistancePerSecond is the expected random walk distance per second.
	DistancePerSecond float64
	// SpeedPerSecond is the expected speed per second change.
	SpeedPerSecond float64
}

GeoProcessNoise is used to initialize the process noise.

type Observed

type Observed struct {
	X, Y, Z       float64 // Coordinates.
	VX, VY, VZ    float64 // Speed.
	XA, YA, ZA    float64 // Accuracy (coordinates).
	VXA, VYA, VZA float64 // Accuracy (speed).
}

Observed represents a single observation.

type ProcessNoise

type ProcessNoise struct {
	SX, SY, SZ    float64 // Random step (coordinates).
	SVX, SVY, SVZ float64 // Random step (speed).
	ST            float64 // Random step (time).
}

ProcessNoise represents process noise.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL