File: multilateration_test.cpp

package info (click to toggle)
libitpp 4.3.1-14
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (318 lines) | stat: -rw-r--r-- 9,071 bytes parent folder | download | duplicates (6)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*!
 * \file
 * \brief Unit tests for multilateration class
 * \author Bogdan Cristea
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2013  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/itcomm.h>
#include "gtest/gtest.h"

using namespace itpp;
using namespace std;

#ifdef _MSC_VER
#include <float.h>
#define isnan _isnan
#endif

#define BASE_LINE_M 10.0
#define SPHERE_RADIUS_M 450

//uncomment the line below in order to run full tests
//this is needed in order to make sure that the tests pass on non-i386 architectures
//#define FULL_TESTS

static
bool point_eq(const vec &expect, const vec &actual, double eps)
{
  if((3 != length(expect)) || (3 != length(actual))) {
    it_warning("invalid input");
    return false;
  }
  bool out = ((fabs(expect(0) - actual(0)) < eps) && (fabs(expect(1) - actual(1)) < eps) && (fabs(expect(2) - actual(2)) < eps)
              && !isnan(actual(0)) && !isnan(actual(1)) && !isnan(actual(2)));
  if(false == out) {
    cout << "expect " << expect << endl;
    cout << "actual " << actual << endl;
  }
  return out;
}

static
double get_dist(const vec &p0, const vec &p1)
{
  double out = 0.0;
  for(int n = 0; n < 3; ++n) {
    out += (p1[n] - p0[n]) * (p1[n] - p0[n]);
  }
  return sqrt(out);
}

//used for both spheric and hybrid multilateration
static
bool generate_meas(vec &meas, const bvec &method, const mat &bs_pos, const vec &ms_pos)
{
  unsigned int method_len = length(method);
  unsigned int nb_bs = bs_pos.cols();
  bool column = true;
  if(3 == nb_bs) {
    nb_bs = bs_pos.rows();
    column = false;
  }
  if((nb_bs < method_len) || (3 != length(ms_pos))) {
    return false;
  }
  meas.set_size(method_len);
  vec pos(3);
  vec pos_ref(3);
  pos_ref = column ? bs_pos.get_col(0) : bs_pos.get_row(0);
  for(unsigned int k = 0; k < method_len; ++k) {
    if(bin(1) == method[k]) {  /* hyperbolic */
      pos = column ? bs_pos.get_col(k + 1) : bs_pos.get_row(k + 1);
      meas[k] = get_dist(pos, ms_pos) - get_dist(pos_ref, ms_pos);
    }
    else { /* spherical */
      pos = column ? bs_pos.get_col(k) : bs_pos.get_row(k);
      meas[k] = get_dist(pos, ms_pos);
    }
  }
  return true;
}

//used only for hyperbolic multilateration
static
bool generate_meas(mat &meas, const bvec &method, const mat &bs_pos, const vec &ms_pos)
{
  unsigned int k;
  unsigned int i;
  unsigned int method_len = length(method);
  unsigned int nb_bs = bs_pos.cols();
  bool column = true;
  if(3 == nb_bs) {
    nb_bs = bs_pos.rows();
    column = false;
  }
  if((nb_bs < method_len) || (3 != length(ms_pos))) {
    return false;
  }
  meas.set_size(nb_bs, nb_bs);
  vec pos_i(3);
  vec pos_k(3);
  for(k = 0; k < nb_bs; ++k) {
    pos_k = column ? bs_pos.get_col(k) : bs_pos.get_row(k);
    for(i = 0; i < nb_bs; ++i) {
      pos_i = column ? bs_pos.get_col(i) : bs_pos.get_row(i);
      meas(i, k) = get_dist(pos_i, ms_pos) - get_dist(pos_k, ms_pos);
    }
  }
  return true;
}

static
bool get_bs(mat &bs_pos, unsigned int nb_bs, double l)
{
  int n, i, j, k;
  static const double SQRT2M1 = 0.7071067811865475;

  if(8 < nb_bs) {
    it_warning("at most 8 BSs");
    return false;
  }
  n = 0;
  i = j = k = 0;
  bs_pos.set_size(3, nb_bs);
  while((unsigned int)n < nb_bs) {
    bs_pos(0, n) = (0.5 - i) * l;
    bs_pos(1, n) = (0.5 - j) * l;
    bs_pos(2, n++) = (1 - 2 * k) * l * SQRT2M1;
    switch(n) { /*ensure that the first 4 BSs are not on the same plane*/
    case 3:
      i = j = 0;
      k = 1;
      break;
    case 5:
      i = k = 1;
      j = 0;
      break;
    case 4:
      k = 0;
      i = j = 1;
      break;
    default:
      i = (i + 1) % 2;
      if((0 != n) && (0 == n % 2)) {
        j = (j + 1) % 2;
      }
    }
  }
  return true;
}

static
vec get_ms(double radius)
{
  static const double SQRT3M1 = 0.5773502691896258;
  vec out;
  out.set_size(3);
  for(int n = 0; n < 3; ++n) {
    out[n] = SQRT3M1 * radius * (2.0 * randu() - 1.0);
  }
  return out;
}

