forked from gante/mmWave-localization-learning
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
652 lines (431 loc) · 19.8 KB
/
main.cpp
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
//---------------------------------------------------------------------------//
// João Gante's PhD work IST-Portugal //
// Oct 2017 //
// //
// Data Preprocessing - transforms the discrete impulses //
// into fixed time intervals values //
//---------------------------------------------------------------------------//
// ~~~~~~~~~~~~~~~ IMPORTANT ~~~~~~~~~~~~~~~~~~
// If using too much RAM (> 1 GB), change the processing
// (i.e. remove the BEAMFORMING dimension from the table
// and store after each file)
// ~~~~~~~~~~~~~~~ IMPORTANT ~~~~~~~~~~~~~~~~~~
#include "general_includes.hpp"
void create_position_table(const int total_elements);
void process_paths(const int & paths, const int & time_slots, const float path_phase[1000],
const float path_delay[1000], const float path_power[1000], float * slot_values);
void copy_to_main_data(const float * slot_values, float * data_table, const int & i,
const int & bf_index, const int & time_slots);
int flag_invalid(float * data_table, const int & users, const int & time_slots);
void fill_final_table(float * final_table, const int & valid_users, const int & elements_per_user);
float dbm_to_mw(const float & x);
float mw_to_dbm(const float & x);
////////////////////////////////////////////////////////////////////////////////////
// Function: Main
// Details: The main function over here
// Inputs: --
// Outputs: --
////////////////////////////////////////////////////////////////////////////////////
int main(){
//Sequence:
// 1 - Creates the tables [#user vs position & main table]
// 2 - Setting the outer loop, each iteration corresponds to a CIR file
// 3 - Runs the inner loop, each iteration corresponds to a user
// a) Reads the user header (#user, total paths)
// b) Reads all the paths
// c) Distributes the paths among the time intervals
// 4 - Checks for valid/invalid positions (invalid has a +1 at the first value)
// 5 - Stores the resulting table (and cleans up)
// 6 - Converts the two tables into DNN-ready data
//--------------------------------------------------------------------------//
// 1 - Creates the positions table [#user vs position] //
//--------------------------------------------------------------------------//
//Knowing the problem size (area + resolution), it's easy to create the tables
int X_elements = (GRID_SIZE_X/RESOLUTION) + 1;
int Y_elements = (GRID_SIZE_Y/RESOLUTION) + 1;
int total_elements = X_elements * Y_elements;
//Creates, initializes and stores the position table
create_position_table(total_elements);
//Table format: dim1 = N_elements = X_elements * Y_elements
// dim2 = beamformings = BEAMFORMINGS
// dim3 = time slots = MAX_DELAY / (1/SAMPLE_FREQ)
cout << "Initializing the data table...";
int time_slots = int(MAX_DELAY / (1/SAMPLE_FREQ) );
float * data_table;
data_table = new float[total_elements * BEAMFORMINGS * time_slots];
cout << " Done!\n\n" << endl;
//--------------------------------------------------------------------------//
// 2 - Setting the outer loop, each iteration corresponds to a CIR file //
//--------------------------------------------------------------------------//
int bf_index, i, j, user, paths;
char filename[100], buffer[1000];
float path_power[1000], path_phase[1000], path_delay[1000], f_buffer;
float * slot_values;
FILE *InputFile;
slot_values = new float[time_slots];
for(bf_index = 1; bf_index <= BEAMFORMINGS; bf_index++){
//Loads the file, with the name template "\\data_raw\\cir_" + i + ".txt"
sprintf(filename, "data_raw/CIR_32/cir_%d.txt", bf_index);
InputFile = fopen(filename, "r");
cout << "Processing file #" << bf_index << "... ";
//If there was any problem with the file loading, sets the error up and breaks
if(!InputFile) {
cout << "ERROR loading a simulation file (" << bf_index << ")" << endl;
exit(2);
}
//Ignores the first three lines (unneeded information)
for(i = 0; i < 3; i++){
fgets(buffer, sizeof(buffer), InputFile);
}
//--------------------------------------------------------------------------//
// 3 - Runs the inner loop, each iteration corresponds to a user //
//--------------------------------------------------------------------------//
// For each user, the processing is split among two sections
// i) user header (1 line, indicates how many paths)
// ii) user paths (N lines, with the key information)
for(i = 1; i <= total_elements; i++){
// a) Reads the user header (#user, total paths)
//Reads the user index
if(fscanf(InputFile,"%d",&user) == 0){
cout << "\nERROR reading a value";
exit(3);
}
//Reads the # of paths for that user
if(fscanf(InputFile,"%d",&paths) == 0){
cout << "\nERROR reading a value";
exit(3);
}
if(user != i){
cout << "\nERROR double checking the user index";
exit(4);
}
if(paths > 999){
cout << "\nERROR too many paths! (increase the buffers)";
exit(5);
}
// b) Reads all the paths
for(j = 0; j < paths; j++){
//Reads the path index (not used)
if(fscanf(InputFile,"%f",&f_buffer) == 0){
cout << "\nERROR reading a value";
exit(3);
}
//Reads the path phase
if(fscanf(InputFile,"%f",&f_buffer) == 0){
cout << "\nERROR reading a value";
exit(3);
}
path_phase[j] = f_buffer;
//Reads the path delay
if(fscanf(InputFile,"%f",&f_buffer) == 0){
cout << "\nERROR reading a value";
exit(3);
}
path_delay[j] = f_buffer;
//Reads the path power in dBm
if(fscanf(InputFile,"%f",&f_buffer) == 0){
cout << "\nERROR reading a value";
exit(3);
}
path_power[j] = f_buffer;
} // end the *for* cycle, which goes through all the paths
// c) Distributes the paths among the time intervals
process_paths(paths, time_slots, path_phase, path_delay, path_power, slot_values);
copy_to_main_data(slot_values, data_table, i, bf_index, time_slots);
if(i%(int)(total_elements/10) == 0){
cout << "*";
}
} // end the *for* cycle, which goes through all the users
fclose(InputFile);
cout << " Done!" << endl;
} // end the *for* cycle, which goes through all the files
//--------------------------------------------------------------------------//
// 4 - Checks for valid positions (invalid has a +1 at the first value) //
//--------------------------------------------------------------------------//
cout << "Flagging invalid positions...";
int invalid_postions;
invalid_postions = flag_invalid(data_table, total_elements, time_slots);
cout << " Done!" << endl;
//--------------------------------------------------------------------------//
// 5 - Stores the resulting table (and cleans up) //
//--------------------------------------------------------------------------//
cout << "Storing the results...";
FILE *Data;
//Opens the data file
sprintf(filename, "./data_processed/data_table");
Data = fopen(filename, "wb");
if(!Data) {
cout << "\nERROR opening the data file";
exit(2);
}
//Writes the data
fwrite(data_table, sizeof(float), (total_elements * BEAMFORMINGS * time_slots), Data);
fclose(Data);
delete [] data_table;
delete [] slot_values;
cout << " Done!" << endl;
//--------------------------------------------------------------------------//
// 6 - Converts the two tables into DNN-ready data //
//--------------------------------------------------------------------------//
cout << "Converting into the final table...";
//Final table : pos_0, data_pos_0, pos_1, data_pos_1,...
// (discards the invalid positions)
// Current data format: 32 bit floating point
int valid_users = (total_elements-invalid_postions);
int elements_per_pos = ((BEAMFORMINGS * time_slots) + 2);
int final_data_elements = valid_users * elements_per_pos;
float * final_table;
final_table = new float[final_data_elements];
//fills the table
fill_final_table(final_table,valid_users,elements_per_pos);
//stores the table
sprintf(filename, "./data_processed/final_table");
Data = fopen(filename, "wb");
if(!Data) {
cout << "\nERROR opening the final data file";
exit(2);
}
//Writes the data
fwrite(final_table, sizeof(float), final_data_elements, Data);
fclose(Data);
delete [] final_table;
cout << " Done!" << endl;
cout << "\nSuccess!" << endl;
return(0);
}
////////////////////////////////////////////////////////////////////////////////////
// Function: Create Position Table
// Details: Creates, initializes and stores the position table
// Inputs: Total number of elements (the rest are defines)
// Outputs: --void fill_final_table(float * final_table, const int & valid_users, const int & elements_per_user)
////////////////////////////////////////////////////////////////////////////////////
void create_position_table(const int total_elements){
//Position Table: correspondence between position index and actual position [#user = implicit, X, Y]
cout << "\nInitializing the position table...";
int i, index;
float x_aux = 0.0, y_aux = 0.0;
float * position_table;
position_table = new float[total_elements * 2];
//initializes the position table data
// (the users are numbered in a "index = x + y*Y_elements" fashion)
for(i = 0; i < total_elements; i++){
index = i*2;
position_table[index] = STARTING_X + x_aux; // X
position_table[index+1] = STARTING_Y + y_aux; // Y
x_aux = x_aux + RESOLUTION;
if(x_aux > GRID_SIZE_X){
x_aux = 0.0;
y_aux = y_aux + RESOLUTION;
}
}
//Opens the position file
char filename[100];
FILE *OutputFile;
sprintf(filename, "./data_processed/position_table");
OutputFile = fopen(filename, "wb");
if(!OutputFile) {
cout << "\nERROR opening the position file!";
exit(1);
}
//Writes the data
fwrite(position_table, sizeof(float), (total_elements*2), OutputFile);
//clean up
delete [] position_table;
cout << " Done!" << endl;
}
////////////////////////////////////////////////////////////////////////////////////
// Function: Process paths
// Details: merges the paths phase, delay and power info into the correct time slot
// Inputs: # paths, paths' phase, delay and power information
// Outputs: the slot values (which then must be copied into the main matrix)
////////////////////////////////////////////////////////////////////////////////////
void process_paths(const int & paths, const int & time_slots, const float path_phase[1000],
const float path_delay[1000], const float path_power[1000], float * slot_values){
int i, current_slot = 0, exist_path;
float time_per_slot = 1/SAMPLE_FREQ;
float current_time = 0.0;
float next_time = current_time + time_per_slot;
float tmp_phase, i_power, q_power, power_mw;
// For each time slot, passes through all the paths
while(current_slot < time_slots){
for(i = 0; i < paths; i++){
//checks if the path belongs to the desired time slot
if(path_delay[i] >= current_time && path_delay[i] < next_time && path_power[i] >= MIN_POWER){
//if it does, checks if there is another path for this slot
// no previous path = stores directly in dbm
// previous path = IQ processing
if(exist_path == 0){
exist_path = 1;
slot_values[current_slot] = path_power[i];
tmp_phase = path_phase[i];
}
else{
//when processing the 2nd path, also converts the first into IQ
if(exist_path == 1){
exist_path = 2;
power_mw = dbm_to_mw(slot_values[current_slot]);
i_power = power_mw * cos(tmp_phase);
q_power = power_mw * sin(tmp_phase);
}
power_mw = dbm_to_mw(path_power[i]);
i_power += power_mw * cos(path_phase[i]);
q_power += power_mw * sin(path_phase[i]);
}
}
}
//if multiple paths were processed, converts the IQ into dBm
if(exist_path == 2){
//power_dbm = dbm( power_mw ), power_mw = sqrt(i^2 + q^2)
power_mw = sqrt(pow(i_power,2) + pow(q_power,2));
slot_values[current_slot] = mw_to_dbm(power_mw);
}
//if no path was processed, store 0
else if(exist_path == 0){
slot_values[current_slot] = 0.0;
}
//prepares the data for the next time_slot
current_time = next_time;
next_time = current_time + time_per_slot;
current_slot++;
exist_path = 0;
}
if( (current_slot - 1) > time_slots){
cout << "ERROR! too many time slots stored";
exit(6);
}
}
////////////////////////////////////////////////////////////////////////////////////
// Function: Copy to main data
// Details: copies the processed path data into the main data
// Inputs: processed path data, BF index, user index
// Outputs: main data filled up
////////////////////////////////////////////////////////////////////////////////////
void copy_to_main_data(const float * slot_values, float * data_table, const int & user,
const int & bf_index, const int & time_slots){
//data_table = new float[total_elements * BEAMFORMINGS * time_slots];
// ATTENTION: both "user" and "bf_index" start at 1, so we should subtract it :D
int i;
int starting_index = ((user-1) *(BEAMFORMINGS * time_slots)) + ((bf_index-1) * (time_slots));
for(i = 0; i < time_slots; i++){
data_table[starting_index + i] = slot_values[i];
}
}
////////////////////////////////////////////////////////////////////////////////////
// Function: Flag Invalid
// Details: Flags invalid positions with a "1" (= impossible value) at the first time slot
// Inputs: data size and the data
// Outputs: updated data, number of INVALID positions
////////////////////////////////////////////////////////////////////////////////////
int flag_invalid(float * data_table, const int & users, const int & time_slots){
int i, j, index, slots_to_check = BEAMFORMINGS * time_slots, invalids = 0;
bool valid, dbg = false;
int dbg_40 = 0, dbg_50 = 0, dbg_60 = 0, dbg_index;
//for each user, scans the BF * TS for non-zero values
for(i = 0; i < users; i++){
valid = false;
index = i * (slots_to_check);
//for user i, scans the j slots
for(j = 0; j < slots_to_check; j++){
//as soon as it finds a filled slot, breaks
if(data_table[index + j] != 0.0){
valid = true;
if(dbg){
dbg_index = j%70;
if(dbg_index > 40) dbg_40++;
if(dbg_index > 50) dbg_50++;
if(dbg_index > 60) dbg_60++;
}
else{
break;
}
}
}
//if no valid was found, flags it
if(valid == false){
data_table[index] = 1;
invalids++;
}
//dbg: prints user #14648 = index 14647
if(dbg && i == 14647){
cout << "\nDBG: printing user # 14648" << endl;
for(j = 0; j < slots_to_check; j++){
cout << "j = " << j << "; dBm = " << data_table[index + j] << endl;
}
}
}
cout << "(" << invalids << " invalid positions)";
if(dbg){
cout << "(" << dbg_40 << " = ts_40)";
cout << "(" << dbg_50 << " = ts_50)";
cout << "(" << dbg_60 << " = ts_60)";
}
return(invalids);
}
////////////////////////////////////////////////////////////////////////////////////
// Function: Fill Final Table
// Details: Fills the Final Table, based on previous data
// Inputs: empty table, number of valid users
// Outputs: filled table
////////////////////////////////////////////////////////////////////////////////////
void fill_final_table(float * final_table, const int & valid_users, const int & elements_per_pos){
// 1 - Opens the files
FILE *Data;
FILE *Positions;
char filename[100];
//Opens the data file
sprintf(filename, "./data_processed/data_table");
Data = fopen(filename, "rb");
if(!Data) {
cout << "\nERROR opening the data file";
exit(2);
}
//Opens the positions file
sprintf(filename, "./data_processed/position_table");
Positions = fopen(filename, "rb");
if(!Positions) {
cout << "\nERROR opening the positions file";
exit(2);
}
// 2 - Main loop, where it reads 1 entry and decides
// whether it must save it or not
int read_data_elements = elements_per_pos - 2, valid_count = 0, pos = 0;
float * read_data, * read_position;
read_data = new float[read_data_elements];
read_position = new float[2];
bool dbg = false;
//while there are stuff to read, keeps going
while(fread (read_data, sizeof(float), read_data_elements, Data) == (unsigned int)read_data_elements){
//reads the correspondent position data
fread(read_position, sizeof(float), 2, Positions);
if(dbg && (pos == 0 || pos == 401 || pos == 801 || pos == 802) ){
cout << "\nDBG: pos " << pos << " has the following location: X=" << read_position[0] << " Y=" << read_position[1];
}
//checks for invalid position
if(read_data[0] != 1){
//if it valid, copies the contents
memcpy(final_table+(valid_count*elements_per_pos), read_position, 2 * sizeof(float));
memcpy(final_table+(valid_count*elements_per_pos)+2, read_data, read_data_elements * sizeof(float));
valid_count++;
}
pos++;
}
if(valid_count != valid_users){
cout << "\nERROR - the number of valid users don't match! ";
cout << valid_count;
cout << valid_users;
}
delete [] read_data;
delete [] read_position;
}
////////////////////////////////////////////////////////////////////////////////////
//quick tool: dbm to mw
////////////////////////////////////////////////////////////////////////////////////
float dbm_to_mw(const float & x){
return( pow(10, (x/10)) );
}
////////////////////////////////////////////////////////////////////////////////////
//quick tool: mw to dbm
////////////////////////////////////////////////////////////////////////////////////
float mw_to_dbm(const float & x){
return( 10*log10(x) );
}