-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode.cpp
More file actions
3218 lines (2600 loc) · 167 KB
/
decode.cpp
File metadata and controls
3218 lines (2600 loc) · 167 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This program decodes CRFS BIN files and translate than into HDF5 format performing carrier detection on the process. Detailed background information can be obtained at the TFM from Fábio Santos Lobão at mugi.upv.es. Coded by Fábio Santos Lobão, falosan@inf.upv.es
Program arguments arguments are the following:
"-d" to select a decoding mode. Optional, default is zero.
"-u" to select the output measurement unit to be reported. Optional, default is 0.
"-f" preceeding the filename for the input BIN file.
"-o" preceeding the destination file or folder where the data will be stored.
If a file already exists with the indicated filename, it will be overwritten
If only a path to a folder is provided, a file will be created with name based on the timestamp when the data was collected and its coordinates, with extension ".h5"
"-h" for simple help.
The order in which the options are presented is not relevant, but if a parameter is needed for the option it should be the following string without blanc spaces
-h option have precedence and will null other options
examples:
./decode -d 0 -u 0 -f ./InBox/SCAN_M_450470_rfeye002088_170426_235831.bin -o ./OutBox
./decode -d 1 -u 1 -f ./InBox/SCAN_M_450470_rfeye002088_170426_235831.bin -o ./OutBox/ouput.h5
If the file is a CRFS bin file type 22, generated by RFEye Logger, it will be decoded and,
if successfull, will generate files containing RF spectrum channel information and associated occupancy related statistics.
Empty channels and timeslots will be recorded as noise statistics
This was coded using visual studio code within linux, using gcc libraries and c11 standard for higher iteroperability with other systems
HDF5 library is also required
Use this addon "Better Comments" extension to improved readability
*/
// External Libraries
#include <H5Cpp.h>
#include <limits.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <ctime>
#include <vector>
#include <math.h>
#include <unordered_map>
#include <map>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
// used only for debug with "level_differential_detector.h"
#include "gnuplot-iostream.h"
// Project Libraries
#include <crfsbin.hpp> // constants associated with the CRFS bin file format
#include <h5_spectrum.hpp> // constants associated with the created HDF5 standard for spectrum data
#include "level_differential_detector.hpp" // object to perform channel detection based on the signal level differential.
// Namespaces
using namespace std;
using namespace H5;
// Constants used by program workflow
const char PARAMETER_SWITCH = '-';
const char HELP_OPTION = 'h';
const char DETECTION_MODE_OPTION = 'd';
const int NO_DETECTION_OPTION = 0;
const int LEVEL_DETECTION_OPTION = 1;
const int FIRST_DETECTION_MODE_OPTION = NO_DETECTION_OPTION;
const int LAST_DETECTION_MODE_OPTION = LEVEL_DETECTION_OPTION;
const char OUTPUT_UNIT_OPTION = 'u';
const int DBM = 0; // corresponds to dBm
const int DBUV_M = 1; // corresponds to dBuV/m
const double DBM_TO_DBUV_FACTOR = 107.0; // transformation factor from dBm to dBuV for 50ohms impedance.
const char FILENAME_OPTION = 'f';
const char OUTPUT_DESTINATION_OPTION = 'o';
// Constant that control de behaviour of the program on specific conditions
// Maximum number of levels for a CRFS bin file. Original format has a 0.5dB resolution and a range of 127dB, making up 255 bin levels. Increasing the resolution to 0.25dB to reduce error
const uint32_t MAXIMUM_NUMBER_OF_LEVEL_BINS = 256;
// Reference to the byte definition
const double BYTE_MAX_VALUE = 255.0;
// Default level step computed from bin standard definition and maximum number of level bins
const double DEFAULT_LEVEL_STEP = ( BYTE_MAX_VALUE / STEP_PER_LEVEL_UNIT ) / (MAXIMUM_NUMBER_OF_LEVEL_BINS-1);
// Reference to the conversion from nanosencod into second
const double NANOSECONDS_IN_SECOND = 1000000000.0;
// Reference to be used to mark segments of the spectrum that where removed.
const float NULL_SPECTRUM = -99999;
// Minimum usable ID for channel stream information. Also the limit for CRFS thread ID that can be converted to stream ID
const double MINIMUM_CHANNEL_THREAD_ID = 20000;
// responsible
const string RESPONSIBLE("falosan@inf.upv.es");
// Global variables used for application control
// store the different block types
int block_type_count[MAXIMUM_BLOCK_NUMBER+1];
// allow for different detection mode
char detection_mode = NO_DETECTION_OPTION;
// store the input parameter related to the requested output measurement unit
string output_unit_label;
char output_unit = DBM;
// point to byte being processed from the data loaded
char *memblock;
// store the amount of data stored on the file, used for estimating memory usage and perform allocation
streampos data_size;
// string storage for JSON output
stringstream output_log;
// Object used to store parameters used for data conversion and detection
struct sweep_parameters {
int window_size;
int second_window_offset;
double detection_threshold;
// in number of bins, no channel will be smaller than this size
int minimum_channel_width;
// in number of bins, no channel will be spaced with less than this size
int minimum_ascending_break_separation;
int minimum_descending_break_separation;
// in number of bins, no channel will be larger than this size. A break will be automatically created
int maximum_channel_width;
// size of the channel size array
int number_frequency_bins;
// pointer to 1D array that list the trace data streams over all frequency bins, allowing for fast adjustment
double * level_offset_factor_frequency;
// antenna factor used for the adjustment to an isotropic antenna equivalent
double * level_offset_factor;
// level offset factor minimum value
double level_offset_factor_min;
// level offset factor maximum value
double level_offset_factor_max;
// Null constructor
sweep_parameters() : window_size(5),
second_window_offset(1),
detection_threshold(7.0),
minimum_channel_width(15),
minimum_ascending_break_separation(2),
minimum_descending_break_separation(2),
maximum_channel_width(125),
number_frequency_bins(0),
level_offset_factor_frequency(nullptr),
level_offset_factor(nullptr),
level_offset_factor_min(__DBL_MAX__),
level_offset_factor_max(-__DBL_MAX__) {}
// constructor to an specific frequency range
sweep_parameters (double start_frequency,
double stop_frequency,
uint32_t number_frequency_bins_data,
int input_unit,
int output_unit,
string antenna_id) {
// TODO: Load the data to these variables from the database, add database handle to input parameters
double differential_threshold_detection_level = 7.0; // in dB
double frequency_offset_for_differential_computation = 3125.0; // in Hz
double window_size_for_differential_computation = 6250.0; // in Hz - Actual channel spacing should be more than this value to ensure detection
double minimum_channel_width_reference = 6250.0; // in Hz, no channel will be smaller than this size
double minimum_channel_spacing_at_start = 781; // in Hz, no channel will be spaced with less than this size
double minimum_channel_spacing_at_end = 781; // in Hz, no channel will be spaced with less than this size
double maximum_channel_width_reference = 1000000; // in Hz, no channel will be larger than this size. A break will be automatically created
// load the antenna factor data
// TODO: the following variable initialization should be performed with data loaded from the database
int antenna_factor_length = 17;
double antenna_factor_data[2][17] = { { 20000000, 30000000, 40000000, 50000000, 80000000, 100000000, 150000000, 200000000, 300000000, 400000000, 500000000, 600000000, 700000000, 800000000, 900000000, 1000000000, 1200000000},
{ 18.8, 12.8, 9.3, 7.7, 8.1, 10.3, 17.3, 21.1, 25.8, 27.3, 27.2, 28.8, 31.9, 31.0, 30.3, 33.7, 31.3} };
// initialize variables used to store the reference to the correction factor
int level_offset_factor_length = 2;
double *level_offset_factor_data;
if ( input_unit == output_unit ) {
level_offset_factor_length = 2;
level_offset_factor_data = new double[2*level_offset_factor_length];
level_offset_factor_data[0] = start_frequency;
level_offset_factor_data[1] = stop_frequency;
level_offset_factor_data[2] = -STANDARD_LEVEL_OFFSET;
level_offset_factor_data[3] = -STANDARD_LEVEL_OFFSET;
}
else {
// no detection simple store the data for later processing
switch ( (output_unit*10) + input_unit ) {
case DBUV_M*10+DBM : {
// locate the initial position of the two references to be used for the first interpolation (finish pointing to the higher frequency boundary)
double * antenna_factor_iterator = &antenna_factor_data[0][0];
double * pointer_end = &antenna_factor_data[0][antenna_factor_length];
bool inside_array = true; // using this variable is possible to skip the second loop in case the first reaches the end of the array
while ( (*antenna_factor_iterator < start_frequency) && inside_array ) {
antenna_factor_iterator++;
if ( antenna_factor_iterator >= pointer_end ) inside_array = false;
}
// locate the final position of the two references to be used for the first interpolation and update the counter associated with the number of factors to be used (finish pointing to the higher frequency boundary)
while ( (*antenna_factor_iterator < stop_frequency) && inside_array ) {
antenna_factor_iterator++;
level_offset_factor_length++;
if ( antenna_factor_iterator >= pointer_end ) inside_array = false;
}
// create the array that will be used for later processing and store on it the corresponding values
level_offset_factor_data = new double[2*level_offset_factor_length];
for ( int i = 0; i < level_offset_factor_length; i++ ) {
level_offset_factor_data[i] = antenna_factor_iterator[i-level_offset_factor_length+1];
level_offset_factor_data[i+level_offset_factor_length] = antenna_factor_iterator[(i-level_offset_factor_length+1)+antenna_factor_length]-STANDARD_LEVEL_OFFSET+DBM_TO_DBUV_FACTOR;
}
} break;
default :
// output message
output_log << "\n\t\t\"Warning\": \"No conversion method between input measurement unit type " << input_unit << " and output measurement unit type " << output_unit << "\",";
}
}
// compute bin size in Hz
double bin_size = (stop_frequency - start_frequency)/((double)number_frequency_bins_data-(double)1.0);
// initialize variables to the object
window_size = (int)round(window_size_for_differential_computation/bin_size);
second_window_offset = (int)round(frequency_offset_for_differential_computation/bin_size);
detection_threshold = differential_threshold_detection_level;
minimum_channel_width = (int)round(minimum_channel_width_reference/bin_size);
minimum_ascending_break_separation = (int)round((2.0*minimum_channel_spacing_at_start)/bin_size);
minimum_descending_break_separation = (int)round((2.0*minimum_channel_spacing_at_end)/bin_size);
maximum_channel_width = (int)round(maximum_channel_width_reference/bin_size);
number_frequency_bins = number_frequency_bins_data;
// compute the frequency step between bins center frequency
double frequency_step = (stop_frequency - start_frequency)/((double)number_frequency_bins-(double)1.0);
// initialize vectors for the channel stream index
level_offset_factor_frequency = new double[number_frequency_bins];
level_offset_factor_frequency[0] = start_frequency;
// initialize the frequency vector and end condition variable
double * level_offset_factor_frequency_iterator = level_offset_factor_frequency;
// initialize values for the range of the level offset
level_offset_factor_min = __DBL_MAX__;
level_offset_factor_max = -__DBL_MAX__;
// initialize vectors for the level offset ( antenna factor + correction )
level_offset_factor = new double[number_frequency_bins];
double * level_offset_factor_iterator = level_offset_factor;
// declare variables used to compute interpolation coefficients
double factor_gradient = 0.0;
double factor_offset = 0.0;
double delta_frequency = 0.0;
double next_factor_frequency = 0.0;
double frequency_exact_value = start_frequency;
// define lambda function compute interpolation coefficients
auto compute_coefficient = [&] {
factor_gradient = (level_offset_factor_data[level_offset_factor_length]-level_offset_factor_data[level_offset_factor_length+1]) / (level_offset_factor_data[0]-level_offset_factor_data[1]);
factor_offset = level_offset_factor_data[level_offset_factor_length];
delta_frequency = start_frequency - level_offset_factor_data[0];
next_factor_frequency = level_offset_factor_data[1];
};
// define lambda function to compute the coeffiecient variation range
auto update_range = [&] {
if ( level_offset_factor_min > *level_offset_factor_iterator ) level_offset_factor_min = *level_offset_factor_iterator;
if ( level_offset_factor_max < *level_offset_factor_iterator ) level_offset_factor_max = *level_offset_factor_iterator;
};
// compute initial interpolation coefficients
compute_coefficient();
// sweep the frequency array, filling the corresponding antenna factor and antenna frequency arrays
double * pointer_end = &level_offset_factor_frequency[number_frequency_bins];
do {
// compute frequency value for the next position at the array, rounding
*level_offset_factor_frequency_iterator = round(frequency_exact_value);
// check if antenna factor still aplies to the current frequency
if ( *level_offset_factor_frequency_iterator > next_factor_frequency ) {
// if not, update linear interpolation coefficients
level_offset_factor_data++;
compute_coefficient();
}
// compute antenna factor for the current position
*level_offset_factor_iterator = (factor_gradient*delta_frequency) + factor_offset;
// update the level range information
update_range();
// increment pointer for the next loop
level_offset_factor_frequency_iterator++;
level_offset_factor_iterator++;
delta_frequency+=frequency_step;
frequency_exact_value+=frequency_step;
} while ( level_offset_factor_frequency_iterator < pointer_end );
}
};
// structure to store and perform online computation of basic descriptive indexes for a normally distributed variable
struct normal {
// data fields
double mean_value; // mean_value = ((count*mean_value)+ X )/(count+1);
double std_value; // std_value = ( n-2 / n-1 ) std_value {n-1}+{1\over n}(X_n-\bar X_{n-1})^2.
int count; // count = count + 1;
double sum; // to reduce the computational effort and rounding error on the average computation
double sum_squares; // to reduce the computational effort and reduce error on the standard deviation computation
// null argument constructor for this structure
normal() : mean_value(0.0), std_value(0.0), count(0), sum(0.0), sum_squares(0.0) {}
// Perform online computation to add an element to the object
/*
@ARTICLE{Welford62noteon,
author = {Author(s) B. P. Welford and B. P. Welford},
title = {Note on a method for calculating corrected sums of squares and products},
journal = {Technometrics},
year = {1962},
pages = {419--420}
}
*/
// add element to the standard normal distribution
void add_element (double new_element) {
// local variable to help on the computation
double previous_mean_value;
double delta;
// select appropriate update procedure according to the number of elements.
// for better efficiency, considering a vector with 3 or more elements, first test this general and otherwise move to the other cases
if( count > 1 ) {
previous_mean_value = mean_value;
sum = sum + new_element;
count++;
mean_value = sum / (double)count;
sum_squares = sum_squares + ((new_element-previous_mean_value)*(new_element-mean_value));
std_value = sqrt ( sum_squares / (double)count );
} else {
if ( count < 1 ) {
// if there are 0 (negative number of elements are considered 0), set as the first element
mean_value = new_element;
count = 1;
sum = new_element;
} else {
count = 2;
mean_value = (mean_value + new_element) / (double)count;
sum = sum + new_element;
delta = new_element-mean_value;
sum_squares = delta*delta;
}
}
}
};
// structure to store and perform online computation of basic descriptive indexes for a variable not normally distributed
struct non_normal {
// data fields
float maximum_value;
float minimum_value;
vector<float> data;
// null argument constructor for this structure
non_normal() : maximum_value(0.0), minimum_value(0.0), data() {}
// Perform online computation to add an element to the object
void add_element (double new_element) {
// store the element on the vector
data.push_back(new_element);
// compute maximum, minimum
if (new_element>maximum_value) {
maximum_value = new_element;
} else {
if (new_element<minimum_value) {
minimum_value = new_element;
}
}
}
};
// structures to store recovered positional data
struct position {
// data fields
struct timespec start_clock;
struct timespec stop_clock;
struct non_normal satellite_fix;
struct normal latitude;
struct normal longitude;
struct normal altitude;
struct normal horizontal_variation;
// null argument constructor for this structure
position() : start_clock(), stop_clock(), satellite_fix(), latitude(), longitude(), altitude(), horizontal_variation() {}
// Update time references to the structure
void add_time (timespec new_time) {
// If a time was already recorded, store the new time as the stop clock, otherwise set it as the start
if (start_clock.tv_sec>0) {
stop_clock = new_time;
} else {
start_clock = new_time;
}
}
};
// structure to store runtime events
struct event_item {
// data fields
uint32_t posix_time;
uint32_t nanosecond_time;
string type;
string value;
// null argument constructor for this structure
event_item() : posix_time(), nanosecond_time(), type(), value() {}
// full object constructor
event_item (struct timespec time_data,
string type_data,
string value_data) {
posix_time = (uint32_t)time_data.tv_sec;
nanosecond_time = (uint32_t)time_data.tv_nsec;
type = type_data;
value = value_data;
}
// operator to perfor vector sorting by the posix timestamp
bool operator < (const event_item& str) const
{
return (posix_time < str.posix_time);
}
};
// structures to store recovered equipment data
struct equipment_data {
// data fields
string hostname;
string unit_info;
string method;
// null argument constructor for this structure
equipment_data() : hostname(""), unit_info(""), method("") {}
// Register equipment information as part of the measurement log
void set_to_log(
// Initial measurement timestamp, that will be used for reference on the log
struct timespec time_data,
// Log to store the information
vector<event_item> *event_log_pointer) {
event_log_pointer->push_back(event_item(time_data,CRFS_HOSTNAME,hostname));
event_log_pointer->push_back(event_item(time_data,CRFS_UNIT_INFO,unit_info));
event_log_pointer->push_back(event_item(time_data,METHOD,method));
}
};
// Constants to be used to create a tristate simplified logic using int
const int UNKNOWN = 0;
const int BUSY = 1;
const int FREE = -1;
// structure that stores a channel status event
struct channel_status {
// data fields
// Channel status is "1" for occupied (with detected emission within the channel core), "-1" for unoccupied or "0" for unknown.
int status;
// posix time of the event occurrence
uint32_t posix_time;
// nanosecond added time of the event occurrence
uint32_t nanosecond_time;
// duration of the status
double duration;
// null argument constructor for this structure
channel_status() :
status(UNKNOWN),
posix_time(0),
nanosecond_time(0),
duration(0.0) {}
// method to set all values.
void set(
// Channel status is "true" for occupied (with detected emission within the channel core) or false otherwise
int status_data,
// posix time of the event occurrence
uint32_t posix_time_data,
// nanosecond added time of the event occurrence
uint32_t nanosecond_time_data) {
status = status_data;
posix_time = (uint32_t)posix_time_data;
nanosecond_time =(uint32_t)nanosecond_time_data;
}
// method to convert from three state to bool will return error if status unknown
bool is_active(void) {
switch ( status ) {
case BUSY :
return true;
break;
case FREE :
return false;
break;
default :
return EXIT_FAILURE;
}
}
};
// structure to store trace data relative to one capture
struct channel_data {
// data fields
// Stream code based on central frequency in kHz, rounded to 1kHz
double stream_code;
// Initial frequency of the channel
double start_frequency;
// Final frequency of the channel
double stop_frequency;
// number of frequency bins on the power signature
uint32_t number_frequency_bins;
// frequency axis
double *frequency_axis;
// number of level bins on the power signature
uint32_t number_level_bins;
// step between two level bins
double level_step;
// level axis values
double *level_axis;
// 2D array power frequency relation for all detected occurrences
uint16_t *power_signature;
// Number of traces considered
uint32_t number_of_samples;
// Level normal approximation statistics
vector<normal> level_normal;
// Level density inside the core over time
vector<float> core_level_density;
// Posix time axis for the core density vector
vector<uint32_t> posix_time_for_density;
// Nanosecond time axis for the core density vector
vector<uint32_t> nanosecond_time_for_density;
// POSIX time of the start of the on event
vector<uint32_t> ON_event_Posix;
// uint32_t t f the start of the on event
vector<uint32_t> ON_event_Nanosecond;
// uint32_ton the on_event
vector<double> ON_event_duration;
// POSIX time of the start of the on event
vector<uint32_t> OFF_event_Posix;
// uint32_time of the start of the on event
vector<uint32_t> OFF_event_Nanosecond;
// uint32_ton the on_event
vector<double> OFF_event_duration;
// Beginning Status. Used for later file merging
channel_status begin_status;
// Initial Status Duration. Used for later file merging
channel_status end_status;
// Average sample rate
normal sample_rate_period;
// initial frequency of the channel core
double core_initial_frequency;
// final frequency of the channel core
double core_final_frequency;
// null argument constructor for this structure
channel_data() :
stream_code(0.0),
start_frequency(0.0),
stop_frequency(0.0),
number_frequency_bins(0),
frequency_axis(nullptr),
number_level_bins(0),
level_step(0.0),
level_axis(nullptr),
power_signature(nullptr),
number_of_samples(0),
level_normal(),
core_level_density(),
posix_time_for_density(),
nanosecond_time_for_density(),
ON_event_Posix(),
ON_event_Nanosecond(),
ON_event_duration(),
OFF_event_Posix(),
OFF_event_Nanosecond(),
OFF_event_duration(),
begin_status(),
end_status(),
sample_rate_period(),
core_initial_frequency(0.0),
core_final_frequency(0.0) {}
// Constructor to an specific volume of spectrum data. Allocate memory space.
channel_data(
// Lower channel frequency edge
double start_frequency_data,
// Higher channel frequency edge
double stop_frequency_data,
// number of frequency bins on the power signature
uint32_t number_frequency_bins_data,
//
double level_step_data,
// minimum level value
double min_level,
// maximum level value
double max_level) {
// compute the initial estimate for the stream code
stream_code = round((stop_frequency_data+start_frequency_data)/2000.0);
// save data that was passed and allocate memory to arrays
start_frequency = start_frequency_data;
stop_frequency = stop_frequency_data;
number_frequency_bins = number_frequency_bins_data;
level_step = level_step_data;
number_level_bins = (uint32_t)round((max_level-min_level)/level_step)+1;
try {
frequency_axis = new double[number_frequency_bins_data];
}
catch (bad_alloc& ba) {
cerr << "Channel frequency axis bad_alloc caught: " << ba.what() << '\n';
}
try {
level_axis = new double[number_level_bins];
}
catch (bad_alloc& ba) {
cerr << "Channel level axis bad_alloc caught: " << ba.what() << '\n';
}
try {
power_signature = new uint16_t[number_level_bins*number_frequency_bins_data]();
}
catch (bad_alloc& ba) {
cerr << "Channel power signature bad_alloc caught: " << ba.what() << '\n';
}
level_normal.resize(number_frequency_bins_data);
number_of_samples = 0;
begin_status = channel_status();
end_status = channel_status();
sample_rate_period = normal();
// fill the level index array
double * level_axis_iterator = level_axis;
double * level_axis_end = &level_axis[number_level_bins];
*level_axis_iterator = max_level;
level_axis_iterator++;
while ( level_axis_iterator < level_axis_end ) {
*level_axis_iterator = level_axis_iterator[-1]-level_step;
level_axis_iterator++;
}
}
// operator to perfor vector sorting
bool operator < (const channel_data& str) const
{
return (start_frequency < str.start_frequency);
}
// shrink arrays to save memory
void shrink_to_fit(
//
uint32_t minimum_level_index,
// trace that contains the segment to be moved
uint32_t maximum_level_index) {
// TODO: Create a method to reduce memory usage by reducing the arrays to fit the data exactly
}
};
// structure to store trace data relative to one capture
struct trace_data {
// data fields
// number of measurement loops performed to create this trace
uint32_t number_of_loops;
// wideband sample duration time using time time in nanoseconds for direct compatibility with time.h
double sample_duration;
// size of the frequency vector
uint32_t number_frequency_bins;
// size of the time vector
uint32_t number_time_bins;
// 1D array with length number_frequency_bins and that store the frequency axis or indexes to channel streams
double *frequency;
// frequency space between two adjascent bins
double frequency_step;
// 1D array with length number_time_bins with posix time with no leap second
uint32_t *posix_time;
// 1D array with length number_time_bins with nanosecond precision information to be added to posix time.
uint32_t *nanosecond_time;
// 2D array with dimensions of number_frequency_bins x number_time_bins with power level for each time and frequency bin
float *spectrogram;
// 2D array with dimensions of number_frequency_bins x number_time_bins with power level for each time and frequency bin
double *spectrogram_sum;
// 2D vector with list of the index of channels detected on each trace
vector<detection_flag> threshold_crossing;
// 1D vector with noise reference level computed for each trace
vector<double> noise_reference;
// Applied threshold above noise
double relative_thershold_value;
// Minimum expected level value using full scale by default. Adjusted to the minimum observed level if channel detection is performed.
double level_min;
// Maximum expected level value using full scale by default. Adjusted to the minimum observed level if channel detection is performed.
double level_max;
// Index on the level axis to the minimum level observed
uint32_t level_min_index;
// Index on the level axis to the maximum level observed
uint32_t level_max_index;
// Channel code, index to the corresponding channel information, if available
double channel_code;
// null argument constructor for this structure
trace_data() :
number_of_loops(0),
sample_duration(0),
number_frequency_bins(0),
number_time_bins(0),
frequency(nullptr),
frequency_step(0.0),
posix_time(nullptr),
nanosecond_time(nullptr),
spectrogram(nullptr),
spectrogram_sum(nullptr),
threshold_crossing(),
noise_reference(),
level_min (__DBL_MAX__),
level_min_index(0),
level_max (-__DBL_MAX__),
level_max_index(UINT32_MAX),
channel_code(0) {}
// Constructor to an specific volume of spectrum data. Allocate memory space.
trace_data(
uint32_t number_of_loops_data,
uint32_t sample_duration_data,
uint32_t number_frequency_bins_data,
int estimated_number_time_bins,
double start_frequency_data,
double stop_frequency_data,
timespec time_data,
bool flag_store_event) {
// save data that was passed
number_of_loops = number_of_loops_data;
sample_duration = sample_duration_data;
number_frequency_bins = number_frequency_bins_data;
// allocate memory to store an array with frequency data
// frequency = &vector<double>(number_frequency_bins)[0];// (double*) malloc (number_frequency_bins);
frequency = new double[number_frequency_bins];
// compute the step of each bin
frequency_step = (stop_frequency_data - start_frequency_data)/((double)number_frequency_bins-(double)1.0);
double frequency_exact_value = start_frequency_data;
// create array with frequencies values to all measurement bins
double * pointer_end = &frequency[number_frequency_bins];
double * frequency_iterator = &frequency[1];
frequency[0] = start_frequency_data;
while (frequency_iterator < pointer_end)
{
frequency_exact_value+=frequency_step;
*frequency_iterator = round(frequency_exact_value);
frequency_iterator++;
}
// allocate memory to store the array with time data
posix_time = new uint32_t[estimated_number_time_bins];
nanosecond_time = new uint32_t[estimated_number_time_bins];
// store the time information for the first trace
posix_time[0] = (uint32_t)time_data.tv_sec;
nanosecond_time[0] = (uint32_t)time_data.tv_nsec;
number_time_bins = 0;
level_min = __DBL_MAX__;
level_min_index = UINT32_MAX;
level_max = -__DBL_MAX__;
level_max_index = 0;
int number_of_elements = number_frequency_bins*(estimated_number_time_bins+1);
// allocate memory to store the array with spectrogram
try {
spectrogram = new float[number_of_elements];
}
catch (bad_alloc& ba) {
cerr << "spectrogram bad_alloc caught: " << ba.what() << '\n';
}
try {
spectrogram_sum = new double[number_of_elements];
}
catch (bad_alloc& ba) {
cerr << "spectrogram bad_alloc caught: " << ba.what() << '\n';
}
}
// Method to test if a trace copy is possible
bool test_copy(
// Trace data object that will receive the moved data at the end postion
trace_data *destination_trace_data,
// trace that contains the segment to be moved
uint32_t trace_time_index,
// Initial trace data position
uint32_t trace_frequency_begin_index,
// Number of bins to be moved
uint32_t trace_frequency_end_index) {
// Test if the number of frequency bins at the destination is compatible with the number of bins to be copied
// TODO: Improve error handling on this case and break execution
if ( destination_trace_data->number_frequency_bins != (trace_frequency_end_index - trace_frequency_begin_index) ) {
// report error
return false;
}
// if the frequency array has the same size, check if the initial value is NOT the same
else {
// if the initial frequency value for source and destination are different
if ( *destination_trace_data->frequency != frequency[trace_frequency_begin_index] ) {
// report error
return false;
}
// if the initial value is the same, check if the last is also the same
else if ( destination_trace_data->frequency[destination_trace_data->number_frequency_bins-1] == frequency[trace_frequency_end_index] ){
// report error
return false;
}
// else, the frequency array at the destination matches the frequency array at the source and the move may proceed
else {
return true;
}
}
}
// Method to copy segment of a trace into another and delete the original trace data, updating channel information in the process
void move_channel_trace(
// Trace data object that will receive the moved data at the end postion
trace_data *destination_trace_data,
// Channel data object that will receive the moved data at the end postion
channel_data *destination_channel_data,
// trace that contains the segment to be moved
uint32_t trace_time_index,
// Initial trace data position
uint32_t trace_frequency_begin_index,
// Number of bins to be moved
uint32_t trace_frequency_end_index) {
// number of measurement loops performed to create this trace
destination_trace_data->number_of_loops = number_of_loops;
// wideband sample duration time using time time in nanoseconds for direct compatibility with time.h
destination_trace_data->sample_duration = sample_duration;
// Insert new value at the end of the time arrays
destination_trace_data->posix_time[destination_trace_data->number_time_bins] = posix_time[trace_time_index];
destination_trace_data->nanosecond_time[destination_trace_data->number_time_bins] = nanosecond_time[trace_time_index];
// Copy the spectrogram values and delete the original, computing minimum and maximum values during this process
float *source_iterator = &spectrogram[(trace_time_index*number_frequency_bins)+trace_frequency_begin_index];
float *source_end_pointer = &spectrogram[(trace_time_index*number_frequency_bins)+trace_frequency_end_index+1];
float *target_iterator = &destination_trace_data->spectrogram[destination_trace_data->number_time_bins*destination_trace_data->number_frequency_bins];
// reset frequency index to point at the first element on the vector
uint32_t frequency_index = 0;
// compute a reference index based on the first level and reset maximum and minimum values
uint32_t level_index = (int)round((level_max-(double)*source_iterator)/destination_channel_data->level_step);
destination_trace_data->level_min_index = level_index;
destination_trace_data->level_max_index = level_index;
destination_trace_data->level_min = (double)*source_iterator;
destination_trace_data->level_max = (double)*source_iterator;
while (source_iterator < source_end_pointer) {
// copy the level data
*target_iterator = *source_iterator;
// compute the level index
level_index = (int)round((level_max-(double)*target_iterator)/destination_channel_data->level_step);
// Increment value at the power signature array at the computed position
destination_channel_data->power_signature[frequency_index+(level_index*destination_trace_data->number_frequency_bins)]++;
// add value to the normal distributioin computation for level
destination_channel_data->level_normal[frequency_index].add_element((double)*target_iterator);
// update maximum and minimul level references for the channel
if ( *target_iterator < destination_trace_data->level_min ) {
destination_trace_data->level_min = *target_iterator;
destination_trace_data->level_min_index = level_index;
}
if ( *target_iterator > destination_trace_data->level_max ) {
destination_trace_data->level_max = *target_iterator;
destination_trace_data->level_max_index = level_index;
}
// Erase the source trace infomation
*source_iterator = NULL_SPECTRUM;
// Increment iterators, skipping cases where, even after increment, source stilll pointing to a NULL_SPECTRUM, meaning that it reached an interception with adjascent channel
while ( *source_iterator == NULL_SPECTRUM) {
frequency_index++;
source_iterator++;
target_iterator++;
if (source_iterator == source_end_pointer) break;
}
}
// Increment the number of time bins on the destination to include the new trace
destination_trace_data->number_time_bins++;
// increment the counter for the number of channel samples
destination_channel_data->number_of_samples++;
// set the trace data with the same noise reference. Necessary in case that multiple streams are available on the file
destination_trace_data->noise_reference = noise_reference;
}
// Method to copy segment of a trace into another and delete the original trace data, updating channel information in the process.
void abstract_noise(
// Reference stream thread ID
double reference_thread_index,
// Channel data object that will receive the moved data at the end postion
channel_data *destination_channel_data) {
// Update basic information on channel data for the noise
destination_channel_data->stream_code = reference_thread_index;
destination_channel_data->number_of_samples = number_time_bins;
destination_channel_data->begin_status = channel_status();
destination_channel_data->begin_status.status = false;
destination_channel_data->begin_status.posix_time = posix_time[0];
destination_channel_data->begin_status.nanosecond_time = nanosecond_time[0];
destination_channel_data->begin_status.duration = ((double)posix_time[number_time_bins]-(double)posix_time[0]) + (((double)nanosecond_time[number_time_bins]-(double)nanosecond_time[0])/NANOSECONDS_IN_SECOND);
destination_channel_data->end_status = channel_status();
destination_channel_data->end_status.status = false;
destination_channel_data->end_status.posix_time = posix_time[number_time_bins];
destination_channel_data->end_status.nanosecond_time = nanosecond_time[number_time_bins];
destination_channel_data->end_status.duration = 0;
// reset index variables
level_min_index = 0;
level_max_index = UINT32_MAX;
// Update spectrogram infomation values taking into consideration the discarted channels, computing minimum and maximum values during this process
float *spectrogram_iterator = spectrogram;
float *end_pointer = &spectrogram[number_time_bins*number_frequency_bins];
// Copy the original channel maximum as reference before updating to the actual maximum value
double reference_level = level_max;
level_max = (double)*spectrogram_iterator;
level_min = (double)*spectrogram_iterator;
// compute a reference index based on the first level and reset maximum and minimum values
uint32_t level_index = (int)round((reference_level-(double)*spectrogram_iterator)/destination_channel_data->level_step);
level_min_index = level_index;
level_max_index = level_index;
// reset frequency index to point at the first element on the vector
uint32_t frequency_index = 0;
// compute spectrum profile and level range for the noise data
while (spectrogram_iterator < end_pointer) {
// if the value at the current iterator is not null
if ( *spectrogram_iterator != NULL_SPECTRUM) {
// compute the level index
level_index = (int)round((reference_level-(double)*spectrogram_iterator)/destination_channel_data->level_step);
// Increment value at the power signature array at the computed position
destination_channel_data->power_signature[frequency_index+(level_index*number_frequency_bins)]++;
// TODO: Replace the NULL_SPECTRUM by the noise reference for the trace
// update maximum and minimum level references for the channel. Scale is reversed, the minimum level is has the maximum index
if ( *spectrogram_iterator > level_max ) {
level_max = *spectrogram_iterator;
level_max_index = level_index;
}
if ( *spectrogram_iterator < level_min ) {
level_min = *spectrogram_iterator;
level_min_index = level_index;
}
}
// Increment iterators
frequency_index++;
spectrogram_iterator++;
// reset the frequency index when it reaches the maximum
if (frequency_index == number_frequency_bins) frequency_index = 0;
}
}