-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdoa.py
242 lines (203 loc) · 8.25 KB
/
tdoa.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# ____ ____ _ __ __ ____ ___
# | _ \| _ \ / \ | \/ |/ ___/ _ \
# | | | | |_) | / _ \ | |\/| | | | | | |
# | |_| | _ < / ___ \| | | | |__| |_| |
# |____/|_| \_\/_/ \_\_| |_|\____\___/
# research group
# dramco.be/
#
# KU Leuven - Technology Campus Gent,
# Gebroeders De Smetstraat 1,
# B-9000 Gent, Belgium
#
# File: tdoa.py
# Created: 2018-01-31
# Author: Geoffrey Ottoy
# Version: 0.1
#
# Description: Functions for TDOA localization
# some more info
#
#
import numpy as np
import loclib.util as loctools
# compute a 2 dimensional position
def compute_xy(ref_nodes, d_differences, min_r_error=0.001, max_nr_steps=10):
position = np.array([np.nan, np.nan], dtype=float)
# Type check for 'ref_nodes'
if type(ref_nodes) is np.ndarray:
# get dimensions
rshape = ref_nodes.shape
else:
print("Error: Argument 'ref_nodes' should be a matrix (np.matrix).")
return position # [NaN, NaN]
# Type check for 'd_differences'
if type(d_differences) is np.ndarray:
# get dimensions
dshape = d_differences.shape
else:
print("Error: Argument 'd_differences' should be a matrix (np.matrix).")
return position # [NaN, NaN]
# Verify that first value in d_differences is 0
if d_differences[0] != 0:
print("Error: Differences need to be referenced to first the node,")
print(" i.e., the first distance difference needs to be 0.")
return position # [NaN, NaN]
# Check d_differences dimensions
if len(dshape) != 1:
print("Error: Argument 'd_differences' should be an n-element array.")
return position # [NaN, NaN]
else:
drows = dshape[0]
# Check ref_nodes dimensions
if len(rshape) != 2:
print("Error: Argument 'ref_nodes' needs to be an n-by-2 matrix.")
return position # [NaN, NaN]
else:
rrows = rshape[0]
rcols = rshape[1]
if rcols != 2:
print("Error: Argument 'ref_nodes' needs to be an n-by-2 matrix.")
return position # [NaN, NaN]
if rrows != drows:
print("Error: Arguments 'ref_nodes' and 'd_differences' should have an equal number of rows.")
return position # [NaN, NaN]
# Make sure enough data are provided to run the TDOA algorithm
if drows < 3:
print("Error: At least 3 reference nodes are required to compute a position.")
return position # [NaN, NaN]
# Check if all reference nodes are colinear
if loctools.colinear(ref_nodes):
print("Warning: Reference nodes are located on the same (straight) line."
"This might impact the algorithm's operation.")
# Now we can do the magic (i.e., run the algorithm)
# Create necessary matrices
x1 = ref_nodes[0, 0]
y1 = ref_nodes[0, 1]
# -------------------------------------------------
# Initial estimation
# -------------------------------------------------
# we start 'in the middle'
p_est = np.array([0, 0], dtype=float)
p_est[0] = ref_nodes[:, 0].mean()
p_est[1] = ref_nodes[:, 1].mean()
# -------------------------------------------------
# Iterative phase
# -------------------------------------------------
step = 0
r_error = 100
while (r_error > min_r_error) and (step < max_nr_steps):
step = step + 1
r_est = loctools.distance_between_2d(p_est, ref_nodes)
a1 = (-x1 + p_est[0]) / r_est[0]
b1 = (-y1 + p_est[1]) / r_est[0]
ai = (-ref_nodes[:, 0] + p_est[0]) / r_est # division is element-by-element
bi = (-ref_nodes[:, 1] + p_est[1]) / r_est # division is element-by-element
a_mat = np.column_stack(((a1 - ai), (b1 - bi)))
b_mat = d_differences - r_est + r_est[0]
# least-squares estimation of the positioning error
step1 = np.matmul(a_mat.T, a_mat)
try:
step2 = np.linalg.inv(step1)
except np.linalg.LinAlgError as err:
print(err)
return position # [NaN, NaN]
esti = np.matmul(np.matmul(step2, a_mat.T), b_mat)
if np.isnan(esti).any():
return position # [NaN, NaN]
# see how far we are still off
r_error = abs(esti.mean())
# update location guess based on error
p_est[0] -= esti[0]
p_est[1] -= esti[1]
return p_est
# compute a 3 dimensional position
def compute_xyz(ref_nodes, d_differences, min_r_error=0.001, max_nr_steps=10):
position = np.array([np.nan, np.nan, np.nan], dtype=float)
# Type check for 'ref_nodes'
if type(ref_nodes) is np.ndarray:
# get dimensions
rshape = ref_nodes.shape
else:
print("Error: Argument 'ref_nodes' should be a matrix (np.matrix).")
return position # [NaN, NaN]
# Type check for 'd_differences'
if type(d_differences) is np.ndarray:
# get dimensions
dshape = d_differences.shape
else:
print("Error: Argument 'd_differences' should be a matrix (np.matrix).")
return position # [NaN, NaN]
# Verify that first value in d_differences is 0
if d_differences[0] != 0:
print("Error: Differences need to be referenced to first the node,")
print(" i.e., the first distance difference needs to be 0.")
return position # [NaN, NaN]
# Check d_differences dimensions
if len(dshape) != 1:
print("Error: Argument 'd_differences' should be an n-element array.")
return position # [NaN, NaN]
else:
drows = dshape[0]
# Check ref_nodes dimensions
if len(rshape) != 2:
print("Error: Argument 'ref_nodes' needs to be an n-by-3 matrix.")
return position # [NaN, NaN]
else:
rrows = rshape[0]
rcols = rshape[1]
if rcols != 3:
print("Error: Argument 'ref_nodes' needs to be an n-by-3 matrix.")
return position # [NaN, NaN]
if rrows != drows:
print("Error: Arguments 'ref_nodes' and 'd_differences' should have an equal number of rows.")
return position # [NaN, NaN]
# Make sure enough data are provided to run the TDOA algorithm
if drows < 4:
print("Error: At least 4 reference nodes are required to compute a position.")
return position # [NaN, NaN]
if loctools.coplanar(ref_nodes):
print("Warning: Reference nodes are located in the same plane. This might impact the algorithm's operation.")
# Now we can do the magic (i.e., run the algorithm)
# Create necessary matrices
x1 = ref_nodes[0, 0]
y1 = ref_nodes[0, 1]
z1 = ref_nodes[0, 2]
# -------------------------------------------------
# Initial estimation
# -------------------------------------------------
# we start 'in the middle'
p_est = np.array([ref_nodes[:, 0].mean(), ref_nodes[:, 1].mean(), ref_nodes[:, 2].mean()], dtype=float)
# -------------------------------------------------
# Iterative phase
# -------------------------------------------------
step = 0
r_error = 100
while (r_error > min_r_error) and (step < max_nr_steps):
step = step + 1
r_est = loctools.distance_between_3d(p_est, ref_nodes)
a1 = (-x1 + p_est[0]) / r_est[0]
b1 = (-y1 + p_est[1]) / r_est[0]
c1 = (-z1 + p_est[2]) / r_est[0]
ai = (-ref_nodes[:, 0] + p_est[0]) / r_est # division is element-by-element
bi = (-ref_nodes[:, 1] + p_est[1]) / r_est # division is element-by-element
ci = (-ref_nodes[:, 2] + p_est[2]) / r_est # division is element-by-element
a_mat = np.column_stack(((a1 - ai), (b1 - bi), (c1 - ci)))
b_mat = d_differences - r_est + r_est[0]
# least-squares estimation of the positioning error
step1 = np.matmul(a_mat.T, a_mat)
try:
step2 = np.linalg.inv(step1)
except np.linalg.LinAlgError as err:
print(err)
return position # [NaN, NaN]
esti = np.matmul(np.matmul(step2, a_mat.T), b_mat)
if np.isnan(esti).any():
return position # [NaN, NaN]
# see how far we are still off
r_error = abs(esti.mean())
# update location guess based on error
p_est[0] -= esti[0]
p_est[1] -= esti[1]
p_est[2] -= esti[2]
return p_est