-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRadar_Plot_SAR.py
326 lines (254 loc) · 9.64 KB
/
Radar_Plot_SAR.py
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
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate as interp
from scipy.signal import hilbert
import time
def lanczos_interp1d(x, y):
a = 3
def finterp(xn):
y_new = np.zeros(xn.shape[0], dtype=y.dtype)
diff = np.ediff1d(x, to_end=x[-1]-x[-2])
for e, xi in enumerate(xn):
if xi < x[0] or xi > x[-1]:
continue
x0 = np.searchsorted(x, xi)
for i in range(max(0, x0-a), min(len(x), x0+a+1)):
z = (xi - x[i]) / diff[i]
y_new[e] += y[i] * np.sinc(z) * np.sinc(z/a)
return y_new
return finterp
def f_to_d(f, bw, sweep_length):
c = 299792458.0
return c*f/(2*(bw/sweep_length))
def r4_normalize(x, d, e=4):
y = np.fft.rfft(x, axis=-1)
n = d[-1]**e
y = y*d**e/n
return np.fft.irfft(y, axis=-1)
def rvp_compensation(x, f, kr):
return x * np.exp(-1j * np.pi * f**2 / kr)
def hilbert_rvp(x, fs, kr):
y = np.fft.fft(x, axis=-1)
y[:,:y.shape[1]//2+1] = 0 # Zero positive frequencies
# Residual video phase term compensation
f = np.linspace(-fs/2, fs/2, y.shape[1])
y *= np.exp(-1j * np.pi * f**2 / kr)
return np.fft.ifft(y, axis=-1)
record_file = open("Radar_Records/radar2v2_horn_48kHz_2024_04_09_16_41_58_parking_lot_sar.txt", "r")
line_counter = 0
data = str(record_file.readline())
line_counter += 1
RECORD_COUNTER = int(int(data[0:len(data) - 1]) * 70 / 100)
print("Record Counter: ", str(RECORD_COUNTER))
data = str(record_file.readline())
line_counter += 1
RECORD_TIME = int(data[0:len(data) - 1])
print("Record Time: ", str(RECORD_TIME), " sec.")
data = str(record_file.readline())
line_counter += 1
SWEEP_TIME = int(data[0:len(data) - 1]) / 1000000
print("Sweep Time : ", str(SWEEP_TIME), " microsec.")
data = str(record_file.readline())
line_counter += 1
SWEEP_DELAY = int(data[0:len(data) - 1]) / 1000000
print("Sweep Delay : ", str(SWEEP_DELAY), " microsec.")
data = str(record_file.readline())
line_counter += 1
SWEEP_START = int(data[0:len(data) - 1])
print("Sweep Start : ", str(SWEEP_START), " Hz")
data = str(record_file.readline())
line_counter += 1
SWEEP_BW = int(data[0:len(data) - 1])
print("Sweep BW : ", str(SWEEP_BW), " Hz")
data = str(record_file.readline())
line_counter += 1
SAMPLING_FREQUENCY = int(data[0:len(data) - 1])
print("Sampling Freqeuncy : ", str(SAMPLING_FREQUENCY), " Hz.")
data = str(record_file.readline())
line_counter += 1
NUMBER_OF_SAMPLES = int(data[0:len(data) - 1])
print("Samples per sweep : ", str(NUMBER_OF_SAMPLES))
data = str(record_file.readline())
line_counter += 1
TX_MODE = int(data[0:len(data) - 1])
print("Tx Mode : ", str(TX_MODE))
data = str(record_file.readline())
line_counter += 1
TX_POWER_DBM = int(data[0:len(data) - 1])
print("Tx Power : ", str(TX_POWER_DBM), " dBm.")
data = str(record_file.readline())
line_counter += 1
TX_POWER_DBM_VOLTAGE = int(data[0:len(data) - 1])
print("Tx Power : ", str(TX_POWER_DBM_VOLTAGE / 100.0), " volts.")
data = str(record_file.readline())
line_counter += 1
hz_per_m = int(data[0:len(data) - 1])
print("Hz per m : ", str(hz_per_m))
data = str(record_file.readline())
line_counter += 1
DATA_LOG = int(data[0:len(data) - 1])
print("Data Log : ", str(DATA_LOG))
data = str(record_file.readline())
line_counter += 1
ADC_SELECT = int(data[0:len(data) - 1])
print("ADC Select : ", str(ADC_SELECT))
data = str(record_file.readline())
line_counter += 1
USB_DATA_TYPE = int(data[0:len(data) - 1])
print("USB Data Type : ", str(USB_DATA_TYPE))
data = str(record_file.readline())
line_counter += 1
ADC_RESOLUTION = int(data[0:len(data) - 1])
print("ADC Resolution : ", str(ADC_RESOLUTION))
data = str(record_file.readline())
line_counter += 1
PHASE_DISTANCE = int(data[0:len(data) - 1])
print("Phase Distance : ", str(PHASE_DISTANCE))
RECORD_DATE = str(record_file.readline())
line_counter += 1
print("Date: ", str(RECORD_DATE))
rs = 0
speed = 1.75
interpolate = 1 #IFFT zero-padding amount, smooths final image
cross_range_padding = 2 #FFT zero-padding amount, increases cross-range with reduced resolution
dynamic_range = 60 #Dynamic range of final image in dB
ky_delta_spacing = 1.80
window = np.hanning
c = 299792458
# rows of a column are adc data and columns are record counte
sample_increment = 1
data_counter = 0 # ignore first recordings until car moves
data1 = np.zeros([int(RECORD_COUNTER/sample_increment)-data_counter, NUMBER_OF_SAMPLES])
for i in range(data_counter):
sample_line = record_file.readline()
sample_counter = 0
while data_counter < RECORD_COUNTER:
sample_line = record_file.readline()
samples_hex = bytes.fromhex(sample_line) # get hex data from string
length_line = len(samples_hex)
if USB_DATA_TYPE == 0:
# at this point samples_hex array have float values of each adc samples
samples_hex_ = [i / 150.0 for i in samples_hex]
samples_float = [i * 3.3 for i in samples_hex_]
data1[sample_counter, :] = samples_float
sample_counter += 1
elif USB_DATA_TYPE == 1:
index = 0
append_counter = 0
while index < length_line:
current_sample_16bit = ((samples_hex[index] & 0xFF) << 8) | (samples_hex[index + 1] & 0xFF)
current_sample_float = (current_sample_16bit / 2 ** ADC_RESOLUTION) * 3.3
index += 2
data1[sample_counter, append_counter] = current_sample_float
append_counter += 1
sample_counter += 1
data_counter += sample_increment
start_time = time.time()
data = np.array(data1)
fs = SAMPLING_FREQUENCY
tsweep = SWEEP_TIME
tdelay = SWEEP_DELAY + ((SWEEP_TIME + SWEEP_DELAY) * (sample_increment - 1))
bw = SWEEP_BW
fc = SWEEP_START + bw/2
sweep_samples = len(data[0])
delta_crange = (tsweep + tdelay) * speed
print('Cross range delta {:.3f} m, {:.3f} lambda'.format(delta_crange, delta_crange/(c/fc)))
f = np.linspace(0, fs/2, sweep_samples//2+1)
d = f_to_d(f, bw, tsweep)
range0 = 0
range1 = c*(fs/2.0)*tsweep/(2*bw)
delta_range = range1/sweep_samples
crange0 = -len(data)*delta_crange/2.0
crange1 = len(data)*delta_crange/2.0
raw_extent = (range0, range1, crange0, crange1)
#Window data
data = data * window(sweep_samples)
print('Sweep points', sweep_samples)
#Hilbert transformation to get complex data
data = hilbert_rvp(data, fs, bw/tsweep)
# plot raw data
if 0:
shdata = 20*np.log10(np.abs(np.real([np.fft.rfft(r) for r in data])))
plt.figure()
plt.title('Raw data, range FFT')
imgplot = plt.imshow(shdata, aspect='auto', interpolation='none', extent=raw_extent)
plt.xlabel('Range [m]')
plt.ylabel('Cross-range [m]')
m = np.max(shdata)
#Limit the dynamic range to clean the rounding errors
imgplot.set_clim(m-dynamic_range,m)
if 0:
plt.figure()
plt.title('Raw data')
kr0 = (4*np.pi/c)*(fc - bw/2)
kr1 = (4*np.pi/c)*(fc + bw/2)
plt.imshow(data.real, aspect='auto', interpolation='none', extent=(kr0, kr1, crange0, crange1))
plt.xlabel('Range wavenumber [1/m]')
plt.ylabel('Cross-range [m]')
plt.show()
#Zeropad cross-range
if cross_range_padding > 1:
zpad = int((cross_range_padding - 1)*data.shape[0])
data = np.pad(data, ((zpad//2, zpad//2), (0, 0)), 'constant')
kx = np.linspace(-np.pi/delta_crange, np.pi/delta_crange, len(data))
kr = np.linspace(((4*np.pi/c)*(fc - bw/2)), ((4*np.pi/c)*(fc + bw/2)), sweep_samples);
#along the track fft
cfft = np.fft.fftshift(np.fft.fft(data, axis=0), 0)
if 0:
plt.figure()
plt.title('Along track FFT phase')
plt.imshow(np.angle(cfft), aspect='auto', extent=[kr[0], kr[-1], kx[0], kx[-1]])
plt.figure()
plt.title('Along track FFT magnitude')
plt.imshow(np.abs(cfft), aspect='auto', extent=[kr[0], kr[-1], kx[0], kx[-1]])
#matched filter
if rs != 0:
phi_mf = np.zeros(cfft.shape)
for ii in range(cfft.shape[1]):
for jj in range(cfft.shape[0]):
phi_mf = rs*(kr[ii]**2-kx[jj]**2 )**0.5
smf = np.exp(1j*phi_mf)
cfft = cfft*smf
#ky0 = (-1*(kr[0]**2 - kx[0]**2))**0.5 # kr - kx is negative gives nan
ky0 = (kr[0]**2 - kx[0]**2)**0.5
kr_delta = kr[1] - kr[0]
ky_delta = ky_delta_spacing * kr_delta
ky_even = np.arange(ky0, kr[-1], ky_delta)
st = np.zeros((cfft.shape[0], len(ky_even)), dtype=np.complex_(1))
print("entering slot interpolation")
#Stolt interpolation
for i in range(len(kx)):
ky = (kr**2 - kx[i]**2)**0.5
ci = interp.interp1d(ky, cfft[i], fill_value=0, bounds_error=False)
#ci = lanczos_interp1d(ky, cfft[i])
st[i,:] = ci(ky_even)
print("finished slot interpolation")
if 0:
plt.figure()
plt.title('Stolt interpolation phase')
plt.imshow(np.angle(st), aspect='auto', extent=[ky_even[0], ky_even[-1], kx[0], kx[-1]])
plt.figure()
plt.title('Stolt interpolation magnitude')
plt.imshow(np.abs(st), aspect='auto', extent=[ky_even[0], ky_even[-1], kx[0], kx[-1]])
#Window again
#wx = window(st.shape[0])
#wy = window(st.shape[1])
#w = np.sqrt(np.outer(wx, wy))
#st = st * w
end_time = time.time()
print("calculation time:",end_time-start_time)
#IFFT to generate the image
st = np.fft.ifft2(st)
st_sum = np.sum(np.abs(st))
print('Entropy', -np.sum((np.abs(st)/st_sum) * np.log(np.abs(st)/st_sum)))
#Cross-range size of image in meters
crange = delta_crange*st.shape[0]/interpolate
max_range = range1 * ky_delta / (2 * kr_delta)
plt.figure()
plt.title('SAR Image')
st = 20*np.log10(np.abs(st))
imgplot = plt.imshow(st, aspect='auto', interpolation='none', extent=[0, range1,-crange/2.0,crange/2.0], origin='lower')
m = np.max(st)
#Limit the dynamic range to clean the rounding errors
imgplot.set_clim(m-dynamic_range,m)
plt.show()