-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
412 lines (299 loc) · 8.12 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
#include <vector>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <armadillo>
#include <string>
#include <vector>
#include <bits/stdc++.h>
#include "Timer.h"
// g++ batches.cpp main.cpp -o example -O2 -larmadillo
using namespace std;
using namespace arma;
struct Process {
int Fs, c;
int freq, t;
int nSamples, interval;
int nBatches;
int samples;
int batchStrideNSamples;
int batchNSamples;
int nRangeBins;
int nDopplerBins;
int nSampBatches;
int ARDMaxRange_m, ARDMaxDoppler_Hz;
int TxToRefRxDistance_m;
};
struct Cancellation
{
cx_mat R;
cx_mat S;
};
cx_mat circshift(cx_mat in, int xshift, int yshift)
{
int ydim = in.n_cols;
int xdim = in.n_rows;
cx_mat out(size(in));
// circshift each row of mat in
for (int i = 0; i < xdim; ++i)
{
int ii = (i + xshift) % xdim;
for (int j = 0; j < ydim; ++j)
{
int jj = (j + yshift) % ydim;
out(ii,jj) = in(i,j);
}
}
return out;
}
/*
Function to perform matlab's equivalent of fftshift(X)
Where X is some vector
Swap left and right halves of X
centre point belongs to the left
*/
cx_rowvec fftshift(cx_rowvec X)
{
// check even or odd length
int left_size;
int length = X.n_elem;
if (length % 2 == 0)
{
left_size = length/2 - 1;
} else {
left_size = length/2;
}
cx_rowvec left = X.subvec(0, left_size);
cx_rowvec right = X.subvec(left_size+1, length-1);
cx_rowvec result = join_rows(right, left);
return result;
}
/*
Implement ECA_CD cancellation
*/
Cancellation ECA_CD(cx_vec r_ch, cx_vec s_ch, Process proc)
{
// form matrix r, s
// don't transpose since armadillo iterates by col
cx_mat r = reshape(r_ch, proc.interval / proc.nSamples, proc.nSamples);
cx_mat s = reshape(s_ch, proc.interval / proc.nSamples, proc.nSamples);
// Transform pulses into freq domain
// fft of each col
Cancellation result;
result.S = fft(s).t();
result.R = fft(r).t();
// for no cancellations
//cout << "No cancellation" << endl;
//return result;
double fd = 1*(1/proc.t);
// Make a phase shifting matrix
//constants
cx_double j(0.0,1.0);
const double PI = 3.141592653589793238463;
cx_vec phase_shift_vec(proc.nSamples);
#pragma omp parallel for
for (int i = 0; i < proc.nSamples; ++i)
{
phase_shift_vec(i) = exp(j*PI*((double)2*fd*i*proc.t/proc.nSamples));
}
cx_mat L = diagmat(phase_shift_vec);
#pragma omp parallel for
for (int i = 0; i < proc.interval/proc.nSamples; ++i)
{
cx_vec q = result.R.col(i);
cx_mat Q = join_horiz(L.t()*q, q, L*q);
cx_vec y = result.S.col(i);
//this line
cx_vec z = (eye(proc.nSamples, proc.nSamples) - Q*inv(Q.t()*Q)*Q.t())*y;
result.S.col(i) = z;
}
return result;
}
cx_mat CAF_batches_alt(cx_mat R, cx_mat S, Process proc)
{
cx_mat H = S % conj(R); //cross correlation
H = H.t(); // column = old rows
cx_mat h_mat = ifft(H); //ifft of each column
h_mat = h_mat.t(); // transpose again to get rows
// fftshift(h_mat, 2) swap halves for each row
#pragma omp parallel for
for (int i = 0; i < h_mat.n_rows; ++i)
{
h_mat.row(i) = fftshift(h_mat.row(i));
}
int p = floor(h_mat.n_cols/2);
cx_mat caf = conj(h_mat.cols(p, h_mat.n_cols-1)); //select right half of matrix
cx_mat CAF = fft(caf);
//fftshift(conj,1) swap each col
#pragma omp parallel for
for (int i = 0; i < CAF.n_cols; ++i)
{
cx_rowvec r = CAF.col(i).t();
CAF.col(i) = fftshift(r).t();
}
return CAF;
}
mat CFAR(cx_mat CAF, vec kernel, double pfa)
{
int row_shift = floor(kernel.n_rows/2);
double N = sum(kernel);
//cout << "N: " << N << endl;
double a = N*(pow(pfa,(-1.0/N)) - 1);
mat CAF_squared = abs(pow(CAF,2));
//CAF_squared.save("CAF_squared.csv", csv_ascii);
mat k = zeros(size(CAF));
k(span(0,kernel.n_rows-1), span(0, kernel.n_cols-1)) = kernel;
k = k/N;
cx_mat K = fft2(k);
cx_mat P = conj(K) % fft2(CAF_squared);
cx_mat p_unshift = ifft2(P);
cx_mat p = circshift(p_unshift, row_shift, 0);
//cout << p(span(1, 1), span(1,10)) << endl;
mat inds(size(CAF_squared));
mat PA = real(p*a);
// #pragma omp parallel for
for (int i = 0; i < inds.n_rows; ++i)
{
for (int j = 0; j < inds.n_cols; ++j)
{
if (CAF_squared(i,j) > PA(i,j))
{
inds(i,j) = 1;
} else{
inds(i,j) = 0;
}
}
}
return inds;
}
int main(int argc, char const *argv[])
{
// Load the Receiver data
int dur = 1; // time duration for each recording in seconds
int elems = dur * 240e3; //num of elements per iteration
int16_t r_val, s_val;
ifstream r_file("Channel_0.bin", ios::in | ios::binary);
ifstream s_file("Channel_1.bin", ios::in | ios::binary);
if (!r_file or !s_file)
{
cout << "Error reading data" << endl;
return 0;
}
cout << "\t ECA \t ARD \t CFAR " << endl;
std::vector<cx_double> r_ch_vec;
std::vector<cx_double> s_ch_vec;
cx_double j(0.0, 1.0);
mat output;
mat ARD_total;
for (int run = 0; run < 288; ++run) //288 for full
{
cout << run << "\t";
tic();
// read both files at the same time
// took longer when threaded
//#pragma omp parallel for
for (int file = 0; file < 2; ++file)
{
if (file == 0)
{
int counter = 0;
for (int i = 0; i < 2*elems; ++i)
{
r_file.read(reinterpret_cast<char*>(&r_val), 2);
if (i%2 == 0) // even sample is real
{
r_ch_vec.push_back((double)r_val);
} else { // odd sample is imag
r_ch_vec[counter++] += j*(double)r_val;
}
}
} else {
int counter = 0;
for (int i = 0; i < 2*elems; ++i)
{
s_file.read(reinterpret_cast<char*>(&s_val), 2);
if (i%2 == 0) // even sample is real
{
s_ch_vec.push_back((double)s_val);
} else { // odd sample is imag
s_ch_vec[counter++] += j*(double)s_val;
}
}
}
}
cout << "Done reading data" << endl;
cx_vec r_ch(r_ch_vec);
r_ch_vec.clear();
// cout << r_ch.size() << endl;
cx_vec s_ch(s_ch_vec);
s_ch_vec.clear();
// cout << s_ch.size() << endl;
Process proc;
proc.freq = 99.3e6;
proc.t = dur;
proc.nSamples = 500;
proc.Fs = 240e3; // Hz
proc.interval = proc.t * proc.Fs;
proc.c = 3e8; // m/s
proc.nBatches = 100; //vary this
proc.samples = r_ch.n_elem;
proc.ARDMaxRange_m = 150000;
proc.ARDMaxDoppler_Hz = 200; // around here
proc.TxToRefRxDistance_m = 0;
proc.nSampBatches = 10;
tic();
Cancellation c = ECA_CD(r_ch, s_ch, proc);
double time_taken = toc();
cout << fixed << time_taken << setprecision(5) << "s \t" ;
tic();
cx_mat CAF = reverse(CAF_batches_alt(c.R, c.S, proc));
time_taken = toc();
mat ARD_out = abs(CAF);
cout << fixed << time_taken << setprecision(5) << "s \t" ;
tic();
double max = ARD_out.max(); // expensive operation
ARD_out = ARD_out / max;
ARD_out.save("ARD_out.csv", csv_ascii);
// implement CFAR algorithm
vec kernel = {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1};
double pfa = 1e-5;
mat detections = CFAR(CAF, kernel, pfa);
// combine CFAR results
if (run == 0)
{
output = detections;
//ARD_total = ARD_out;
} else {
output = output + detections;
//ARD_total = ARD_total + ARD_out;
}
//ARD_total.save("ARD_total.csv", csv_ascii);
//detections.save("detections.csv", csv_ascii);
time_taken = toc();
cout << fixed << time_taken << setprecision(5) << "s \t" << endl;
// cout << "Time taken is: " << fixed << time_taken << setprecision(5);
// cout << " sec " << endl;
}
mat::iterator it = output.begin();
mat::iterator it_end = output.end();
// parallel this?
tic();
/*
for(; it != it_end; ++it)
{
if ((*it) > 0)
{
(*it) = 1;
} else {
(*it) = 0;
}
}
*/
double time_taken = toc();
cout << "Time taken is: " << fixed << time_taken << setprecision(5);
cout << " sec " << endl;
output.save("output.csv", csv_ascii);
return 0;
}