aboutsummaryrefslogtreecommitdiffstats
path: root/dab_tuner.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'dab_tuner.ipynb')
-rw-r--r--dab_tuner.ipynb324
1 files changed, 324 insertions, 0 deletions
diff --git a/dab_tuner.ipynb b/dab_tuner.ipynb
new file mode 100644
index 0000000..635ee35
--- /dev/null
+++ b/dab_tuner.ipynb
@@ -0,0 +1,324 @@
+{
+ "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",
+ "import src.dab_util as du\n",
+ "import src.dab_tuning_lib as dt\n",
+ "\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",
+ " reload(du)\n",
+ " reload(dt)\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": [
+ "top.set_txgain(86)\n",
+ "top.set_rxgain(10)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "top.blocks_file_source_0.open(\"./../dab_normalized_c64.dat\", True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "sync.has_msg()\n",
+ "async.has_msg()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "tt.gen_two_tone(debug = True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "msgs = sync.get_msgs(1)\n",
+ "msgs = [np.fft.fftshift(msg) for msg in msgs]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def measure(param):\n",
+ " n_avg = 20\n",
+ " x2, x3, x4, x5, x6, x7, x8 = param\n",
+ " \n",
+ " repeat = True\n",
+ " while repeat:\n",
+ " #tt.gen_two_tone(debug = True, predist=tt.predist_poly, par=(x2, x3, x4))\n",
+ " \n",
+ " top.dpd_memless_poly_0.set_a1(1)\n",
+ " top.dpd_memless_poly_0.set_a2(x2)\n",
+ " top.dpd_memless_poly_0.set_a3(x3)\n",
+ " top.dpd_memless_poly_0.set_a4(x4)\n",
+ " top.dpd_memless_poly_0.set_a5(x5)\n",
+ " top.dpd_memless_poly_0.set_a6(x6)\n",
+ " top.dpd_memless_poly_0.set_a7(x7)\n",
+ " top.dpd_memless_poly_0.set_a8(x8)\n",
+ " \n",
+ " sync.has_msg()\n",
+ " np.array(sync.get_msgs(0.8))\n",
+ " msgs = np.array(sync.get_msgs(n_avg))\n",
+ " scores = np.zeros(n_avg)\n",
+ " msgs = [np.fft.fftshift(msg) for msg in msgs]\n",
+ " \n",
+ " if async.has_msg():\n",
+ " print (\"repeat due to async message\")\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_%.3fx_5_%.3fx_6_%.3fx_7_%.3fx_8_%.3f\" % \\\n",
+ " (x2, x3, x4, x5, x6, x7, x8)\n",
+ " #sig_to_noise = tt.analyse_power_spec(mean_msg, debug=True, debug_path=\"/tmp/out\", suffix=suffix)\n",
+ " for i in range(n_avg):\n",
+ " if i == 0:\n",
+ " scores[i] = dt.calc_signal_sholder_ratio(msgs[0], sampling_rate=8000000, debug=True, debug_path=\"/tmp/out\", suffix=suffix)\n",
+ " else:\n",
+ " scores[i] = dt.calc_signal_sholder_ratio(msgs[0], sampling_rate=8000000)\n",
+ " \n",
+ " score = np.mean(scores)\n",
+ " print(score, x2, x3, x4, x5, x6, x7, x8)\n",
+ " repeat = False\n",
+ " \n",
+ " return score"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def simple_opt(pars, i, d, func):\n",
+ " par = pars[i]\n",
+ " test_pars = []\n",
+ " for x in [-1, 0, 1]:\n",
+ " new_par = list(pars)\n",
+ " new_par[i] = par + x * d \n",
+ " test_pars.append(new_par)\n",
+ " res = [func(par_new) for par_new in test_pars]\n",
+ " sel = np.argmax(res)\n",
+ " best_par = test_pars[sel]\n",
+ " return best_par\n",
+ "\n",
+ "#pars = [1,1,1]\n",
+ "#i_rand = np.random.randint(0, len(pars))\n",
+ "#pars = simple_opt(pars, i_rand, 0.01, lambda x:np.sum(x))\n",
+ "#pars"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "top.set_txgain(86)\n",
+ "top.set_rxgain(5)\n",
+ "\n",
+ "pars = np.zeros(7)\n",
+ "\n",
+ "for i in range(10000):\n",
+ " i_rand = np.random.randint(0, len(pars))\n",
+ " pars = simple_opt(pars, i_rand, 0.005, measure)\n"
+ ]
+ },
+ {
+ "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": [
+ "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
+}