Repository navigation
Expand file tree
/
Copy pathOFDM_Radar_System.py
More file actions
224 lines (150 loc) · 7.5 KB
/
Copy pathOFDM_Radar_System.py
File metadata and controls
224 lines (150 loc) · 7.5 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
import numpy as np
import matplotlib.pyplot as plt
import scipy.fft as sp
from OFDM import create_ofdm_without_cp
import pdb
class OFDMRadar:
def __init__(self, carrier_frequency, bandwidth, N_sub_carriers, N_symbols, T_cyclic_prefix, speed_of_light):
self.carrier_frequency = carrier_frequency
self.bandwidth = bandwidth
self.N_sub_carriers = N_sub_carriers
self.N_symbols = N_symbols
self.T_cyclic_prefix = T_cyclic_prefix
self.speed_of_light = speed_of_light
self.sub_carrier_spacing = bandwidth / N_sub_carriers
def DFT_matrix(self, N):
"""Compute the unitary DFT matrix F_n F_n^H = I of size N x N with normalization."""
k = np.arange(N)
n = np.arange(N).reshape((N, 1))
DFT_k_n = np.exp(-2j * np.pi * k * n / N)
return DFT_k_n / np.sqrt(N)
def ICI_matrix(self, velocity):
"""Create ICI matrix based on normalized Doppler frequency."""
k = np.arange(self.N_sub_carriers)
freq_sub_carrier = self.carrier_frequency + k*self.sub_carrier_spacing
normalized_doppler_shift = (-2 * velocity * self.carrier_frequency) / (self.speed_of_light * self.sub_carrier_spacing)
diagonal_elements = np.exp(2j * np.pi * normalized_doppler_shift * k/self.N_sub_carriers)
return np.diag(diagonal_elements/ np.abs(diagonal_elements))
def Phase_rotation_matrix(self, range_):
"""Generate phase rotation matrix based on range of the object."""
tau = 2 * range_ / self.speed_of_light
k = np.arange(self.N_sub_carriers)
freq_sub_carrier = self.carrier_frequency + k*self.sub_carrier_spacing
normalized_time_delay = tau * freq_sub_carrier
diagonal_elements = np.exp(-2j * np.pi * normalized_time_delay)
return np.diag(diagonal_elements/ np.abs(diagonal_elements))
def CPE_matrix(self, velocity):
T = 1 / self.sub_carrier_spacing
# T_symbol = 1 / self.sub_carrier_spacing + self.T_cyclic_prefix # Total symbol time
normalized_doppler_shift = (-2 * velocity * self.carrier_frequency) / (self.speed_of_light * self.sub_carrier_spacing)
alpha = (T + self.T_cyclic_prefix) / T
n = np.arange(self.N_symbols)
phase_shifts = np.exp(2j * np.pi * normalized_doppler_shift * alpha * n) # Phase per symbol
return np.diag(phase_shifts/np.abs(phase_shifts))
def process_signal(self, objects, transmit_signal_S):
"""Calculate the received signal with effects from all target objects."""
Y_tf_ts = np.zeros((self.N_sub_carriers, self.N_symbols), dtype=complex)
F_Nc = np.conjugate(self.DFT_matrix(self.N_sub_carriers)).T # IDFT Calculation
for obj in objects:
amplitude = obj['a']
range_ = obj['r']
velocity = obj['v']
D_ICI = self.ICI_matrix(velocity)
D_phase = self.Phase_rotation_matrix(range_)
D_CPE = self.CPE_matrix(velocity)
a = amplitude*np.exp(-2j*np.pi*self.carrier_frequency*2*range_/self.speed_of_light)
# Apply transformations to simulate received signal
Y_tf_ts += a * D_ICI @ F_Nc @ np.conjugate(D_phase) @ transmit_signal_S @ D_CPE
#Y_tf_ts += amplitude * D_ICI @ F_Nc @ np.conjugate(D_phase) @ transmit_signal_S @ D_CPE
return Y_tf_ts
def convert_to_frequency_domain(self, Y_tf_ts):
""" Calculate the frequency domain OFDM symbols as the next step in the radar processing chain"""
F_Nc = self.DFT_matrix(self.N_sub_carriers)
Y_f_ts = F_Nc @ Y_tf_ts
return Y_f_ts
def element_wise_division_with_S(self, Y_f_ts, transmit_signal_S):
""" Hadamard Division: element wise division of Y_f_ts and S"""
if Y_f_ts.shape != transmit_signal_S.shape:
raise ValueError("Y_f_ts and S must have the same shape for element-wise division.")
epsilon = 1e-10 # Stabilization term
Z_f_ts = Y_f_ts / (transmit_signal_S + epsilon)
# Normalize to prevent excessive power gain
Z_f_ts /= np.sqrt(self.N_sub_carriers)
return Z_f_ts
def convert_to_time_domain(self, Z_f_ts):
""" Calculate the time domain OFDM symbols as the next step in the radar processing chain"""
""" After an element-wise division with the transmit subcarrier symbols, the distance information is extracted by applying the so-called range IDFT along
the subcarriers."""
F_Nc = np.conjugate(self.DFT_matrix(self.N_sub_carriers)).T
Z_t_ts = F_Nc @ Z_f_ts
return Z_t_ts
def range_doppler(self, Z_t_ts):
"""Apply F_sym Doppler DFT to each row of Y_f_ts to obtain the final transformed matrix."""
""" the velocity information can be recovered by applying the so-called Doppler DFT
along the Nsym OFDM symbols. This processing step yields the
desired RDM of size Nc × Nsym"""
F_sym = self.DFT_matrix(self.N_symbols) # Get DFT matrix for symbol dimension
Z_r_v = Z_t_ts @ F_sym # Apply to rows by transposing
# Normalize to avoid excessive power
Z_r_v /= np.sqrt(self.N_symbols)
print(np.abs(Z_r_v[:,0]))
return Z_r_v
def plotRDM(self, Z_r_v):
B = 1e9
fc = 77e9
Nc = 1024
Nsym = 256
Tcp = 1e-6
df = B / Nc
T = 1 / df
c0 = 3e8
dr = c0 / (2 * B)
r_axis = dr * np.arange(Nc)
dv = c0 / (2 * fc * Nsym * (T + Tcp))
v_axis = dv * (np.arange(-Nsym/2+1, Nsym/2))
plt.figure(1)
plt.clf()
plt.imshow(20 * np.log10(np.abs(np.flip(np.fft.fftshift(Z_r_v, axes=1), axis=1))), extent=[v_axis[0], v_axis[-1], r_axis[0], r_axis[-1]], origin="upper", aspect=4.0)
plt.colorbar(label="Power (dB)")
plt.xlabel("Velocity (m/s)")
plt.ylabel("Range (m)")
plt.title("Range Doppler Map")
plt.show()
# Main function to initialize obejects and parameters for Task 3
if __name__ == "__main__":
# Radar parameters
Bandwidth = 1e9
carrier_frequency = 77e9
N_symbols = 256
N_sub_carriers = 1024
T_cyclic_prefix = 1e-6
speed_of_light = 3e8
# Initialize radar system
radar_system = OFDMRadar(
carrier_frequency=carrier_frequency,
bandwidth=Bandwidth,
N_sub_carriers=N_sub_carriers,
N_symbols=N_symbols,
T_cyclic_prefix=T_cyclic_prefix,
speed_of_light=speed_of_light
)
# Define radar target objects
objects = [
{'a': 0.9, 'r': 30.5, 'v': -30}, # Object 1
{'a': 0.9, 'r': 90.5, 'v': 110} # Object 2
]
transmit_signal_S = create_ofdm_without_cp(N_sub_carriers, N_symbols)
# Process the received signal to create Y_tf_ts
Y_tf_ts = radar_system.process_signal(objects, transmit_signal_S)
#print(Y_tf_ts.shape)
## Step 2 Recovering the frequency domain OFDM Symbols from Y_tf_ts by multiplying with DFT matrix
Y_f_ts = radar_system.convert_to_frequency_domain(Y_tf_ts)
# pdb.set_trace()
## Step 3 Element wise Hadamard Division with S : Transmit Data
Z_f_ts = radar_system.element_wise_division_with_S(Y_f_ts, transmit_signal_S)
# Step 4 Apply inverse of the DFT matrix as in step 2 to convert to time domain
Z_t_ts = radar_system.convert_to_time_domain(Z_f_ts)
# Step 5 Row wise DFT transformation
Z_r_v = radar_system.range_doppler(Z_t_ts)
# Plot Range-Doppler Map (RDM)
radar_system.plotRDM(Z_r_v)