-
Notifications
You must be signed in to change notification settings - Fork 7
/
0001-sgd.patch
449 lines (445 loc) · 16.2 KB
/
0001-sgd.patch
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
Index: gnss-sdr/src/algorithms/signal_source/libs/sgd.h
===================================================================
--- /dev/null
+++ gnss-sdr/src/algorithms/signal_source/libs/sgd.h
@@ -0,0 +1,101 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2020 gr-sgd author.
+ *
+ * This 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, or (at your option)
+ * any later version.
+ *
+ * This software 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 this software; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_SGD_SGD_IMPL_H
+#define INCLUDED_SGD_SGD_IMPL_H
+#if GNURADIO_USES_STD_POINTERS
+#include <memory>
+#else
+#include <boost/shared_ptr.hpp>
+#endif
+#include <gnuradio/sync_block.h> // for sync_block
+#include <gnuradio/types.h> // for gr_vector_const_void_star
+#include <pmt/pmt.h>
+
+#include <volk/volk.h>
+
+class sgd_impl;
+
+#if GNURADIO_USES_STD_POINTERS
+std::shared_ptr<sgd_impl> gnss_sdr_make_sgd(
+ int delay_max, float seuil, float alpha,
+ bool mean, int mean_length, int iter_count);
+#else
+boost::shared_ptr<sgd_impl> gnss_sdr_make_sgd(
+ int delay_max, float seuil, float alpha,
+ bool mean, int mean_length, int iter_count);
+#endif
+
+ class sgd_impl : public gr::sync_block
+ {
+ private:
+#if GNURADIO_USES_STD_POINTERS
+ friend std::shared_ptr<sgd_impl> gnss_sdr_make_sgd(
+ int delay_max, float seuil, float alpha,
+ bool mean, int mean_length, int iter_count);
+#else
+ friend boost::shared_ptr<sgd_impl> gnss_sdr_make_sgd(
+ int delay_max, float seuil, float alpha,
+ bool mean, int mean_length, int iter_count);
+#endif
+
+ int _w1_size;
+ float _seuil;
+ float _alpha;
+ int _delay_max;
+ uint32_t alignment;
+
+ gr_complex *tmp2;
+ gr_complex e;
+ float val_max;
+ uint32_t *val_max_index;
+ gr_complex *XXxw1;
+ gr_complex *w1;
+ float *w1_mag;
+ gr_complex *xxConj;
+ int _residual;
+ FILE *_w1_out;
+ int _iter;
+ std::complex<double> **_w1_array;
+ std::complex<double> *w1_accum;
+ gr_complex *w1_res;
+ int _array_index;
+ int _nb_accum;
+ bool _mean;
+ int _mean_length;
+ int _iter_count;
+
+ public:
+ sgd_impl();
+ sgd_impl(int w1_size, float seuil, float alpha,
+ bool mean, int mean_length, int iter_count);
+ ~sgd_impl();
+ int fixed_rate_ninput_to_noutput(int ninput);
+
+ // Where all the action really happens
+ int work(
+ int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items
+ );
+ };
+
+#endif /* INCLUDED_SGD_SGD_IMPL_H */
+
Index: gnss-sdr/src/algorithms/signal_source/libs/sgd_impl.cc
===================================================================
--- /dev/null
+++ gnss-sdr/src/algorithms/signal_source/libs/sgd_impl.cc
@@ -0,0 +1,237 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2020 gr-sgd author.
+ *
+ * This 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, or (at your option)
+ * any later version.
+ *
+ * This software 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 this software; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gnuradio/io_signature.h>
+#include "sgd.h"
+
+//#define DEBUG_COEFF
+
+#if GNURADIO_USES_STD_POINTERS
+std::shared_ptr<sgd_impl> gnss_sdr_make_sgd(
+ int delay_max, float seuil, float alpha, bool mean, int mean_length,
+ int iter_count)
+{
+ std::shared_ptr<sgd_impl> sgd_inst(new sgd_impl(delay_max, seuil, alpha, mean, mean_length, iter_count));
+ return sgd_inst;
+}
+#else
+boost::shared_ptr<sgd_impl> gnss_sdr_make_sgd(
+ int delay_max, float seuil, float alpha, bool mean, int mean_length,
+ int iter_count)
+{
+ boost::shared_ptr<sgd_impl> sgd_inst(new sgd_impl(delay_max, seuil, alpha, mean, mean_length, iter_count));
+ return sgd_inst;
+}
+#endif
+
+ /*
+ * The private constructor
+ */
+ sgd_impl::sgd_impl(int delay_max, float seuil, float alpha, bool mean, int mean_length, int iter_count)
+ : gr::sync_block("sgd",
+ gr::io_signature::make(2, 2, sizeof(gr_complex)),
+ gr::io_signature::make(1, 1, sizeof(gr_complex))),
+ _w1_size(delay_max * 2 + 1), _seuil(seuil), _alpha(alpha),
+ _delay_max(delay_max), _residual(0), _w1_out(NULL), _iter(1),
+ _array_index(0), _nb_accum(1), _mean(mean), _mean_length(mean_length),
+ _iter_count(iter_count)
+ {
+ std::cout << "SGD sliding average: "<< _mean << " on " << _mean_length << "samples" << std::endl;
+ alignment = volk_get_alignment();
+ tmp2 = (gr_complex *)volk_malloc(sizeof(gr_complex) * _w1_size, alignment);
+ val_max_index = (uint32_t *)volk_malloc(sizeof(uint32_t), alignment);
+ XXxw1 = (gr_complex *)volk_malloc(sizeof(gr_complex), alignment);
+ w1 = (gr_complex *)volk_malloc(sizeof(gr_complex) * _w1_size, alignment);
+ w1_mag = (float *)volk_malloc(sizeof(float)* _w1_size, alignment);
+ xxConj = (gr_complex *)volk_malloc(sizeof(gr_complex) * _w1_size, alignment);
+
+ w1_res = (gr_complex *)volk_malloc(sizeof(gr_complex) * _w1_size, alignment);
+ if (_mean) {
+ w1_accum = (std::complex<double> *)malloc(sizeof(std::complex<double>) * _w1_size);
+ _w1_array = (std::complex<double> **)malloc(sizeof(std::complex<double>*) * _mean_length);
+ for (int i=0; i < _mean_length; i++) {
+ _w1_array[i] = (std::complex<double> *)malloc(sizeof(std::complex<double>) * _w1_size);
+ for (int ii=0; ii < _w1_size; ii++)
+ _w1_array[i][ii]={0.0};
+ }
+ for (int ii=0; ii < _w1_size; ii++)
+ w1_accum[ii]={0.,0.};
+ }
+
+ /* cleanup w1 */
+ for (int i=0; i < _w1_size; i++)
+ w1[i] = 0;
+
+//#ifdef DEBUG_COEFF
+ _w1_out = fopen("w1_dump.bin", "w+");
+//#endif
+ }
+
+ /*
+ * Our virtual destructor.
+ */
+ sgd_impl::~sgd_impl()
+ {
+ for (int index = 0; index < _w1_size; index++) {
+ float tr = w1[index].real();
+ float ti = w1[index].imag();
+ fwrite(&tr, sizeof(float), 1, _w1_out);
+ fwrite(&ti, sizeof(float), 1, _w1_out);
+ }
+//#ifdef DEBUG_COEFF
+ fclose(_w1_out);
+//#endif
+ volk_free(tmp2);
+ volk_free(val_max_index);
+ volk_free(XXxw1);
+ volk_free(w1);
+ volk_free(w1_mag);
+ volk_free(xxConj);
+ volk_free(w1_res);
+ if (_mean) {
+ free(w1_accum);
+ for (int i = 0; i < _mean_length; i++)
+ free(_w1_array[i]);
+ free(_w1_array);
+ }
+ }
+
+ int
+ sgd_impl::fixed_rate_ninput_to_noutput(int ninput)
+ {
+ return ninput - (_w1_size);
+ }
+
+ int
+ sgd_impl::work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
+ {
+ const gr_complex *x = (const gr_complex *) input_items[0];
+ const gr_complex *y = (const gr_complex *) input_items[1];
+ gr_complex *res = (gr_complex *) output_items[0];
+
+ int size = noutput_items;
+
+ if (size < _w1_size) {
+ _residual = size;
+ set_history(_residual);
+ return 0;
+ }
+
+ for (int k=0; k < size-_w1_size; k++) {
+
+ volk_32fc_x2_dot_prod_32fc(XXxw1, x+k, w1, _w1_size);
+ //e = (y[k + _delay_max] - *XXxw1) * (float)_alpha;//(1/sqrtf(_iter));
+ //e = (y[k + _delay_max] - *XXxw1) * (float)(1./sqrtf((float)_iter)); // loss when moving with average
+ e = (y[k + _delay_max] - *XXxw1) * (float)(_alpha/sqrtf((float)_iter)); // loss when moving with average
+ if (_iter < _iter_count)
+ _iter++;
+ else {
+ _iter = 1;
+ //for (int index = 0; index < _w1_size; index ++)
+ // std::cout << index - (_delay_max) << " " << w1[index] << std::endl;
+ }
+
+ volk_32fc_conjugate_32fc(xxConj, x+k, _w1_size);
+ volk_32fc_s32fc_multiply_32fc(tmp2, xxConj, e, _w1_size);
+
+ volk_32fc_x2_add_32fc(w1, w1, tmp2, _w1_size);
+
+// remove if _w1_size == 1 (case of GPS un-jamming)
+ if (_w1_size>1)
+ {
+ /* abs */
+ volk_32fc_magnitude_32f(w1_mag, w1, _w1_size);
+ /* search max */
+ volk_32f_index_max_32u(val_max_index, w1_mag, _w1_size);
+ /* store max */
+ val_max = w1_mag[*val_max_index];
+
+ val_max *= _seuil;
+ /* all entry < val_max => 0 */
+ for (int index = 0; index < _w1_size; index++) {
+ if (w1_mag[index] < val_max)
+ w1[index] = 0;
+#ifdef DEBUG_COEFF
+ float toto = w1[index].real();
+ fwrite(&toto, sizeof(float), 1, _w1_out);
+ toto = w1[index].imag();
+ fwrite(&toto, sizeof(float), 1, _w1_out);
+#endif
+ }
+ }
+// remove until here if _w1_size == 1 (case of GPS un-jamming)
+ if (_mean) {
+ for (int index = 0; index < _w1_size; index++) {
+ w1_accum[index] -= _w1_array[_array_index][index];
+
+ _w1_array[_array_index][index].real((double)w1[index].real());
+ _w1_array[_array_index][index].imag((double)w1[index].imag());
+
+ w1_accum[index] += _w1_array[_array_index][index];
+
+ std::complex<double> t = w1_accum[index] / (double)_nb_accum;
+ w1_res[index].real((float)t.real());
+ w1_res[index].imag((float)t.imag());
+ }
+
+
+ if (_array_index == _mean_length-1)
+ _array_index = 0;
+ else
+ _array_index++;
+ if (_nb_accum < _mean_length) _nb_accum++;
+ } else {
+ memcpy(w1_res, w1, _w1_size * sizeof(gr_complex));
+ }
+ if (_iter == 1) {
+ for (int index = 0; index < _w1_size; index ++) {
+ for (int index = 0; index < _w1_size; index ++) {
+ std::cout << index - (_delay_max) << " ";
+ std::cout << w1_res[index].real() << " " << w1_res[index].imag() << std::endl;
+ }
+ }
+ }
+ }
+
+ /* suppress part */
+ int maxsize = size;
+ int coeffsize = _w1_size;
+ int sum_size = maxsize-coeffsize;
+ gr_complex tmp;
+ for (int i=0; i < sum_size; i++) {
+ tmp = 0;
+ volk_32fc_x2_dot_prod_32fc(&tmp, x+i, w1_res, coeffsize);
+ res[i] = y[_delay_max + i] - tmp;
+ }
+
+ _residual = _w1_size - 1;
+ set_history(_residual);
+
+ // Tell runtime system how many output items we produced.
+ return sum_size;
+ }
+
+
Index: gnss-sdr/src/algorithms/signal_source/adapters/uhd_signal_source.cc
===================================================================
--- gnss-sdr.orig/src/algorithms/signal_source/adapters/uhd_signal_source.cc
+++ gnss-sdr/src/algorithms/signal_source/adapters/uhd_signal_source.cc
@@ -21,6 +21,7 @@
#include "GPS_L1_CA.h"
#include "configuration_interface.h"
#include "gnss_sdr_valve.h"
+#include "sgd.h"
#include <glog/logging.h>
#include <uhd/exception.hpp>
#include <uhd/types/device_addr.hpp>
@@ -55,9 +56,22 @@ UhdSignalSource::UhdSignalSource(const C
{
dev_addr["serial"] = device_serial;
}
- subdevice_ = configuration->property(role + ".subdevice", empty);
clock_source_ = configuration->property(role + ".clock_source", std::string("internal"));
RF_channels_ = configuration->property(role + ".RF_channels", 1);
+ sgd_ = configuration->property(role + ".sgd", 0);
+ if (sgd_ !=0)
+ {printf("SGD jamming protection %d\n",sgd_); fflush(stdout);
+ RF_channels_=sgd_; // SGD jamming protection: override RF_channels in this block only => N inputs and RF_chan=1 output
+ subdevice_ = configuration->property(role + ".subdevice", std::string("A:A A:B"));
+ float sgd_alpha = configuration->property(role + ".sgd_alpha", 1e-2);
+ bool sgd_mean = configuration->property(role + ".sgd_mean", true);
+ int sgd_mean_length = configuration->property(role + ".sgd_mean_length", 100);
+ int sgd_iter_count = configuration->property(role + ".sgd_iter_count", 10000);
+ jamming_sgd_=gnss_sdr_make_sgd(0, 5e-3, sgd_alpha, sgd_mean, sgd_mean_length, sgd_iter_count);
+ }
+ if ((sgd_ == 0))
+ {subdevice_ = configuration->property(role + ".subdevice", empty);
+ }
sample_rate_ = configuration->property(role + ".sampling_frequency", 4.0e6);
item_type_ = configuration->property(role + ".item_type", default_item_type);
@@ -248,6 +262,11 @@ void UhdSignalSource::connect(gr::top_bl
DLOG(INFO) << "connected usrp source to file sink RF Channel " << i;
}
}
+ if (sgd_ != 0)
+ {
+ top_block->connect(uhd_source_, i, jamming_sgd_, i);
+ printf("UHD -> SGD connect: %d\n",i);fflush(stdout);
+ }
}
}
@@ -298,5 +317,9 @@ gr::basic_block_sptr UhdSignalSource::ge
{
return valve_.at(RF_channel);
}
+ if ( jamming_sgd_ != 0ULL)
+ {
+ return jamming_sgd_;
+ }
return uhd_source_;
}
Index: gnss-sdr/src/algorithms/signal_source/adapters/uhd_signal_source.h
===================================================================
--- gnss-sdr.orig/src/algorithms/signal_source/adapters/uhd_signal_source.h
+++ gnss-sdr/src/algorithms/signal_source/adapters/uhd_signal_source.h
@@ -78,8 +78,10 @@ private:
gr::uhd::usrp_source::sptr uhd_source_;
#if GNURADIO_USES_STD_POINTERS
std::vector<std::shared_ptr<gr::block>> valve_;
+ std::shared_ptr<gr::block> jamming_sgd_;
#else
std::vector<boost::shared_ptr<gr::block>> valve_;
+ boost::shared_ptr<gr::block> jamming_sgd_;
#endif
std::vector<gr::blocks::file_sink::sptr> file_sink_;
std::vector<double> freq_;
@@ -100,6 +102,7 @@ private:
double sample_rate_;
size_t item_size_;
int RF_channels_;
+ int sgd_;
unsigned int in_stream_;
unsigned int out_stream_;
};
Index: gnss-sdr/src/algorithms/signal_source/libs/CMakeLists.txt
===================================================================
--- gnss-sdr.orig/src/algorithms/signal_source/libs/CMakeLists.txt
+++ gnss-sdr/src/algorithms/signal_source/libs/CMakeLists.txt
@@ -25,6 +25,7 @@ set(SIGNAL_SOURCE_LIB_SOURCES
rtl_tcp_commands.cc
rtl_tcp_dongle_info.cc
gnss_sdr_valve.cc
+ sgd_impl.cc
${OPT_SIGNAL_SOURCE_LIB_SOURCES}
)
@@ -32,6 +33,7 @@ set(SIGNAL_SOURCE_LIB_HEADERS
rtl_tcp_commands.h
rtl_tcp_dongle_info.h
gnss_sdr_valve.h
+ sgd.h
${OPT_SIGNAL_SOURCE_LIB_HEADERS}
)