kalman_filter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/kalman_filter.rb +59 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 41d63902ccc5b6ca64c0c9087b2603370704b688
|
4
|
+
data.tar.gz: e40bfa7caf38d1eb8040cabd9868992807a06de5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 208467abcba8e3b36ea1c0982edac1b4fcccb5d17491b93e9ebe0b527314137519482c463922180b074b14c4bcd1861be26ae0a08b8186faf093880361cf1489
|
7
|
+
data.tar.gz: ce9bf2e4f39950f14066227ce2cac7a28adb15c426ca9323d241b7a07f392da7ed1604b6a2768d88c095e64f8f34a156794bef01c9f0fe14498426f7488477d9
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class KalmanFilter
|
2
|
+
attr_writer :process_noise, :measurement_noise, :state_vector,
|
3
|
+
:control_vector, :measurement_vector
|
4
|
+
|
5
|
+
attr_reader :value
|
6
|
+
|
7
|
+
attr_accessor :measurement
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
@covariance = nil
|
11
|
+
@value = nil
|
12
|
+
@measurement = nil
|
13
|
+
|
14
|
+
# setup defaults
|
15
|
+
@process_noise = (options[:process_noise] || 1.0)
|
16
|
+
@measurement_noise = (options[:measurement_noise] || 1.0)
|
17
|
+
@state_vector = (options[:state_vector] || 1.0)
|
18
|
+
@control_vector = (options[:control_vector] || 0.0)
|
19
|
+
@measurement_vector = (options[:measurement_vector] || 1.0)
|
20
|
+
end
|
21
|
+
|
22
|
+
def measurement=(new_measurement, control = 0.0)
|
23
|
+
@measurement = new_measurement
|
24
|
+
|
25
|
+
if value.nil?
|
26
|
+
self.value = new_measurement / measurement_vector
|
27
|
+
self.covariance = 1.0 / measurement_vector
|
28
|
+
else
|
29
|
+
|
30
|
+
# Prediction
|
31
|
+
predicted_x =
|
32
|
+
(state_vector * value) + (control_vector * control)
|
33
|
+
predicted_covariance =
|
34
|
+
((state_vector * covariance) * state_vector) + process_noise
|
35
|
+
|
36
|
+
# Gain
|
37
|
+
kalman_gain = predicted_covariance * measurement_vector * (1 /
|
38
|
+
((measurement_vector * predicted_covariance *
|
39
|
+
measurement_vector) + measurement_noise))
|
40
|
+
|
41
|
+
# Correction
|
42
|
+
covariance = predicted_covariance -
|
43
|
+
(kalman_gain * measurement_vector * predicted_covariance)
|
44
|
+
|
45
|
+
self.value = predicted_x + kalman_gain *
|
46
|
+
(new_measurement - (measurement_vector * predicted_x))
|
47
|
+
end
|
48
|
+
|
49
|
+
value
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
attr_writer :value
|
55
|
+
attr_reader :process_noise, :measurement_noise, :state_vector,
|
56
|
+
:control_vector, :measurement_vector
|
57
|
+
|
58
|
+
attr_accessor :covariance
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kalman_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joseph Viscomi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |-
|
14
|
+
Noisy sensor data, approximations in the equations that
|
15
|
+
describe the system evolution, and external factors that are not accounted
|
16
|
+
for all place limits on how well it is possible to determine the system's
|
17
|
+
state. The Kalman filter deals effectively with the uncertainty due to
|
18
|
+
noisy sensor data and to some extent also with random external factors.
|
19
|
+
The Kalman filter produces an estimate of the state of the system as an
|
20
|
+
average of the system's predicted state and of the new measurement using a
|
21
|
+
weighted average. The purpose of the weights is that values with better
|
22
|
+
(i.e., smaller) estimated uncertainty are "trusted" more. The weights
|
23
|
+
are calculated from the covariance, a measure of the estimated uncertainty
|
24
|
+
of the prediction of the system's state. The result of the weighted
|
25
|
+
average is a new state estimate that lies between the predicted and
|
26
|
+
measured state, and has a better estimated uncertainty than either alone.
|
27
|
+
This process is repeated at every time step, with the new estimate and its
|
28
|
+
covariance informing the prediction used in the following iteration. This
|
29
|
+
means that the Kalman filter works recursively and requires only the last
|
30
|
+
"best guess", rather than the entire history, of a system's state to
|
31
|
+
calculate a new state.
|
32
|
+
email: jjviscomi@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/kalman_filter.rb
|
38
|
+
homepage: https://github.com/jjviscomi/kalman-filter
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 2.5.1
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: A Ruby implimentation of a Kalman Filter, linear quadratic estimator.
|
62
|
+
test_files: []
|