{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import time\n", "import scipy\n", "from scipy import signal\n", "import matplotlib.pyplot as plt\n", "import matplotlib.colors as mpcol\n", "import src.dab_util as du" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "import src.signal_gen as sg\n", "reload(sg)\n", "reload(du)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "path_in = \"./input.dat\"\n", "path_out = \"./output.dat\"\n", "a_max = 0.95\n", "n_steps = 64\n", "amps = np.linspace(0.001, a_max, num = n_steps)\n", "txgains = (50, 55, 60, 65, 70, 75, 81, 82, 83, 84, 85, 86, 87, 88, 89)\n", "rxgains = (50, 40, 40, 25, 25, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20)\n", "txgains = (75, 81, 84, 85, 88, 89)\n", "rxgains = (20, 20, 20, 20, 20, 20)\n", "txgains = (83,)\n", "rxgains = (20,)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "from grc.amam_ampm import amam_ampm" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "sg.gen_ramps(path=\"./input.dat\", n_periods=64, pause=8, amplitudes=amps)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "top = amam_ampm()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "def extract_measurement(a_in, a_out, db, a_max, n_steps, debug = False):\n", " a_in = du.crop_signal(a_in)\n", " a_out = du.crop_signal(a_out)\n", " \n", " if debug:\n", " plt.plot(np.abs(a_in.real) + 1, color='b');\n", " plt.plot(np.abs(a_out.real), color='g');\n", " plt.show()\n", " \n", " #l = min(a_out.shape[0], a_in.shape[0])\n", " #a_out = a_out[0:l]\n", " #a_in = a_in[0:l]\n", " \n", " #c = du.lagcorr(np.abs(a_out), np.abs(a_in), 120000)[:,0]\n", " #c = signal.fftconvolve(a_in, a_out) - a_out.shape[0]\n", " #delay = du.fftlag(np.abs(a_in), np.abs(a_out))\n", " delay = du.fftlag(a_in, a_out) #TODO\n", " \n", " #delay = np.argmax(c)\n", " a_out = a_out[delay - 1:]\n", " \n", " l = min(a_out.shape[0], a_in.shape[0])\n", " a_out = a_out[0:l]\n", " a_in = a_in[0:l]\n", " \n", " if debug:\n", " print (\"delay = \" + str(delay))\n", " plt.plot(np.abs(a_in), color='g');\n", " plt.plot(np.abs(a_out) - 0.5, color='y');\n", " plt.show()\n", " \n", " bins = np.linspace(+0.5/n_steps,a_max + 0.5/n_steps,num=n_steps)\n", " res = []\n", " \n", " a_out_up = scipy.signal.resample(a_out, a_out.shape[0] * 8)\n", " a_in_up = scipy.signal.resample(a_in, a_in.shape[0] * 8)\n", " \n", " a_out_abs = np.abs(a_out_up)\n", " a_in_abs = np.abs(a_in_up)\n", " for ampl_1, ampl_2 in zip(bins, bins[1:]):\n", " res.append(du.get_amp_ratio(ampl_1, ampl_2, a_out_abs, a_in_abs))\n", " del a_out_abs\n", " del a_in_abs\n", " mean_amp, var_amp = zip(*res)\n", " \n", " res = []\n", " for ampl_1, ampl_2 in zip(bins, bins[1:]):\n", " res.append(du.get_phase(ampl_1, ampl_2, a_out_up, a_in_up))\n", " mean_phase, var_phase = zip(*res)\n", " return mean_amp, var_amp, mean_phase, var_phase, db" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "reload(du)\n", "res = []\n", "\n", "for txgain, rxgain in zip(txgains, rxgains):\n", " print (txgain, rxgain)\n", " res_tmp = None\n", " for i in range(10):\n", " top.uhd_usrp_sink_0_0.set_gain(txgain)\n", " top.uhd_usrp_source_0.set_gain(rxgain)\n", " \n", " top.file_sink_out.close()\n", " top.blocks_file_source_0.close()\n", " \n", " top.file_sink_out.open(path_out)\n", " top.blocks_file_source_0.open(path_in, False)\n", " top.start()\n", " \n", " time.sleep(1)\n", " \n", " top.stop()\n", " top.wait()\n", " \n", " a_in = np.fromfile(path_in, dtype=np.complex64)\n", " a_out = np.fromfile(path_out, dtype=np.complex64)\n", " res_tmp = extract_measurement(a_in, a_out, txgain, a_max, n_steps, debug=True)\n", " \n", " def is_finite(r): return np.all([np.all(np.isfinite(c)) for c in r])\n", " #def has_small_jumps(mean_amp): return np.max(np.abs(np.diff(mean_amp))) / np.median(np.abs(np.diff(mean_amp))) < 100\n", " \n", " #TODO\n", " if is_finite(res_tmp): # and 1 + has_small_jumps(res_tmp[0]):\n", " break\n", " else:\n", " print (is_finite(res_tmp))#, has_small_jumps(res_tmp[0]))\n", " \n", " res.append(res_tmp)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [ "fig = plt.figure(figsize=(10,10))\n", "ax1 = plt.subplot(211)\n", "\n", "def plot_with_label(x, y, color, label):\n", " ax1.plot(x, y, color=color, label=txgain)\n", " \n", "for idx, (txgain, rxgain) in enumerate(zip(*(txgains, rxgains))):\n", " plot_with_label(\n", " x = amps[1:], \n", " y = 10*np.log(res[idx][0])/np.log(10) - rxgain + 102,\n", " color = mpcol.hsv_to_rgb((idx * 0.75 / len(txgains), 0.6, 1)),\n", " label = txgain\n", " )\n", "ax1.set_ylabel(\"Gain [dB]\")\n", "\n", "ax2 = plt.subplot(212)\n", "\n", "def plot_with_label(x, y, color, label):\n", " ax2.plot(x, y, color=color, label=txgain)\n", " \n", "for idx, (txgain, rxgain) in enumerate(zip(*(txgains, rxgains))):\n", " plot_with_label(\n", " x = amps[1:],\n", " y = res[idx][2],\n", " color = mpcol.hsv_to_rgb((idx * 0.75 / len(txgains), 0.6, 1)),\n", " label = txgain\n", " )\n", "\n", "ax2.set_ylabel(\"Pase [degree]\")\n", "ax2.set_xlabel(\"Amplitude\")\n", "\n", "#legend\n", "# Shrink current axis by 20%\n", "box = ax1.get_position()\n", "ax1.set_position([box.x0, box.y0, box.width * 0.8, box.height])\n", "box = ax2.get_position()\n", "ax2.set_position([box.x0, box.y0, box.width * 0.8, box.height])\n", "\n", "# Put a legend to the right of the current axis\n", "ax1.legend(loc='center left', bbox_to_anchor=(1.05, -0.3))\n", "\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": true, "editable": true }, "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 }