aboutsummaryrefslogtreecommitdiffstats
path: root/dab_tuner.py
blob: d2d6df44574d1cff5b5fd09a1bcc13f1c6aa06d6 (plain)
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
# coding: utf-8

# In[1]:

get_ipython().magic('matplotlib inline')
import matplotlib.pyplot as plt
import numpy as np
import time
import src.gen_source as gen_source
import src.two_tone_lib as tt

import src.tcp_async as tcp_async
import src.tcp_sync as tcp_sync
import src.dab_util as du
import src.dab_tuning_lib as dt


from live_analyse_py import live_analyse_py


# In[15]:

try:
    __IPYTHON__
    reload(tcp_async)
    reload(tcp_sync)
    reload(gen_source)
    reload(tt)
    reload(du)
    reload(dt)
except:
    pass


# In[3]:

sync = tcp_sync.UhdSyncMsg(packet_size=4*8192,
                           packet_type="".join(["f"]*8192))
async = tcp_async.UhdAsyncMsg()


# In[4]:

top = live_analyse_py()


# In[5]:

top.start()


# In[6]:

top.set_txgain(86)
top.set_rxgain(10)


# In[7]:

top.blocks_file_source_0.open("./../dab_normalized_c64.dat", True)


# In[8]:

sync.has_msg()
async.has_msg()


# In[9]:

tt.gen_two_tone(debug = True)


# In[10]:

msgs = sync.get_msgs(1)
msgs = [np.fft.fftshift(msg) for msg in msgs]


# In[18]:

def measure(param):
    n_avg = 20
    x2, x3, x4, x5, x6, x7, x8 = param
    
    repeat = True
    while repeat:
        #tt.gen_two_tone(debug = True, predist=tt.predist_poly, par=(x2, x3, x4))
        
        top.dpd_memless_poly_0.set_a1(1)
        top.dpd_memless_poly_0.set_a2(x2)
        top.dpd_memless_poly_0.set_a3(x3)
        top.dpd_memless_poly_0.set_a4(x4)
        top.dpd_memless_poly_0.set_a5(x5)
        top.dpd_memless_poly_0.set_a6(x6)
        top.dpd_memless_poly_0.set_a7(x7)
        top.dpd_memless_poly_0.set_a8(x8)
        
        sync.has_msg()
        np.array(sync.get_msgs(0.8))
        msgs = np.array(sync.get_msgs(n_avg))
        scores = np.zeros(n_avg)
        msgs = [np.fft.fftshift(msg) for msg in msgs]
        
        if async.has_msg():
            print ("repeat due to async message")
            continue
            
        a = np.array(msgs)
        mean_msg = a.mean(axis = 0)
        suffix = "x_2_%.3f_x_3_%.3f_x_4_%.3fx_5_%.3fx_6_%.3fx_7_%.3fx_8_%.3f" %                 (x2, x3, x4, x5, x6, x7, x8)
        #sig_to_noise = tt.analyse_power_spec(mean_msg, debug=True, debug_path="/tmp/out", suffix=suffix)
        for i in range(n_avg):
            if i == 0:
                scores[i] = dt.calc_signal_sholder_ratio(msgs[0], sampling_rate=8000000, debug=True, debug_path="/tmp/out", suffix=suffix)
            else:
                scores[i] = dt.calc_signal_sholder_ratio(msgs[0], sampling_rate=8000000)
                
        score = np.mean(scores)
        print(score, x2, x3, x4, x5, x6, x7, x8)
        repeat = False
        
        return score


# In[16]:

def simple_opt(pars, i, d, func):
    par = pars[i]
    test_pars = []
    for x in [-1, 0, 1]:
        new_par = list(pars)
        new_par[i] = par + x * d 
        test_pars.append(new_par)
    res = [func(par_new) for par_new in test_pars]
    sel = np.argmax(res)
    best_par = test_pars[sel]
    return best_par

#pars = [1,1,1]
#i_rand = np.random.randint(0, len(pars))
#pars = simple_opt(pars, i_rand, 0.01, lambda x:np.sum(x))
#pars


# In[ ]:

top.set_txgain(86)
top.set_rxgain(5)

pars = np.zeros(7)

for i in range(10000):
    i_rand = np.random.randint(0, len(pars))
    pars = simple_opt(pars, i_rand, 0.005, measure)


# In[ ]:




# In[ ]:




# In[ ]:




# In[15]:

top.set_txgain(85)

params = []
for x2 in np.linspace(-0.1, 0.1, num = 11):
    for x3 in np.linspace(-0.1, 0.1, num = 11):
        for x4 in np.linspace(-0.1, 0.1, num = 11):
            params.append((x2, x3, x4))
            
t_start = time.time()
for idx, param in enumerate(params):
    measure(param)
    time_per_element = (time.time() - t_start) / (idx + 1)
    print ("Time per Element " + str(time_per_element) +
           ", total: " + str(time_per_element * len(params)),
           ", left: " + str(time_per_element * (len(params) - 1 - idx))
          )


# In[ ]:




# In[31]:

sync.stop()
async.stop()
top.stop()
top.wait()


# In[ ]:




# In[ ]:




# In[ ]:




# In[ ]: