-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignalExamples.py
More file actions
207 lines (140 loc) · 5.14 KB
/
signalExamples.py
File metadata and controls
207 lines (140 loc) · 5.14 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
import numpy as np
import scipy.signal
import matplotlib.pyplot as plt
def vanilla_signal_e(t, *args):
return np.exp(-2 * np.pi * 1j * t)
def lfm_signal_e(t, t_range, f0, k, flip=False):
if t_range is None:
return np.exp(-(f0 - k * t/2) * 2 * np.pi * 1j * t)
elif not flip:
t0, t1 = t_range[0], t_range[1]
out = np.zeros(t.shape[0], dtype=np.complex128)
for i, t_i in enumerate(t):
if t0 <= t_i and t_i <= np.abs(t1):
out[i] = np.exp(-(2 * np.pi * 1j * (t_i - t0) * (f0 + k * (t_i - t0) / 2)))
return out
elif flip:
t0, t1 = t_range[0], t_range[1]
out = np.zeros(t.shape[0], dtype=np.complex128)
for i, t_i in enumerate(t):
if t0 <= t_i and t_i <= np.abs(t1):
# out[i] = np.exp(-(2 * np.pi * 1j * (f0 + k * t1) * (t_i - t1) + np.pi * 1j * -k * (t_i - t1)**2))
out[i] = np.exp(-(2 * np.pi * 1j * (t_i - t0) * (f0 + (t1 - t0) * k - k * (t_i - t0) / 2)))
return out
def get_real_iq(x):
return np.abs(x) * np.cos(np.angle(x))
def lfm_phi(t, t_range, f0, k):
t0, t1 = t_range
out = np.zeros(t.shape[0], dtype=np.complex128)
t_out = np.array([t_i for t_i in t if (t0 <= t_i and t_i <= t1)])
out = f0 + k * (t_out - t0) / 2
return t_out, out
def lfm_demo0():
f0 = 1 # baseband freq
k = 1 # chrip-rate
n_pts = 1.0e3
t_min = 0
t_max = 4 * np.pi
t = np.linspace(t_min, t_max, int(n_pts))
range_0 = [2, 6]
range_1 = [5, 9]
signal_0 = lfm_signal_e(t, range_0, f0, k, False)
signal_1 = lfm_signal_e(t, range_1, f0, k, False)
_, ax = plt.subplots(2, figsize=(10, 20))
for a in ax:
a.set_xticks([], [])
a.set_yticks([], [])
a.set_yticklabels([])
a.set_xticklabels([])
ax[0].plot(t, signal_0)
ax[0].plot(t, signal_1)
ax[0].set_ylim(-3, 3)
ax[0].set_title("Time Domain LFM Data")
ax[1].plot(*lfm_phi(t, range_0, f0, k))
ax[1].plot(*lfm_phi(t, range_1, f0, k))
ax[1].set_xlim(t_min, t_max)
ax[1].set_title("Freq Domain LFM Data")
ax[0].set_ylabel("Amplitude")
ax[0].set_xlabel("Time")
ax[1].set_ylabel("Frequency")
ax[1].set_xlabel("Time")
plt.show()
def lfm_demo1():
f0 = 0.1 # baseband freq
k = 0.2 # chrip-rate
n_pts = 1.0e3
t_min = 0
t_max = 4 * np.pi
t = np.linspace(t_min, t_max, int(n_pts))
range_0 = [2, 10]
signal_0 = lfm_signal_e(t, range_0, f0, k, False)
_, ax = plt.subplots(2, figsize=(10, 20))
ax[0].plot(t, signal_0)
ax[0].set_title("Time Domain LFM Signal")
ax[1].plot(*lfm_phi(t, range_0, f0, k))
ax[1].set_xlim(t_min, t_max)
ax[1].set_title("Frequency Domain LFM Signal")
for a in ax:
a.set_xticks([], [])
a.set_yticks([], [])
a.set_yticklabels([])
a.set_xticklabels([])
ax[0].set_ylabel("Amplitude")
ax[0].set_xlabel("Time")
ax[1].set_ylabel("Frequency")
ax[1].set_xlabel("Time")
plt.show()
def lfm_demo2():
f0 = 0 # baseband freq
k = 20 # chrip-rate
n_pts = 500
noise = np.random.normal(0, 0.5, n_pts) # mean, sigma n_pts
# noise = 0.5*np.random.randn(n_pts)
t = np.linspace(0, 2 * np.pi, n_pts)
signal_rx0 = lfm_signal_e(t, [1, 2], f0, k, True) # signals from different reflections
# signal_tx = lfm_signal_e(t, [0, 1], )
def main():
f0 = 0 # baseband freq
k = 20 # chrip-rate
n_pts = 100000
t_min = 0
t_max = 4 * np.pi
snr = 1
noise = np.random.normal(0, snr, n_pts) # mean, sigma n_pts
# noise = 0.5*np.random.randn(n_pts)
t = np.linspace(t_min, t_max, n_pts)
signal_rx0 = lfm_signal_e(t, [2, 3], f0, k, True) # signals from different reflections
signal_rx1 = lfm_signal_e(t, [4, 5], f0, k, True) # time reversed
a0 = 0.8 # attenuation factors
a1 = 0.4
signal_tx = lfm_signal_e(t, [0, 1], f0, k)
signal_tx_flipped = lfm_signal_e(t, [0, 1], f0, k, True)
signal_rx = a0 * signal_rx0# + a1 * signal_rx1
# signal_rx = a0 * signal_rx1
signal_rx = a0 * signal_rx0 + a1 * signal_rx1
signal_rx_noise = signal_rx + noise
signal_correlated = scipy.signal.correlate(signal_rx, signal_tx_flipped, mode="same")
signal_correlated_noise = scipy.signal.correlate(signal_rx_noise, signal_tx_flipped, mode="same")
_, ax = plt.subplots(5, sharex=True, figsize=(10, 20))
ax[0].plot(t, get_real_iq(signal_tx))
ax[0].set_title("Transmitted Signal")
ax[1].plot(t, get_real_iq(signal_rx))
ax[1].set_title("Reflected Signals")
ax[2].plot(t, get_real_iq(signal_rx_noise))
ax[2].set_title("Received Noisy Signal")
ax[3].plot(t, get_real_iq(signal_correlated))
ax[3].set_title("Correlated Signal No Noise")
ax[4].plot(t, get_real_iq(signal_correlated_noise))
ax[4].set_title("Correlated Signal With Noise")
for a in ax:
a.set_xticks([], [])
a.set_yticks([], [])
a.set_yticklabels([])
a.set_xticklabels([])
ax[-1].set_xlabel("Time")
# plt.plot(t, get_real_iq(signal_rx0))
plt.show()
if __name__ == "__main__":
# main()
# lfm_demo1()
lfm_demo0()