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
|
# coding: utf-8
# In[1]:
get_ipython().magic('matplotlib inline')
import numpy as np
import time;
from scipy import signal
import matplotlib.pyplot as plt
import matplotlib.colors as mpcol
import src.dab_util as du
# In[2]:
import src.signal_gen as sg
reload(sg)
reload(du)
# In[3]:
path_in = "./input.dat"
path_out = "./output.dat"
a_max = 0.95
n_steps = 64
amps = np.linspace(0.001, a_max, num = n_steps)
txgains = (50, 55, 60, 65, 70, 75, 81, 82, 83, 84, 85, 86, 87, 88, 89)
rxgains = (50, 40, 40, 25, 25, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20)
# In[4]:
from grc.amam_amap import amam_amap
# In[5]:
top = amam_amap()
# In[6]:
sg.gen_ramps(amplitudes=amps)
# In[7]:
def fftlag(signal_original, signal_rec):
"""
Efficient way to find lag between two signals
Args:
signal_original: The signal that has been sent
signal_rec: The signal that has been recored
"""
c = np.flipud(signal.fftconvolve(signal_original,np.flipud(signal_rec)))
#plt.plot(c)
return np.argmax(c) - signal_original.shape[0] + 1
#pattern = np.array([-2,2,-1,+3,-5,+7])
#delays = [0,1,2,3,4]
#padding = [0]
#padding_fil = [0]
#
#res = []
#for d in delays:
# for p in padding:
# for p2 in padding_fil:
# a = np.concatenate((pattern, np.zeros(p2)))
# b = np.concatenate((np.zeros(d), pattern, np.zeros(p)))
# res.append((d,conv(a,b)))
#res = np.array(res)
#plt.plot(zip(*res)[0], zip(*res)[1], 'p')
# In[ ]:
# In[ ]:
# In[8]:
def get_amp_ratio(ampl_1, ampl_2, a_out_abs, a_in_abs):
idxs = (a_in_abs > ampl_1) & (a_in_abs < ampl_2)
ratio = a_out_abs[idxs] / a_in_abs[idxs]
return ratio.mean(), ratio.var()
def get_phase(ampl_1, ampl_2, a_out, a_in):
idxs = (np.abs(a_in) > ampl_1) & (np.abs(a_in) < ampl_2)
ratio = np.angle(a_out[idxs], deg=True) - np.angle(a_in[idxs], deg=True)
return ratio.mean(), ratio.var()
# In[9]:
def extract_measurement(a_in, a_out, db, a_max, n_steps, debug = False):
a_in = du.crop_signal(a_in)
a_out = du.crop_signal(a_out)
if debug:
plt.plot(np.abs(a_in.real) + 1, color='b');
plt.plot(np.abs(a_out.real), color='g');
plt.show()
#l = min(a_out.shape[0], a_in.shape[0])
#a_out = a_out[0:l]
#a_in = a_in[0:l]
#c = du.lagcorr(np.abs(a_out), np.abs(a_in), 120000)[:,0]
#c = signal.fftconvolve(a_in, a_out) - a_out.shape[0]
delay = fftlag(np.abs(a_in), np.abs(a_out))
#delay = np.argmax(c)
a_out = a_out[delay - 1:]
l = min(a_out.shape[0], a_in.shape[0])
a_out = a_out[0:l]
a_in = a_in[0:l]
if debug:
print ("delay = " + str(delay))
plt.plot(np.abs(a_in), color='g');
plt.plot(np.abs(a_out) - 0.5, color='y');
plt.show()
bins = np.linspace(+0.5/n_steps,a_max + 0.5/n_steps,num=n_steps)
res = []
a_out_abs = np.abs(a_out)
a_in_abs = np.abs(a_in)
for ampl_1, ampl_2 in zip(bins, bins[1:]):
res.append(get_amp_ratio(ampl_1, ampl_2, a_out_abs, a_in_abs))
del a_out_abs
del a_in_abs
mean_amp, var_amp = zip(*res)
res = []
for ampl_1, ampl_2 in zip(bins, bins[1:]):
res.append(get_phase(ampl_1, ampl_2, a_out, a_in))
mean_phase, var_phase = zip(*res)
return mean_amp, var_amp, mean_phase, var_phase, db
# In[ ]:
# In[10]:
res = []
for txgain, rxgain in zip(txgains, rxgains):
print (txgain, rxgain)
res_tmp = None
for i in range(10):
top.uhd_usrp_sink_0_0.set_gain(txgain)
top.uhd_usrp_source_0.set_gain(rxgain)
top.file_sink_out.close()
top.blocks_file_source_0.close()
top.file_sink_out.open(path_out)
top.blocks_file_source_0.open(path_in, False)
top.start()
time.sleep(1)
top.stop()
top.wait()
a_in = np.fromfile(path_in, dtype=np.complex64)
a_out = np.fromfile(path_out, dtype=np.complex64)
res_tmp = extract_measurement(a_in, a_out, txgain, a_max, n_steps, debug=True)
def is_finite(r): return np.all([np.all(np.isfinite(c)) for c in r])
def has_small_jumps(mean_amp): return np.max(np.abs(np.diff(mean_amp))) / np.median(np.abs(np.diff(mean_amp))) < 100
if is_finite(res_tmp) and has_small_jumps(res_tmp[0]):
break
else:
print (is_finite(res_tmp), has_small_jumps(res_tmp[0]))
res.append(res_tmp)
# In[ ]:
# In[47]:
fig = plt.figure(figsize=(10,10))
ax1 = plt.subplot(211)
def plot_with_label(x, y, color, label):
ax1.plot(x, y, color=color, label=txgain)
for idx, (txgain, rxgain) in enumerate(zip(*(txgains, rxgains))):
plot_with_label(
x = amps[1:],
y = 10*np.log(res[idx][0])/np.log(10) - rxgain + 102,
color = mpcol.hsv_to_rgb((idx * 0.75 / len(txgains), 0.6, 1)),
label = txgain
)
ax1.set_ylabel("Gain [dB]")
ax2 = plt.subplot(212)
def plot_with_label(x, y, color, label):
ax2.plot(x, y, color=color, label=txgain)
for idx, (txgain, rxgain) in enumerate(zip(*(txgains, rxgains))):
plot_with_label(
x = amps[1:],
y = res[idx][2],
color = mpcol.hsv_to_rgb((idx * 0.75 / len(txgains), 0.6, 1)),
label = txgain
)
ax2.set_ylabel("Pase [degree]")
ax2.set_xlabel("Amplitude")
#legend
# Shrink current axis by 20%
box = ax1.get_position()
ax1.set_position([box.x0, box.y0, box.width * 0.8, box.height])
box = ax2.get_position()
ax2.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax1.legend(loc='center left', bbox_to_anchor=(1.05, -0.3))
plt.show()
# In[ ]:
# In[ ]:
# In[205]:
# In[ ]:
# In[ ]:
# In[ ]:
|