TEST(Multilateration, get_pos)
{
  RNG_reset(0);

  const unsigned int nb_points = 10;
  const double eps = 1e-3;
  mat bs_pos;
  unsigned int method_len = 0;//NB: the number of BSs is greater by one for hyprid and hyperbolic
  bvec method;
  vec ms_pos;
  vec actual_ms_pos;
  unsigned int i;
  Multilateration multi;
  vec meas;//measurements vector for spherical and hybrid multilateration
  mat meas_hyper;//measurements matrix for hyperbolic multilateration

  //test inputs
  bs_pos.set_size(3, 4);
  method.set_size(4);
  method.zeros();
  multi.setup(method, bs_pos);
  ASSERT_TRUE(Multilateration::MULTI_SPHERICAL == multi.get_type());
  method(0) = 1;
  bs_pos.set_size(3, 5);//nb of BSs is the number of measures plus one
  multi.setup(method, bs_pos);
  ASSERT_TRUE(Multilateration::MULTI_HYBRID == multi.get_type());
  method.ones();
  multi.setup(method, bs_pos);
  ASSERT_TRUE(Multilateration::MULTI_HYPERBOLIC == multi.get_type());

  for(method_len = 4; method_len < 8; ++method_len) {
    //spherical multilateration
    ASSERT_TRUE(get_bs(bs_pos, method_len, BASE_LINE_M));
    method.set_size(method_len);
    method.zeros();
    multi.setup(method, bs_pos);
    ms_pos = get_ms(SPHERE_RADIUS_M);
    ASSERT_TRUE(generate_meas(meas, method, bs_pos, ms_pos));
    ASSERT_TRUE(multi.get_pos(actual_ms_pos, meas));
    ASSERT_TRUE(point_eq(ms_pos, actual_ms_pos, eps));
    actual_ms_pos.zeros();

    //hybrid multilateration
    ASSERT_TRUE(get_bs(bs_pos, method_len + 1, BASE_LINE_M));
    for(i = 0; i < (method_len - 1); ++i) {
      method[i] = 1;
      multi.setup(method, bs_pos);
      ms_pos = get_ms(SPHERE_RADIUS_M);
      ASSERT_TRUE(generate_meas(meas, method, bs_pos, ms_pos));
      ASSERT_TRUE(multi.get_pos(actual_ms_pos, meas));
      ASSERT_TRUE(point_eq(ms_pos, actual_ms_pos, eps));
      actual_ms_pos.zeros();
    }

    //hyperbolic multilateration
    method[i] = 1;
    multi.setup(method, bs_pos);
    ms_pos = get_ms(SPHERE_RADIUS_M);
    ASSERT_TRUE(generate_meas(meas_hyper, method, bs_pos, ms_pos));
    ASSERT_TRUE(multi.get_pos(actual_ms_pos, meas_hyper));
    ASSERT_TRUE(point_eq(ms_pos, actual_ms_pos, eps));
    actual_ms_pos.zeros();
  }
#ifdef FULL_TESTS
  //test case when the last measure is always from TDOA
  for(method_len = 5; method_len < 8; ++method_len) {
    ASSERT_TRUE(get_bs(bs_pos, method_len + 1, BASE_LINE_M));
    method.set_size(method_len);
    method.ones();
    for(i = 0; i < (method_len - 1); ++i) {
      method[i] = 0;
      multi.setup(method, bs_pos);
      ms_pos = get_ms(SPHERE_RADIUS_M);
      EXPECT_TRUE(generate_meas(meas, method, bs_pos, ms_pos));
      EXPECT_TRUE(multi.get_pos(actual_ms_pos, meas));
      EXPECT_TRUE(point_eq(ms_pos, actual_ms_pos, eps));
      actual_ms_pos.zeros();
    }
  }
#endif
}

TEST(Multilateration, get_crlb)
{
  vec ms_pos(3);
  mat bs_pos;
  bvec method;
  unsigned int method_len = 4;
  double sigma2 = 0.0;
  Multilateration multi;
  unsigned int i;

  method.set_size(method_len);
  method.zeros();
  ASSERT_TRUE(get_bs(bs_pos, method_len, BASE_LINE_M));
  multi.setup(method, bs_pos);

  ms_pos = get_ms(SPHERE_RADIUS_M);

  double crlb = multi.get_crlb(ms_pos, sigma2);
  ASSERT_EQ(0.0, crlb);

  sigma2 = 1e-6;
  for(method_len = 4; method_len < 8; ++method_len) {
    ASSERT_TRUE(get_bs(bs_pos, method_len + 1, BASE_LINE_M));
    method.set_size(method_len);
    method.zeros();
    for(i = 0; i < method_len; ++i) {
      method(i) = 1;
      multi.setup(method, bs_pos);
      ms_pos = get_ms(SPHERE_RADIUS_M);
      crlb = multi.get_crlb(ms_pos, sigma2);
      ASSERT_NEAR(0.0, crlb, (i < 3) ? 1e-1 : 12);
    }
  }

  ms_pos.ones();
  sigma2 = 0.1;
  for(method_len = 4; method_len < 8; ++method_len) {
    ASSERT_TRUE(get_bs(bs_pos, method_len, BASE_LINE_M));
    method.set_size(method_len);
    method.zeros();
    multi.setup(method, bs_pos);
    crlb = multi.get_crlb(ms_pos, sigma2);
    ASSERT_NEAR(0.5, crlb, 0.2);
    ASSERT_TRUE(get_bs(bs_pos, method_len + 1, BASE_LINE_M));
    for(i = 0; i < method_len; ++i) {
      method(i) = 1;
      multi.setup(method, bs_pos);
      crlb = multi.get_crlb(ms_pos, sigma2);
      ASSERT_NEAR(0.5, crlb, 0.6);
    }
  }
}