Skip to content

Commit 3e0fd16

Browse files
committed
particle sim ready with noise induced height
1 parent 7d4faf5 commit 3e0fd16

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

MyCode/Particle_Filter/1D.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import numpy as np
22
import matplotlib.pyplot as plt
33
from numpy.polynomial import Chebyshev as T
4+
from utils import create_toy_data
45

56
if __name__ == "__main__":
67
velocity = 1 # m/s
7-
DT = 1 # sec
8+
DT = 5 # sec
89
start_pos = 0 # meters
910
time = 0.0
1011

11-
plane_height = 5
12+
plane_height = 10
1213

1314
pos = start_pos
1415

@@ -18,45 +19,62 @@
1819
y3 = T.basis(4)(x) + T.basis(5)(x)
1920

2021
ly = np.concatenate((y2, y1, y3))
21-
ly = ly + abs(np.min(ly))
22+
ly_min = abs(np.min(ly))
23+
ly = ly + (ly_min*2)
24+
2225
lx = np.arange(len(ly))
2326

27+
# std
28+
std = 1
29+
y_sine_noise = create_toy_data(ly, std)
30+
2431
SIM_TIME = len(ly)
2532

2633
show_animation = True
2734

2835
fig, axs = plt.subplots(1, 1, figsize=(14, 5))
29-
axs.set_axisbelow(True)
36+
ax = axs
37+
ax.set_axisbelow(True)
3038

31-
while SIM_TIME >= time:
32-
time += DT
33-
print(time, ly[int(time)], end='\r')
34-
pos += velocity * DT
39+
# while SIM_TIME > time:
40+
for pos in range(0, len(lx), DT):
3541

3642
if show_animation:
3743
plt.cla()
3844
# for stopping simulation with the esc key.
3945
plt.gcf().canvas.mpl_connect('key_release_event',
4046
lambda event: [exit(0) if event.key == 'escape' else None])
4147

42-
axs.grid(ls='--')
43-
axs.plot(lx, ly, c='g')
44-
axs.fill_between(lx, ly, color="green", alpha=0.6)
48+
ax.grid(ls='--')
49+
ax.plot(lx, ly, c='g')
50+
ax.fill_between(lx, ly, color="green", alpha=0.6)
51+
52+
# std range
53+
ax.fill_between(lx, ly - std, ly + std,
54+
color="gray", label="std.", alpha=0.5)
55+
56+
# noise within std
57+
58+
ax.scatter(lx, y_sine_noise, c='b', marker='*', lw=0.5)
4559

4660
# Plot plane route
47-
axs.axhline(y=plane_height, xmin=0, xmax=1,
48-
c='r', ls='--', lw=1)
61+
ax.axhline(y=plane_height, xmin=0, xmax=1,
62+
c='r', ls='--', lw=1)
4963
# plane current pos
50-
axs.scatter(pos, plane_height, c='r', lw=3)
64+
ax.scatter(pos, plane_height, c='r', lw=3, marker='>')
5165

5266
# distance to current height
53-
axs.scatter(pos, ly[int(time)], c='k', lw=3)
67+
68+
ax.scatter(pos, ly[pos], c='k', lw=3)
5469

5570
# settings
56-
axs.grid(True)
71+
ax.grid(True)
5772
# plt.xlim(-1, SIM_TIME)
58-
axs.set_xlabel('X Position (m)')
59-
axs.set_ylabel('Height (m)')
60-
axs.set_title('Simulation time: {:.2f} seconds'.format(time))
73+
ax.set_xlabel('X Position (m)')
74+
ax.set_ylabel('Height (m)')
75+
ax.set_title('Simulation time: {:.1f} seconds'.format(pos+DT))
6176

6277
plt.pause(0.001)
78+
79+
if show_animation:
80+
plt.show()

MyCode/Particle_Filter/poly.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
33
from numpy.polynomial import Chebyshev as T
4+
from utils import create_toy_data
5+
46

57
if __name__ == "__main__":
68

@@ -30,15 +32,24 @@
3032
ax.grid(ls='--')
3133

3234
ly = np.concatenate((y2, y1, y3))
33-
ly = ly + abs(np.min(ly))
35+
ly_min = abs(np.min(ly))
36+
ly = ly + (ly_min*2)
3437
lx = np.arange(len(ly))
3538

3639
# plot mountain
3740
ax.plot(lx, ly, c='g')
3841
ax.fill_between(lx, ly, color="green", alpha=0.6)
3942

4043
# Plot plane route
41-
ax.axhline(y=5, xmin=0, xmax=1, c='r', ls='--', lw=0.75)
44+
ax.axhline(y=10, xmin=0, xmax=1, c='r', ls='--', lw=0.75)
45+
46+
# std
47+
std = 1
48+
ax.fill_between(lx, ly - std, ly + std,
49+
color="r", label="std.", alpha=0.5)
50+
51+
y_sine_noise = create_toy_data(ly, std)
52+
ax.scatter(lx, y_sine_noise, c='b', marker='*', lw=0.5)
4253

4354
ax.set_xlabel('X position (m)')
4455
ax.set_ylabel('Height (m)')

MyCode/Particle_Filter/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
3+
np.random.seed(123)
4+
5+
def create_toy_data(ly, std):
6+
t = ly + np.random.normal(scale=std, size=ly.shape)
7+
return t
8+
9+
10+
if __name__ == "__main__":
11+
pass

0 commit comments

Comments
 (0)