CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91871
1
# -*- coding: utf-8 -*-
2
3
4
import numpy as np
5
from numpy.linalg import norm, inv
6
from numpy.random import randn
7
from numpy import dot
8
9
10
numpy.random.seed(1234)
11
user_pos = np.array([1000, 100]) # d5, D6
12
13
pred_user_pos = np.array([100, 0]) #d7, d8
14
15
16
t_pos = np.asarray([[0, 1000],
17
[0, -1000],
18
[500, 500]], dtype=float)
19
20
21
def transmitter_range(pos, transmitter_pos):
22
""" Compute distance between position 'pos' and the list of positions
23
in transmitter_pos"""
24
25
N = len(transmitter_pos)
26
rng = np.zeros(N)
27
28
diff = np.asarray(pos) - transmitter_pos
29
30
for i in range(N):
31
rng[i] = norm(diff[i])
32
33
return norm(diff, axis=1)
34
35
36
37
38
# compute measurement of where you are with respect to seach sensor
39
40
41
rz= transmitter_range(user_pos, t_pos) # $B21,22
42
43
# add some noise
44
for i in range(len(rz)):
45
rz[i] += randn()
46
47
48
# now iterate on the predicted position
49
pos = pred_user_pos
50
51
52
def hx_range(pos, t_pos, r_est):
53
N = len(t_pos)
54
H = np.zeros((N, 2))
55
for j in range(N):
56
H[j,0] = -(t_pos[j,0] - pos[0]) / r_est[j]
57
H[j,1] = -(t_pos[j,1] - pos[1]) / r_est[j]
58
return H
59
60
61
def lop_ils(zs, t_pos, pos_est, hx, eps=1.e-6):
62
""" iteratively estimates the solution to a set of measurement, given
63
known transmitter locations"""
64
pos = np.array(pos_est)
65
66
converged = False
67
for i in range(20):
68
r_est = transmitter_range(pos, t_pos) #B32-B33
69
print('iteration:', i)
70
#print ('ra1, ra2', ra1, ra2)
71
print()
72
73
H=hx(pos, t_pos, r_est)
74
75
Hinv = inv(dot(H.T, H)).dot(H.T)
76
77
#update position estimate
78
y = zs - r_est
79
print('residual', y)
80
81
Hy = np.dot(Hinv, y)
82
print('Hy', Hy)
83
84
pos = pos + Hy
85
print('pos', pos)
86
87
print()
88
print()
89
90
if max(abs(Hy)) < eps:
91
converged = True
92
break
93
94
return pos, converged
95
96
97
98
print(lop_ils(rz, t_pos, (900,90), hx=hx_range))
99
100
101
102
#####################
103
"""
104
# compute measurement (simulation)
105
rza1, rza2 = transmitter_range(user_pos) # $B21,22
106
107
rza1 += randn()
108
rza2 += randn()
109
110
# now iterate on the predicted position
111
pos = pred_user_pos
112
113
114
for i in range(10):
115
ra1, ra2 = transmitter_range(pos) #B32-B33
116
print('iteration:', i)
117
print ('ra1, ra2', ra1, ra2)
118
print()
119
120
H = np.array([[-(t1_pos[0] - pos[0]) / ra1, -(t1_pos[1] - pos[1]) / ra1],
121
[-(t2_pos[0] - pos[0]) / ra2, -(t2_pos[1] - pos[1]) / ra2]])
122
Hinv = inv(H)
123
124
#update position estimate
125
residual_t1 = rza1 - ra1
126
residual_t2 = rza2 - ra2
127
y = np.array([[residual_t1], [residual_t2]])
128
print('residual', y.T)
129
130
131
Hy = np.dot(Hinv, y)
132
133
pos = pos + Hy[:,0]
134
print('pos', pos)
135
136
print()
137
print()
138
139
if (max(abs(y)) < 1.e-6):
140
break
141
"""
142
143