-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkalman.py
58 lines (42 loc) · 1.56 KB
/
kalman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
from utils import gaussian
class Gaussian(object):
def __init__(self, mu, sigma):
self.mu = float(mu)
self.sigma = float(sigma)
def vectorize(self, xlims, step=0.1):
x_array = np.arange(*xlims, step)
y_array = [100 * gaussian(x, self.mu, self.sigma) for x in x_array]
return x_array, y_array
def bayesian_update(self, other):
sigma_sum = self.sigma**2 + other.sigma**2
self.mu = ((self.sigma**2) * other.mu + (other.sigma**2) * self.mu) / sigma_sum
self.sigma = np.sqrt(((self.sigma * other.sigma)**2) / sigma_sum)
def __float__(self):
return float(self.mu)
class Reading(Gaussian):
def __init__(self, sensor, truth, timestamp=None):
self.sensor = sensor
self.timestamp = timestamp
self.truth = truth
self.value = sensor.read(truth)
self.color = sensor.color
self.mu = sensor.predictor(self.value)
self.sigma = sensor.predictor_sigma
class Estimate(Gaussian):
def __init__(self, color='purple'):
self.reading_vector = []
self.mu = None
self.sigma = None
self.color = color
def add_reading(self, reading):
self.reading_vector.append(reading)
self.update(reading)
def reorder(self):
self.reading_vector.sort(key=lambda x: x.timestamp)
def update(self, reading):
if self.mu is None or self.sigma is None:
self.mu = reading.mu
self.sigma = reading.sigma
else:
self.bayesian_update(reading)