{ "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", "\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", "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": [ "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": [ "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": [ "top.set_txgain(85)\n", "for x2 in np.linspace(-0.15, 0.15, num = 15):\n", " t2 = time.time()\n", " for x3 in np.linspace(-0.15, 0.15, num = 14):\n", " for x4 in np.linspace(-0.15, 0.15, num = 7):\n", " path = gen_source.gen_file(frequency_0=frequency_0,\n", " frequency_1=frequency_1,\n", " samp_rate=samp_rate,\n", " x2 = x2,\n", " x3 = x3,\n", " x4 = x4,\n", " path='./np_twotone')\n", " #a_load = np.fromfile(path, dtype=np.complex64)\n", " #plt.plot(a_load.real[0:100])\n", " \n", " sync.has_msg()\n", " np.array(sync.get_msgs(2))\n", " msgs = np.array(sync.get_msgs(5))\n", " inter_mod_avg = (np.max(score(msgs[0], frequency_0, frequency_1, samp_rate)) \n", " + np.max(score(msgs[1], frequency_0, frequency_1, samp_rate)) \n", " + np.max(score(msgs[2], frequency_0, frequency_1, samp_rate)) \n", " + np.max(score(msgs[3], frequency_0, frequency_1, samp_rate)) \n", " + np.max(score(msgs[4], frequency_0, frequency_1, samp_rate)))/5\n", " msg = msgs[0]\n", " \n", " \n", " print(\"inter_mod %.3f, x_2 %.3f, x_3 %.3f\" % (inter_mod_avg, x2, x3))\n", " \n", " plt.plot(msg)\n", " plt.savefig(\"/tmp/out/inter_mod_%.3f_x_2_%.3f_x_3_%.3f_x_4_%.3f.png\" % (inter_mod_avg, x2, x3, x4))\n", " plt.clf()\n", " print(time.time() - t2)" ] }, { "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": [ "path = gen_source.gen_file(frequency_0=frequency_0,\n", " frequency_1=frequency_1,\n", " x1 = 0,\n", " x2 = 0,\n", " x3 = 0,\n", " x4 = 0,\n", " samp_rate=samp_rate,\n", " path='./np_twotone')\n", "a_load = np.fromfile(path, dtype=np.complex64)\n", "plt.plot(a_load.real[0:100])" ] }, { "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": [] }, { "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 }