Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sun Feb 8 09:34:36 2015
4
5
@author: rlabbe
6
"""
7
8
import numpy as np
9
import matplotlib.pyplot as plt
10
11
from numpy import array, asarray
12
from numpy.random import randn
13
import math
14
from filterpy.kalman import UnscentedKalmanFilter as UKF
15
16
17
18
class RadarSim(object):
19
""" Simulates the radar signal returns from an object flying
20
at a constant altityude and velocity in 1D.
21
"""
22
23
def __init__(self, dt, pos, vel, alt):
24
self.pos = pos
25
self.vel = vel
26
self.alt = alt
27
self.dt = dt
28
29
def get_range(self):
30
""" Returns slant range to the object. Call once for each
31
new measurement at dt time from last call.
32
"""
33
34
# add some process noise to the system
35
self.vel = self.vel + .1*randn()
36
self.alt = self.alt + .1*randn()
37
self.pos = self.pos + self.vel*self.dt
38
39
# add measurment noise
40
err = self.pos * 0.05*randn()
41
slant_dist = math.sqrt(self.pos**2 + self.alt**2)
42
43
return slant_dist + err
44
45
46
47
dt = 0.05
48
49
50
def hx(x):
51
return (x[0]**2 + x[2]**2)**.5
52
pass
53
54
55
56
def fx(x, dt):
57
result = x.copy()
58
result[0] += x[1]*dt
59
return result
60
61
62
63
64
f = UKF(3, 1, dt= dt, hx=hx, fx=fx, kappa=1)
65
radar = RadarSim(dt, pos=-1000., vel=100., alt=1000.)
66
67
f.x = array([0, 90, 1005])
68
f.R = 0.1
69
f.Q *= 0.002
70
71
72
73
74
xs = []
75
track = []
76
77
for i in range(int(20/dt)):
78
z = radar.get_range()
79
track.append((radar.pos, radar.vel, radar.alt))
80
81
f.predict()
82
f.update(array([z]))
83
84
xs.append(f.x)
85
86
87
xs = asarray(xs)
88
track = asarray(track)
89
time = np.arange(0,len(xs)*dt, dt)
90
91
plt.figure()
92
plt.subplot(311)
93
plt.plot(time, track[:,0])
94
plt.plot(time, xs[:,0])
95
plt.legend(loc=4)
96
plt.xlabel('time (sec)')
97
plt.ylabel('position (m)')
98
99
100
plt.subplot(312)
101
plt.plot(time, track[:,1])
102
plt.plot(time, xs[:,1])
103
plt.legend(loc=4)
104
plt.xlabel('time (sec)')
105
plt.ylabel('velocity (m/s)')
106
107
plt.subplot(313)
108
plt.plot(time, track[:,2])
109
plt.plot(time, xs[:,2])
110
plt.ylabel('altitude (m)')
111
plt.legend(loc=4)
112
plt.xlabel('time (sec)')
113
plt.ylim((900,1600))
114
plt.show()
115
116