aboutsummaryrefslogtreecommitdiffstats
path: root/two_tone_tuner.ipynb
blob: 02724015d0542297c40f001bd0c0a0af99c6ff7b (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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import time\n",
    "import src.gen_source as gen_source\n",
    "import src.two_tone_lib as tt\n",
    "\n",
    "import src.tcp_async as tcp_async\n",
    "import src.tcp_sync as tcp_sync\n",
    "\n",
    "from live_analyse_py import live_analyse_py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    __IPYTHON__\n",
    "    reload(tcp_async)\n",
    "    reload(tcp_sync)\n",
    "    reload(gen_source)\n",
    "    reload(tt)\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sync = tcp_sync.UhdSyncMsg(packet_size=4*8192,\n",
    "                           packet_type=\"\".join([\"f\"]*8192))\n",
    "async = tcp_async.UhdAsyncMsg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "top = live_analyse_py()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "top.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sync.has_msg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def measure(param):\n",
    "    x2 = param[0]\n",
    "    x3 = param[1]\n",
    "    x4 = param[2]\n",
    "    repeat = True\n",
    "    while repeat:\n",
    "        tt.gen_two_tone(debug = True, predist=tt.predist_poly, par=(x2, x3, x4))\n",
    "        sync.has_msg()\n",
    "        np.array(sync.get_msgs(2))\n",
    "        msgs = np.array(sync.get_msgs(5))\n",
    "        msgs = [np.fft.fftshift(msg) for msg in msgs]\n",
    "        \n",
    "        if async.has_msg():\n",
    "            continue\n",
    "            \n",
    "        a = np.array(msgs)\n",
    "        mean_msg = a.mean(axis = 0)\n",
    "        suffix = \"x_2_%.3f_x_3_%.3f_x_4_%.3f\" % (x2, x3, x4)\n",
    "        sig_to_noise = tt.analyse_power_spec(mean_msg, debug=True, debug_path=\"/tmp/out\", suffix=suffix)\n",
    "        print(sig_to_noise, x2, x3, x4)\n",
    "        repeat = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "top.set_txgain(85)\n",
    "\n",
    "params = []\n",
    "for x2 in np.linspace(-0.1, 0.1, num = 11):\n",
    "    for x3 in np.linspace(-0.1, 0.1, num = 11):\n",
    "        for x4 in np.linspace(-0.1, 0.1, num = 11):\n",
    "            params.append((x2, x3, x4))\n",
    "            \n",
    "t_start = time.time()\n",
    "for idx, param in enumerate(params):\n",
    "    measure(param)\n",
    "    time_per_element = (time.time() - t_start) / (idx + 1)\n",
    "    print (\"Time per Element \" + str(time_per_element) +\n",
    "           \", total: \" + str(time_per_element * len(params)),\n",
    "           \", left: \" + str(time_per_element * (len(params) - 1 - idx))\n",
    "          )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sync.stop()\n",
    "async.stop()\n",
    "top.stop()\n",
    "top.wait()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}