{ "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", "from guppy import hpy \n", "import mydab\n", "\n", "import tcp_async\n", "import 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(mydab)\n", "except:\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sig = np.fromfile(\"/home/andreas/dab/dab_normalized_c64.dat\", dtype=np.complex64)\n", "\n", "#coefs = [0, -0.05, 0, 0, 0.05]\n", "coefs = [0, 0, 0, 0, 0]\n", "res = sig\n", "for idx, coef in enumerate(coefs):\n", " res += sig * np.abs(sig)**(idx+1) * coef #+1 because first correction term is squared\n", "\n", "res = res / np.max(res)\n", "\n", "sig.tofile(\"./input.dat\")\n", "del res\n", "del sig" ] }, { "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": [ "samp_rate = 4000000 #samples / second\n", "frequency_0 = samp_rate/10. #cycles / second\n", "frequency_1 = samp_rate/9. #cycles / second\n", "fft_size = 8192" ] }, { "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": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import peakutils\n", "def score(msg, frequency_0, frequency_1, samp_rate):\n", " frequency_0_idx = np.round(frequency_0 / samp_rate * fft_size).astype(int)\n", " frequency_1_idx = np.round(frequency_1 / samp_rate * fft_size).astype(int)\n", " frequency_idx_diff = frequency_1_idx - frequency_0_idx\n", " \n", " start = frequency_0_idx % frequency_idx_diff + frequency_idx_diff / 2.\n", " start = np.round(start).astype(int)\n", " end = msg.shape[0] - frequency_idx_diff - (msg.shape[0] - start) % frequency_idx_diff\n", " \n", " msg_cut = msg[start:end]\n", " \n", " msg_cut = msg_cut.reshape(-1, frequency_idx_diff)\n", " \n", " msg_peaks = np.max(msg_cut, axis=1)\n", " #plt.plot(msg_peaks, \"ro\")\n", " \n", " inter_mod = \\\n", " (0.5 * (msg_peaks[8]/msg_peaks[7] + msg_peaks[9]/msg_peaks[10]),\n", " 0.5 * (msg_peaks[8]/msg_peaks[6] + msg_peaks[9]/msg_peaks[11]),\n", " 0.5 * (msg_peaks[8]/msg_peaks[5] + msg_peaks[9]/msg_peaks[12]),\n", " 0.5 * (msg_peaks[8]/msg_peaks[4] + msg_peaks[9]/msg_peaks[13]),\n", " 0.5 * (msg_peaks[8]/msg_peaks[3] + msg_peaks[9]/msg_peaks[14]),\n", " 0.5 * (msg_peaks[8]/msg_peaks[2] + msg_peaks[9]/msg_peaks[15]),\n", " 0.5 * (msg_peaks[8]/msg_peaks[1] + msg_peaks[9]/msg_peaks[16]))\n", " \n", " return(inter_mod)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import itertools\n", "x = np.linspace(-0.1, 0.1, num = 5)\n", "l = list(itertools.product(x,x,x,x,x))\n", "mag = [sum(map(lambda x:x*x,el)) for el in l]\n", "idx = list(np.argsort(mag))\n", "l = list(np.array(l)[idx])\n", "len(l)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top.set_txgain(84)\n", "sync.has_msg()\n", "msgs = np.array(sync.get_msgs(2))\n", "fft = np.fft.fftshift(msgs[0])\n", "#plt.plot(fft)\n", "#plt.plot((4000 - f, 4000 -f),(-180,-40))\n", "\n", "mydab.calc_signal_sholder_ratio(fft, fft_size, samp_rate)" ] }, { "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": [ "def meas(i):\n", " import sys\n", " sys.stdout.write(str(i) + \" \")\n", " top.set_txgain(i)\n", " sync.has_msg()\n", " msgs = np.array(sync.get_msgs(10))\n", " \n", " res = []\n", " for msg in msgs:\n", " fft = np.fft.fftshift(msg)\n", " res.append(mydab.calc_signal_sholder_ratio(fft, fft_size, samp_rate))\n", " return np.mean(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top.set_txgain(85)\n", "mydab.calc_signal_sholder_ratio(fft, fft_size, samp_rate, plot=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "res = [meas(i) for i in range(89)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(res)\n", "plt.title(\"DAB sholders\")#, coefs = [0, -0.05, 0, 0, 0.05]\")\n", "plt.xlabel(\"tx gain [db]\")\n", "plt.ylabel(\"signal - sholder [db]\")" ] }, { "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": [] } ], "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 }