forked from ymartin101/QuickRAD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPD_Multiple_OS.m
More file actions
340 lines (295 loc) · 15.9 KB
/
Copy pathPD_Multiple_OS.m
File metadata and controls
340 lines (295 loc) · 15.9 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
%% File information
% Written by M. Y. Martin (MRTMOG014)
% EEE4022S (2018)
% PD_Multiple_OS.m: Determine PD for a varying parameter in a "multiple" dataset using OS-CFAR
%% FUNCTION: Used to determine PFA and PD for a varying parameter
function PD_mean = PD_Multiple_OS(target_doppler)
%% Add multiple targets to a clutter dataset and determine PD
clc;
close all;
%% Inputs
load('CFA17_001.mat'); % loads variables: Cdata, Info, NumOfPRIs, NumOfRangeBins, PCI, PRI_s, Waveform
a = 25;
N = 16;
k = round(3*N/4);
NFFT = 512; % length of window for FFT
clustering_threshold = 0.001;
% target_doppler = 600;
%% Setup
% Sampling frequency, period
PRI = PRI_s;
PRF = 1/PRI;
fD = ceil(((target_doppler) + (PRF/2))*NFFT/PRF);
fD1 = (NFFT/2) - (fD - (NFFT/2));
kc1_mat = zeros(NumOfRangeBins,1);
kc2_mat = zeros(NumOfRangeBins,1);
PD_mat1 = zeros(NumOfRangeBins,1);
PD_mat2 = zeros(NumOfRangeBins,1);
PD_mat3 = zeros(NumOfRangeBins,1);
%% Parameters
c0 = 299792458; % speed of light
lambda = c0/(6.9*(10^9)); % wavelength of the radar; f = 6.9 GHz for the clutter datasets
v_target = lambda*target_doppler/2; % target speed [m/s]
dt = 15/v_target; % v_target = dRangeBin/dt = 15/dt (1 RangeBin = 15 m)
% Spectrogram
overlap = NFFT/2; % overlap in samples
window_length = NFFT;
% Get number of target cells
X = Cdata(1:NumOfPRIs,1);
kc = floor((length(X) - overlap)/(window_length - overlap));
% For target insertion
C_backup = Cdata;
t1 = 0;
t2 = 0;
SINR = 0; % dB
break_loop = 0;
RBMax = -1;
% %% CFAR for PFA
% summation = zeros(kc,NumOfRangeBins);
% for RangeBin = 1:NumOfRangeBins
% X = Cdata(1:NumOfPRIs,RangeBin);
% [S,F,T1,P] = spectrogram(X,window_length,overlap,NFFT,PRF);
% kc = floor((length(X) - overlap)/(window_length - overlap));
% signal = fftshift(P,1);
%
% % matrix containing background statistics
% g = zeros(NFFT,kc); % NFFT x kc matrix
%
% % CUT and reference cells created + shifted for each time instance
% for CUT = ((N/2) + 1):1:(NFFT - (N/2))
% window_sorted = sort([signal(((CUT + 1):(CUT + N/2)),:); signal(((CUT - N/2):(CUT - 1)),:)]); % sort window samples in ascending order
% g(CUT,:) = window_sorted(k,:);
% end
%
% % Extract the data of the detected elements
% T = a.*g; % CFAR detection threshold; NFFT x kc matrix
% signal_minus_T = signal - T; % NFFT x kc matrix
% detections_index = find(signal_minus_T > 0);% returns indices (counting down columns) of values > 0
%
% detection_result = zeros(NFFT,kc);
% detection_result(detections_index) = signal_minus_T(detections_index); % same as signal_minus_T, but non-detections are all zero
% summation(:,(RangeBin)) = fftshift(sum(detection_result,1),1); % sum the columns; produce row; becomes column in summation
%
% clc;
% fprintf('Processing: %0.2f %% complete \n',RangeBin*100/NumOfRangeBins);
% end
%
% % Calculate PFA
% detections_index = find(summation > clustering_threshold); % max number of entries = kc x NumOfRangeBins
% PFA = length(detections_index)/(kc*NumOfRangeBins); % calculate probability of false alarm
%
%% CFAR for PD
%% Range bins with only clutter
if abs(target_doppler) <= 20
RangeBin = 1;
t1 = 1;
t2 = NumOfPRIs; % update t2 with new t1 (PRIs)
kc1 = t1*kc/NumOfPRIs;
kc1_mat(RangeBin) = (kc1);
kc1_mat(49 - RangeBin) = (kc1);
kc2 = t2*kc/NumOfPRIs;
kc2_mat(RangeBin) = (kc2);
kc2_mat(49 - RangeBin) = (kc2);
%% Insert first target (towards radar)
H0 = Cdata(:,(49 - RangeBin)); % column vector from Cdata; flipped plot
num_samples = length(H0);
H0_power = var(H0); % assume Gaussian PDF (incorrect, but just a first order estimate)
% Compute target power
target_power = (H0_power)*10^(SINR/10); % target power - linear
% Steering vector is used to incorporate the speed of the target
m = (0:1:(num_samples - 1)).';
vd = target_doppler/PRF;
v = exp(1j*2*pi*m*vd);
% Calculate target signal
target_amplitude = sqrt(target_power); % target amplitude (Swerling I model)
target_signal = repmat(target_amplitude,num_samples,1).*v; % target signal (Swerling I model)
% Add the simulated target to the measured clutter
H1 = H0 + target_signal;
C_backup(:,(49 - RangeBin)) = H1;
%% Insert second target (away from radar)
H0 = Cdata(:,RangeBin); % column vector from Cdata; use RangeBin
num_samples = length(H0);
% Estimate power of noise and clutter
H0_power = var(H0); % assume Gaussian PDF (incorrect, but just a first order estimate)
% Compute target power
target_power = (H0_power)*10^(SINR/10); % target power - linear
% Steering vector is used to incorporate the speed of the target
m = (0:1:(num_samples - 1)).';
vd = -target_doppler/PRF;
v = exp(1j*2*pi*m*vd);
% Calculate target signal
target_amplitude = sqrt(target_power); % target amplitude (Swerling I model)
target_signal = repmat(target_amplitude,num_samples,1).*v; % target signal (Swerling I model)
% Add the simulated target to the measured clutter
H1 = H0 + target_signal;
C_backup(:,RangeBin) = H1;
else
for RangeBin = 1:NumOfRangeBins
t1 = ceil((t2/PRF)*PRF); % update t1 with previous t2 (PRIs)
t2 = (t1/PRF + dt)*PRF; % gradient of v_target
if t2 > size(Cdata,1)
t2 = NumOfPRIs;
RBMax = RangeBin;
break_loop = 1;
end
kc1 = t1*kc/NumOfPRIs;
if t1 == 0
kc1 = 1;
end
% transform time values to kc values
kc1_mat(RangeBin) = (kc1);
kc2 = t2*kc/NumOfPRIs;
kc2_mat(RangeBin) = (kc2);
%% Insert first target (towards radar)
H0 = Cdata(t1:t2,(49 - RangeBin)); % column vector from Cdata; flipped plot
num_samples = length(H0);
H0_power = var(H0); % assume Gaussian PDF (incorrect, but just a first order estimate)
% Compute target power
target_power = (H0_power)*10^(SINR/10); % target power - linear
% Steering vector is used to incorporate the speed of the target
m = (0:1:(num_samples - 1)).';
vd = target_doppler/PRF;
v = exp(1j*2*pi*m*vd);
% Calculate target signal
target_amplitude = sqrt(target_power); % target amplitude (Swerling I model)
target_signal = repmat(target_amplitude,num_samples,1).*v; % target signal (Swerling I model)
% Add the simulated target to the measured clutter
H1 = H0 + target_signal;
C_backup(t1:t2,(49 - RangeBin)) = H1;
%% Insert second target (away from radar)
H0 = Cdata(t1:t2,RangeBin); % column vector from Cdata; use RangeBin
num_samples = length(H0);
H0_power = var(H0); % assume Gaussian PDF (incorrect, but just a first order estimate)
% Compute target power
target_power = (H0_power)*10^(SINR/10); % target power - linear
% Steering vector is used to incorporate the speed of the target
m = (0:1:(num_samples - 1)).';
vd = -target_doppler/PRF;
v = exp(1j*2*pi*m*vd);
% Calculate target signal
target_amplitude = sqrt(target_power); % target amplitude (Swerling I model)
target_signal = repmat(target_amplitude,num_samples,1).*v; % target signal (Swerling I model)
% Add the simulated target to the measured clutter
H1 = H0 + target_signal;
C_backup(t1:t2,RangeBin) = H1;
if break_loop == 1
break
end
end
end
%% OSGO-CFAR on target dataset; for PD
kc1_mat_rvs = sort(kc1_mat,'descend');
kc2_mat_rvs = sort(kc2_mat,'descend');
% CFAR detection
for RangeBin = 1:NumOfRangeBins % test a small sample to quicken processing
X = C_backup(1:NumOfPRIs,RangeBin);
[S,F,T1,P] = spectrogram(X,window_length,overlap,NFFT,PRF);
signal = fftshift(P,1);
% matrix containing background statistics
g = zeros(NFFT,kc); % NFFT x kc matrix
% CUT and reference cells created + shifted for each time instance
for CUT = ((N/2) + 1):1:(NFFT - (N/2))
window_sorted = sort([signal(((CUT + 1):(CUT + N/2)),:); signal(((CUT - N/2):(CUT - 1)),:)]); % sort window samples in ascending order
g(CUT,:) = window_sorted(k,:);
end
% Extract the data of the detected elements
T = a.*g; % CFAR detection threshold; NFFT x kc matrix
signal_minus_T = signal - T; % NFFT x kc matrix
detections_index = find(signal_minus_T > 0);% returns indices (counting down columns) of values > 0
%% Determine PD; consider all cases of targets crossing, not crossing, etc. (depends on speed)
if (abs(target_doppler) <= 20)
% check fD and fD1
difference2 = signal_minus_T(fD1,kc1_mat(1):kc2_mat(1));
difference1 = signal_minus_T(fD,(kc1_mat(48)):(kc2_mat(48)));
target_detections = length(find(difference2 > 0));
low_freq_1_1 = target_detections/length(kc1_mat(1):kc2_mat(1));
target_detections = length(find(difference1 > 0));
low_freq_48_1 = target_detections/length(kc1_mat(48):kc2_mat(48));
% check fD+1 and fD1+1
difference2 = signal_minus_T(fD1+1,kc1_mat(1):kc2_mat(1));
difference1 = signal_minus_T(fD+1,(kc1_mat(48)):(kc2_mat(48)));
target_detections = length(find(difference2 > 0));
low_freq_1_2 = target_detections/length(kc1_mat(1):kc2_mat(1));
target_detections = length(find(difference1 > 0));
low_freq_48_2 = target_detections/length(kc1_mat(48):kc2_mat(48));
% check fD+2 and fD1+2
difference2 = signal_minus_T(fD1+2,kc1_mat(1):kc2_mat(1));
difference1 = signal_minus_T(fD+2,(kc1_mat(48)):(kc2_mat(48)));
target_detections = length(find(difference2 > 0));
low_freq_1_3 = target_detections/length(kc1_mat(1):kc2_mat(1));
target_detections = length(find(difference1 > 0));
low_freq_48_3 = target_detections/length(kc1_mat(48):kc2_mat(48));
% check fD-1 and fD1-1
difference2 = signal_minus_T(fD1-1,kc1_mat(1):kc2_mat(1));
difference1 = signal_minus_T(fD-1,(kc1_mat(48)):(kc2_mat(48)));
target_detections = length(find(difference2 > 0));
low_freq_1_4 = target_detections/length(kc1_mat(1):kc2_mat(1));
target_detections = length(find(difference1 > 0));
low_freq_48_4 = target_detections/length(kc1_mat(48):kc2_mat(48));
% calculate best PD
PD_mat1(1) = max([low_freq_1_1, low_freq_1_2, low_freq_1_3, low_freq_1_4]);
PD_mat1(48) = max([low_freq_48_1, low_freq_48_2, low_freq_48_3, low_freq_48_4]);
elseif fD == NFFT % target_doppler = 2500 Hz
difference2 = signal_minus_T((fD1+1),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference1 = signal_minus_T(fD,(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
target_detections = (length(find(difference1 > 0)) + length(find(difference2 > 0)));
PD_mat1(RangeBin) = target_detections/(length(kc1_mat(RangeBin):kc2_mat(RangeBin)) + length((kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin))));
% targets do not cross, do nothing where there is no data
elseif (RangeBin > (RBMax)) && (RangeBin < (49 - (RBMax))) && (RBMax > 0) && (RBMax < 25)
continue
% targets do not cross, check RHS
elseif (RangeBin <= (RBMax)) && (RBMax > 0) && (RBMax < 25)
difference1 = signal_minus_T(fD1,kc1_mat(RangeBin):kc2_mat(RangeBin));
difference3 = signal_minus_T((fD1+1),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference5 = signal_minus_T((fD1+2),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference7 = signal_minus_T((fD1-1),kc1_mat(RangeBin):kc2_mat(RangeBin));
diff1_max = max([length(find(difference1 > 0)),length(find(difference3 > 0)),length(find(difference5 > 0)),length(find(difference7 > 0))]);
target_detections = diff1_max;
PD_mat1(RangeBin) = target_detections/(length(kc1_mat(RangeBin):kc2_mat(RangeBin)));
% targets do not cross, check RHS
elseif (RangeBin >= (49 - (RBMax))) && (RBMax > 0) && (RBMax < 25)
difference2 = signal_minus_T(fD1,(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference4 = signal_minus_T((fD1+1),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference6 = signal_minus_T((fD1+2),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference8 = signal_minus_T((fD1-1),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
diff2_max = max([length(find(difference2 > 0)),length(find(difference4 > 0)),length(find(difference6 > 0)),length(find(difference8 > 0))]);
target_detections = diff2_max;
PD_mat1(RangeBin) = target_detections/(length((kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin))));
% targets cross, check RHS of no overlap
elseif (RangeBin > (RBMax)) && (RBMax > 24)
difference2 = signal_minus_T(fD,(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference4 = signal_minus_T((fD+1),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference6 = signal_minus_T((fD+2),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference8 = signal_minus_T((fD-1),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
diff2_max = max([length(find(difference2 > 0)),length(find(difference4 > 0)),length(find(difference6 > 0)),length(find(difference8 > 0))]);
target_detections = diff2_max;
PD_mat1(RangeBin) = target_detections/(length((kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin))));
% targets cross, check LHS of no overlap
elseif (RangeBin < (49 - (RBMax))) && (RBMax > 24)
difference1 = signal_minus_T(fD1,kc1_mat(RangeBin):kc2_mat(RangeBin));
difference3 = signal_minus_T((fD1+1),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference5 = signal_minus_T((fD1+2),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference7 = signal_minus_T((fD1-1),kc1_mat(RangeBin):kc2_mat(RangeBin));
diff1_max = max([length(find(difference1 > 0)),length(find(difference3 > 0)),length(find(difference5 > 0)),length(find(difference7 > 0))]);
target_detections = diff1_max;
PD_mat1(RangeBin) = target_detections/(length(kc1_mat(RangeBin):kc2_mat(RangeBin)));
% targets cross, check overlaps
else
difference1 = signal_minus_T(fD1,kc1_mat(RangeBin):kc2_mat(RangeBin));
difference2 = signal_minus_T(fD,(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference3 = signal_minus_T((fD1+1),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference4 = signal_minus_T((fD+1),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference5 = signal_minus_T((fD1+2),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference6 = signal_minus_T((fD+2),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
difference7 = signal_minus_T((fD1-1),kc1_mat(RangeBin):kc2_mat(RangeBin));
difference8 = signal_minus_T((fD-1),(kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin)));
diff1_max = max([length(find(difference1 > 0)),length(find(difference3 > 0)),length(find(difference5 > 0)),length(find(difference7 > 0))]);
diff2_max = max([length(find(difference2 > 0)),length(find(difference4 > 0)),length(find(difference6 > 0)),length(find(difference8 > 0))]);
target_detections = diff1_max + diff2_max;
PD_mat1(RangeBin) = target_detections/(length(kc1_mat(RangeBin):kc2_mat(RangeBin)) + length((kc1_mat(49-RangeBin)):(kc2_mat(49-RangeBin))));
end
clc;
fprintf('Processing: %0.2f %% complete \n',RangeBin*100/NumOfRangeBins);
end
PD_mean = mean(PD_mat1(PD_mat1 > 0)); % average PD from all range bins
end