-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_reader.m
251 lines (211 loc) · 8.17 KB
/
data_reader.m
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
% AN24_06 -- Range-Doppler basics
% Evaluate the range-Doppler map for a single channel
close all;
% (1) Connect to DemoRad: Check if Brd exists: Problem with USB driver
% (3) Configure RX
% (4) Configure TX
% (5) Start Measurements
% (6) Configure calculation of range profile and range doppler map for
% channel 1
c0 = 3e8;
%--------------------------------------------------------------------------
% Include all necessary directories
%--------------------------------------------------------------------------
CurPath = pwd();
addpath([CurPath,'/../../DemoRadUsb']);
addpath([CurPath,'/../../Class']);
%--------------------------------------------------------------------------
% Setup Connection
%--------------------------------------------------------------------------
Brd = Adf24Tx2Rx4();
Brd.BrdRst();
%--------------------------------------------------------------------------
% Software Version
%--------------------------------------------------------------------------
Brd.BrdDispSwVers();
%--------------------------------------------------------------------------
% Configure Receiver
%--------------------------------------------------------------------------
Brd.RfRxEna();
TxPwr = 80;
NrFrms = 4;
%--------------------------------------------------------------------------
% Configure Transmitter (Antenna 0 - 4, Pwr 0 - 31)
%--------------------------------------------------------------------------
Brd.RfTxEna(1, TxPwr);
%--------------------------------------------------------------------------
% Configure Up-Chirp
% TRamp is only used for chirp rate calculation!!
% TRamp, N, Tp can not be altered in the current framework
% Effective bandwidth is reduced as only 256 us are sampled from the 280 us
% upchirp.
% Only the bandwidth can be altered
%
% The maximum number of chirps is StrtIdx = 0 and StopIdx = 128
%--------------------------------------------------------------------------
Cfg.fStrt = 24.0e9;
Cfg.fStop = 24.3e9;
Cfg.TRampUp = 280e-6;
Cfg.Tp = 284e-6;
Cfg.N = 256;
Cfg.StrtIdx = 0;
Cfg.StopIdx = 64;
Cfg.Np = Cfg.StopIdx - Cfg.StrtIdx;
Brd.RfMeas('Adi', Cfg);
%--------------------------------------------------------------------------
% Read actual configuration
%--------------------------------------------------------------------------
NrChn = Brd.Get('NrChn');
N = Brd.Get('N');
fs = Brd.Get('fs');
%--------------------------------------------------------------------------
% Configure Signal Processing
%--------------------------------------------------------------------------
% Processing of range profile
Win2D = Brd.hanning(N,Cfg.Np);
ScaWin = sum(Win2D(:,1));
NFFT = 2^12;
NFFTVel = 2^8;
kf = (Cfg.fStop - Cfg.fStrt)/Cfg.TRampUp;
vRange = [0:NFFT-1].'./NFFT.*fs.*c0/(2.*kf);
fc = (Cfg.fStop + Cfg.fStrt)/2;
RMin = 0.5;
RMax = 10;
[Val RMinIdx] = min(abs(vRange - RMin));
[Val RMaxIdx] = min(abs(vRange - RMax));
vRangeExt = vRange(RMinIdx:RMaxIdx);
WinVel = Brd.hanning(Cfg.Np);
ScaWinVel = sum(WinVel);
WinVel2D = repmat(WinVel.',numel(vRangeExt),1);
vFreqVel = [-NFFTVel./2:NFFTVel./2-1].'./NFFTVel.*(1/Cfg.Tp);
vVel = vFreqVel*c0/(2.*fc);
%CFAR Implementation
%2D window CA-CFAR
%https://www.mathworks.com/matlabcentral/answers/165561-how-to-write-a-m-file-code-to-cfar-for-fmcw-radar
refLength=10;
guardLength=2;
offset=0.000005;
cfarWin=ones((refLength+guardLength)*2+1,(refLength+guardLength)*2+1); %2D window
cfarWin(refLength+1:refLength+1+2*guardLength,refLength+1:refLength+1+2*guardLength)=0;
cfarWin=cfarWin./sum(cfarWin(:));
% belief is used to track the object
belief = ones(279, 256) / (279*256);
% the range kernel defines how much noise we expect in our motion model
sigma = 3;
sz = 30; % length of gaussFilter vector
x = linspace(-sz / 2, sz / 2, sz);
range_kernel = exp(-x .^ 2 / (2 * sigma ^ 2))';
range_kernel = range_kernel / sum (range_kernel); % normalize
% the doppler kernel defines how much acceleration we can expect
sigma = 60;
sz = 200; % length of gaussFilter vector
x = linspace(-sz / 2, sz / 2, sz);
doppler_kernel = exp(-x .^ 2 / (2 * sigma ^ 2))';
doppler_kernel = doppler_kernel / sum(doppler_kernel); % normalize
dt = 0;
%NEED TO CHANGE THIS TO INF. LOOP?
tic
while true
accum = zeros(279,256);
% datpath='RD/';
% string=[datpath 'RD_' num2str(iter) '.mat'];
% load(string)
%
% Datan = sum(Data,2);
%
% MeasChn = reshape(Datan,N,Cfg.Np);
% % Calculate range profile including calibration
% RP = fft(MeasChn,NFFT,1);%/ScaWin;
% RPExt = RP(RMinIdx:RMaxIdx,:);
Data = Brd.BrdGetData();
dt = toc;
MeasChn = reshape(Data(:,1),N,Cfg.Np);
% Calculate range profile including calibration
RP = fft(MeasChn.*Win2D,NFFT,1).*Brd.FuSca/ScaWin;
RPExt = RP(RMinIdx:RMaxIdx,:);
RD = fft(RPExt.*WinVel2D, NFFTVel, 2)./ScaWinVel;
RD = fftshift(RD, 2);
% Make image of data, note only half of the range is used (other half is invalid)
pmf = abs(RD);
noiseLevel=conv2(pmf,cfarWin,'same');
offset = 1e-6;
cfarThreshold=noiseLevel+offset;
Idx = pmf - cfarThreshold <= 0;
% remove detections around 0 velocity
Idx(:,vVel < 1 & vVel > -1) = 1;
% dilate the mask to get more information about detections
Idx = logical(1 - Idx);
se = strel('disk', 5);
Idx = imdilate(Idx, se);
Idx = logical(1 - Idx);
% normalize data and apply mask
pmf = 10*log10(pmf);
pmf = pmf - min(pmf(:));
pmf = pmf ./ max(pmf(:));
pmf(Idx) = 0.001;
%
% % Tracking algorithm
% Prediction update - applies motion model
for i = 1:size(belief, 2)
% shift ranges according to velocity
if vVel(i) > 0
n = min(floor(vVel(i) * dt + 0.5), 278);
belief(:, i) = [zeros(n, 1); belief(1:end-n, i)];
else
n = min(floor(-vVel(i) * dt + 0.5), 278);
belief(:, i) = [belief(n+1:end, i); zeros(n, 1)];
end
% convolve with range_kernel to model uncertainty in position
belief(:, i) = conv(belief(:, i), range_kernel, 'same');
end
% convolve with doppler_kernel to model uncertainty in velocity
for i = 1:size(belief, 1)
belief(i, :) = conv(belief(i, :), doppler_kernel, 'same');
end
% Correction update - uses range doppler map
for i = 1:size(belief, 1)
for j = 1:size(belief, 2)
belief(i, j) = belief(i, j) * pmf(i, j);
end
end
belief = belief / sum(belief(:));
% subplot(2, 2, 1);
% %imagesc(vVel, vRange(1:128), 10*log10(abs(accum(1:128,:)/4)));
% imagesc(vVel, vRangeExt, abs(RD));
% title('Raw Range-doppler over all channels')
% grid on;
% xlabel('v (m/s)');
% ylabel('R (m)');
% colormap('jet')
% colorbar;
% %caxis([-20 20])
% set(gca,'YDir','normal')
%subplot(2, 2, 2);
%imagesc(vVel, vRangeExt, pmf);
%title('Range-doppler CFAR')
%grid on;
%%xlabel('v (m/s)');
%ylabel('R (m)');
% colormap('jet')
% colorbar;
% caxis([0 1])
% set(gca,'YDir','normal')
%
%subplot(2, 2, 3);
%imagesc(vVel, vRangeExt, belief);
%title('Histogram filter');
%colorbar;
%set(gca,'YDir','normal')
[i, j] = find(belief == max(belief(:)));
%subplot(2, 2, 4);
%plot(vVel(j), vRange(i), 'r*'); %plot the tracker (v,R)
%xlim([vVel(1) vVel(end)]);
%ylim([vRange(1) vRangeExt(end)]);
%title('Tracker');
%colorbar;
if length(i) == 1
fprintf('write status %d', write_detections(vRange(i), vVel(j)));
fprintf('TRACKER: R: %f\t v: %f\n', vRange(i), vVel(j));
end
%pause(0.001)
end