From bf1dcd15c041b73aeca73cd1aebe894d3128b4f2 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Fri, 9 Dec 2016 09:38:33 +0100 Subject: Add grid search in run.ipynb --- .gitignore | 13 + generate_compensated_2_tone_file.ipynb | 291 --------- live_analyse.grc | 1108 +++++++------------------------- run.ipynb | 254 ++++++++ src/__init__.py | 0 src/gen_source.py | 32 + tcp_async.py | 2 +- tcp_sync.py | 34 +- 8 files changed, 560 insertions(+), 1174 deletions(-) delete mode 100644 generate_compensated_2_tone_file.ipynb create mode 100644 run.ipynb create mode 100644 src/__init__.py create mode 100644 src/gen_source.py diff --git a/.gitignore b/.gitignore index 54b9753..9c37b59 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,19 @@ tcp_async.pyc usrp_rx usrp_tx lut_generator.py +generate_compensated_2_tone_file.py +np_twotone +np_twotone_dpd +out_cos +out_sin +tcp_sync.pyc +tmp.grc +top_block.py +Untitled.ipynb +Untitled.py +Untitled.txt + + diff --git a/generate_compensated_2_tone_file.ipynb b/generate_compensated_2_tone_file.ipynb deleted file mode 100644 index 7037d3b..0000000 --- a/generate_compensated_2_tone_file.ipynb +++ /dev/null @@ -1,291 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%matplotlib inline\n", - "import matplotlib.pyplot as plt\n", - "import numpy as np" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "a = np.fromfile(\"./out_sin\", np.complex64)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "a.shape" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "a = a[0:100]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "mag = np.abs(a)\n", - "plt.plot(mag)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.plot(a.imag[0:100])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.plot(a.real[0:100])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def complex_exp(freq, samp_rate, periods, phase_deg=0):\n", - " t_max = 1.0 * samp_rate / freq * periods\n", - " t = np.arange(t_max)\n", - " fac = t / samp_rate * freq\n", - " phase = 1j * phase_deg/360*2*np.pi\n", - " ret = np.exp(phase + 1j * 2 * np.pi * fac - 1j * np.pi / 2, dtype=np.complex64)\n", - " return ret\n", - "ret = complex_exp(10,40,2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "samp_rate = 4000000 #samples / second\n", - "frequency_0 = samp_rate/9. #cycles / second\n", - "frequency_1 = samp_rate/10. #cycles / second\n", - "tone_0 = complex_exp(frequency_0, samp_rate, 10, 180)\n", - "tone_1 = complex_exp(frequency_1, samp_rate, 9)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def complex_exp(freq, samp_rate, periods):\n", - " t_max = 1.0 * samp_rate / freq * periods\n", - " t = np.arange(t_max)\n", - " fac = t / samp_rate * freq\n", - " ret = np.exp(1j * 2 * np.pi * fac - 1j * np.pi / 2)\n", - " return ret\n", - "ret = complex_exp(10,40,2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "two_tone = 1/2.0 * (tone_0 + tone_1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "two_tone[-1]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "two_tone.tofile(\"./np_twotone\")\n", - "two_tone.dtype" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "a_load = np.fromfile(\"./np_twotone\", dtype=np.complex64)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "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": [ - "import pickle" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "lut_dict = pickle.load(open(\"./lut_tab.pkl\", \"rb\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def dpd(x):\n", - " mag = np.abs(x)\n", - " fac = np.interp(mag/2.0, lut_dict[\"ampl\"], lut_dict[\"fac\"])\n", - " ret = x*fac\n", - " return ret.astype(np.complex64)\n", - "\n", - "#def dpd(x):\n", - "# return x*0.5" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "tone_0_dpd = np.apply_along_axis(dpd, 0, tone_0)\n", - "tone_1_dpd = np.apply_along_axis(dpd, 0, tone_1)\n", - "two_tone_dpd = 1/2. * (tone_0_dpd + tone_1_dpd)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.plot(two_tone_dpd.real[0:200])\n", - "plt.plot(two_tone.real[0:200])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "two_tone_dpd.tofile(\"./np_twotone\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "two_tone.tofile(\"./np_twotone\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "a_load = np.fromfile(\"./np_twotone_dpd\", dtype=np.complex64)\n", - "plt.plot(a_load.real[0:200])" - ] - }, - { - "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 -} diff --git a/live_analyse.grc b/live_analyse.grc index 550df5c..5b3db4b 100644 --- a/live_analyse.grc +++ b/live_analyse.grc @@ -170,7 +170,7 @@ value - 0 + 2000000 _enabled @@ -178,7 +178,7 @@ _coordinate - (408, 545) + (336, 568) gui_hint @@ -190,75 +190,12 @@ id - pow1 + high label - pow1 - - - min_len - 200 - - - orient - Qt.Horizontal - - - start - 0 - - - step - 0.05 - - - stop - 1 - - - rangeType - float - - - widget - counter_slider - - - - variable_qtgui_range - - comment - - - - value - 0 - - - _enabled - True - - - _coordinate - (288, 545) - - - gui_hint - - _rotation - 0 - - - id - pow2 - - - label - pow2 - min_len 200 @@ -269,15 +206,15 @@ start - -1 + 50000 step - 0.05 + 1 stop - 1 + 2000000 rangeType @@ -296,7 +233,7 @@ value - 0 + 50000 _enabled @@ -304,7 +241,7 @@ _coordinate - (168, 545) + (168, 568) gui_hint @@ -316,11 +253,11 @@ id - pow3 + low label - pow3 + min_len @@ -332,15 +269,15 @@ start - -1 + 50000 step - 0.05 + 1 stop - 1 + 2000000 rangeType @@ -649,22 +586,14 @@ - analog_const_source_x - - alias - - + variable_qtgui_range comment - const - 0 - - - affinity - + value + 500 _enabled @@ -672,7 +601,11 @@ _coordinate - (488, 108) + (168, 688) + + + gui_hint + _rotation @@ -680,23 +613,43 @@ id - analog_const_source_x_0 + width - maxoutbuf - 0 + label + - minoutbuf - 0 + min_len + 200 - type + orient + Qt.Horizontal + + + start + 1 + + + step + 1 + + + stop + 5000000 + + + rangeType float + + widget + counter_slider + - blocks_add_xx + analog_const_source_x alias @@ -705,6 +658,10 @@ comment + + const + 0 + affinity @@ -715,7 +672,7 @@ _coordinate - (680, 249) + (512, 92) _rotation @@ -723,11 +680,7 @@ id - blocks_add_xx_0 - - - type - complex + analog_const_source_x_0 maxoutbuf @@ -738,12 +691,8 @@ 0 - num_inputs - 2 - - - vlen - 1 + type + complex @@ -766,7 +715,7 @@ _coordinate - (680, 329) + (696, 121) _rotation @@ -817,7 +766,7 @@ _coordinate - (960, 553) + (968, 321) _rotation @@ -841,7 +790,7 @@ - blocks_complex_to_mag + blocks_complex_to_mag_squared alias @@ -860,7 +809,7 @@ _coordinate - (488, 65) + (800, 225) _rotation @@ -868,7 +817,7 @@ id - blocks_complex_to_mag_0 + blocks_complex_to_mag_squared_0 maxoutbuf @@ -899,11 +848,11 @@ _enabled - True + 1 _coordinate - (784, 433) + (792, 401) _rotation @@ -911,7 +860,7 @@ id - blocks_complex_to_mag_squared_0 + blocks_complex_to_mag_squared_0_0 maxoutbuf @@ -927,7 +876,11 @@ - blocks_complex_to_mag_squared + blocks_file_sink + + append + False + alias @@ -942,11 +895,15 @@ _enabled - 1 + True + + + file + fft_sink _coordinate - (784, 633) + (1312, 599) _rotation @@ -954,19 +911,19 @@ id - blocks_complex_to_mag_squared_0_0 + blocks_file_sink_0 - maxoutbuf - 0 + type + float - minoutbuf - 0 + unbuffered + False vlen - 1 + 8192 @@ -993,7 +950,7 @@ _coordinate - (208, 53) + (344, 141) _rotation @@ -1024,49 +981,6 @@ 1 - - blocks_float_to_complex - - alias - - - - comment - - - - affinity - - - - _enabled - True - - - _coordinate - (784, 73) - - - _rotation - 0 - - - id - blocks_float_to_complex_0 - - - maxoutbuf - 0 - - - minoutbuf - 0 - - - vlen - 1 - - blocks_message_burst_source @@ -1087,7 +1001,7 @@ _coordinate - (784, 897) + (896, 785) _rotation @@ -1134,7 +1048,7 @@ _coordinate - (1160, 534) + (1168, 302) _rotation @@ -1189,7 +1103,7 @@ _coordinate - (960, 414) + (976, 206) _rotation @@ -1244,7 +1158,7 @@ _coordinate - (1160, 614) + (1168, 382) _rotation @@ -1299,7 +1213,7 @@ _coordinate - (784, 537) + (792, 305) _rotation @@ -1334,7 +1248,7 @@ const - pow3 + 1 affinity @@ -1346,7 +1260,7 @@ _coordinate - (512, 252) + (536, 148) _rotation @@ -1354,7 +1268,7 @@ id - blocks_multiply_const_vxx_0 + blocks_multiply_const_vxx_1 type @@ -1374,18 +1288,18 @@ - blocks_multiply_const_vxx + blocks_null_sink alias - comment - + bus_conns + [[0,],] - const - pow1 + comment + affinity @@ -1397,7 +1311,7 @@ _coordinate - (528, 356) + (1384, 225) _rotation @@ -1405,19 +1319,15 @@ id - blocks_multiply_const_vxx_1 + blocks_null_sink_0 type - complex - - - maxoutbuf - 0 + float - minoutbuf - 0 + num_inputs + 1 vlen @@ -1425,18 +1335,18 @@ - blocks_multiply_const_vxx + blocks_null_sink alias - comment - + bus_conns + [[0,],] - const - pow2 + comment + affinity @@ -1448,7 +1358,7 @@ _coordinate - (528, 308) + (1552, 321) _rotation @@ -1456,19 +1366,15 @@ id - blocks_multiply_const_vxx_2 + blocks_null_sink_0_0 type - complex - - - maxoutbuf - 0 + float - minoutbuf - 0 + num_inputs + 1 vlen @@ -1476,11 +1382,15 @@ - blocks_multiply_xx + blocks_null_sink alias + + bus_conns + [[0,],] + comment @@ -1495,7 +1405,7 @@ _coordinate - (1008, 81) + (1552, 401) _rotation @@ -1503,215 +1413,15 @@ id - blocks_multiply_xx_0 + blocks_null_sink_0_1 type - complex + float - maxoutbuf - 0 - - - minoutbuf - 0 - - - num_inputs - 2 - - - vlen - 1 - - - - blocks_multiply_xx - - alias - - - - comment - - - - affinity - - - - _enabled - True - - - _coordinate - (1160, 73) - - - _rotation - 0 - - - id - blocks_multiply_xx_0_0 - - - type - complex - - - maxoutbuf - 0 - - - minoutbuf - 0 - - - num_inputs - 2 - - - vlen - 1 - - - - blocks_null_sink - - alias - - - - bus_conns - [[0,],] - - - comment - - - - affinity - - - - _enabled - True - - - _coordinate - (1368, 433) - - - _rotation - 0 - - - id - blocks_null_sink_0 - - - type - float - - - num_inputs - 1 - - - vlen - 1 - - - - blocks_null_sink - - alias - - - - bus_conns - [[0,],] - - - comment - - - - affinity - - - - _enabled - True - - - _coordinate - (1544, 553) - - - _rotation - 0 - - - id - blocks_null_sink_0_0 - - - type - float - - - num_inputs - 1 - - - vlen - 1 - - - - blocks_null_sink - - alias - - - - bus_conns - [[0,],] - - - comment - - - - affinity - - - - _enabled - True - - - _coordinate - (1544, 633) - - - _rotation - 0 - - - id - blocks_null_sink_0_1 - - - type - float - - - num_inputs - 1 + num_inputs + 1 vlen @@ -1742,7 +1452,7 @@ _coordinate - (1208, 897) + (1320, 785) _rotation @@ -1789,7 +1499,7 @@ _coordinate - (1320, 541) + (1328, 309) _rotation @@ -1844,7 +1554,7 @@ _coordinate - (1120, 421) + (1136, 213) _rotation @@ -1872,361 +1582,153 @@ type - fff - - - - fir_filter_xxx - - alias - - - - comment - - - - affinity - - - - decim - decim - - - _enabled - True - - - _coordinate - (1320, 621) - - - _rotation - 0 - - - id - fir_filter_xxx_0_1 - - - maxoutbuf - 0 - - - minoutbuf - 0 - - - samp_delay - 0 - - - taps - [1] - - - type - fff - - - - qtgui_freq_sink_x - - autoscale - False - - - average - 1.0 - - - bw - samp_rate/decimate - - - alias - - - - fc - 0 - - - comment - - - - ctrlpanel - False - - - affinity - - - - _enabled - True - - - fftsize - 16383 - - - _coordinate - (976, 774) - - - gui_hint - - - - _rotation - 0 - - - grid - False - - - id - qtgui_freq_sink_x_0_0 - - - legend - True - - - alpha1 - 1.0 - - - color1 - "blue" - - - label1 - - - - width1 - 1 - - - alpha10 - 1.0 - - - color10 - "dark blue" - - - label10 - - - - width10 - 1 - - - alpha2 - 1.0 - - - color2 - "red" - - - label2 - - - - width2 - 1 - - - alpha3 - 1.0 - - - color3 - "green" - - - label3 - - - - width3 - 1 - - - alpha4 - 1.0 - - - color4 - "black" - - - label4 - - - - width4 - 1 - - - alpha5 - 1.0 - - - color5 - "cyan" - - - label5 - - - - width5 - 1 - - - alpha6 - 1.0 - - - color6 - "magenta" + fff + + + fir_filter_xxx - label6 + alias - width6 - 1 + comment + - alpha7 - 1.0 + affinity + - color7 - "yellow" + decim + decim - label7 - + _enabled + True - width7 - 1 + _coordinate + (1328, 389) - alpha8 - 1.0 + _rotation + 0 - color8 - "dark red" + id + fir_filter_xxx_0_1 - label8 - + maxoutbuf + 0 - width8 - 1 + minoutbuf + 0 - alpha9 - 1.0 + samp_delay + 0 - color9 - "dark green" + taps + [1] - label9 - + type + fff + + + logpwrfft_x - width9 - 1 + avg_alpha + 1.0 - maxoutbuf - 0 + average + False - minoutbuf - 0 + alias + - name - "" + comment + - nconnections - 1 + affinity + - showports + _enabled True - freqhalf - True + fft_size + 8192 - tr_chan - 0 + frame_rate + 30 - tr_level - 0.0 + _coordinate + (1048, 585) - tr_mode - qtgui.TRIG_MODE_FREE + _rotation + 0 - tr_tag - "" + id + logpwrfft_x_0 type complex - update_time - 0.10 + maxoutbuf + 0 - wintype - firdes.WIN_BLACKMAN_hARRIS + minoutbuf + 0 - ymax - 10 + ref_scale + 2 - ymin - -140 + sample_rate + samp_rate - qtgui_time_sink_x + qtgui_freq_sink_x autoscale False + + average + 1.0 + + + bw + samp_rate/decimate + alias + + fc + 0 + comment @@ -2240,16 +1742,16 @@ - entags + _enabled True - _enabled - True + fftsize + 8192 _coordinate - (1072, 310) + (1008, 478) gui_hint @@ -2265,7 +1767,7 @@ id - qtgui_time_sink_x_0 + qtgui_freq_sink_x_0_0 legend @@ -2283,14 +1785,6 @@ label1 - - marker1 - -1 - - - style1 - 1 - width1 1 @@ -2301,20 +1795,12 @@ color10 - "blue" + "dark blue" label10 - - marker10 - -1 - - - style10 - 1 - width10 1 @@ -2331,14 +1817,6 @@ label2 - - marker2 - -1 - - - style2 - 1 - width2 1 @@ -2355,14 +1833,6 @@ label3 - - marker3 - -1 - - - style3 - 1 - width3 1 @@ -2379,14 +1849,6 @@ label4 - - marker4 - -1 - - - style4 - 1 - width4 1 @@ -2403,14 +1865,6 @@ label5 - - marker5 - -1 - - - style5 - 1 - width5 1 @@ -2427,14 +1881,6 @@ label6 - - marker6 - -1 - - - style6 - 1 - width6 1 @@ -2451,14 +1897,6 @@ label7 - - marker7 - -1 - - - style7 - 1 - width7 1 @@ -2475,14 +1913,6 @@ label8 - - marker8 - -1 - - - style8 - 1 - width8 1 @@ -2500,16 +1930,16 @@ - marker9 - -1 + width9 + 1 - style9 - 1 + maxoutbuf + 0 - width9 - 1 + minoutbuf + 0 name @@ -2520,21 +1950,17 @@ 1 - size - 1024 + showports + True - srate - samp_rate + freqhalf + True tr_chan 0 - - tr_delay - 0 - tr_level 0.0 @@ -2543,10 +1969,6 @@ tr_mode qtgui.TRIG_MODE_FREE - - tr_slope - qtgui.TRIG_SLOPE_POS - tr_tag "" @@ -2560,20 +1982,16 @@ 0.10 - ylabel - Amplitude - - - yunit - "" + wintype + firdes.WIN_BLACKMAN_hARRIS ymax - 1 + 10 ymin - -1 + -140 @@ -2604,7 +2022,7 @@ _coordinate - (752, 767) + (776, 471) _rotation @@ -2659,7 +2077,7 @@ _coordinate - (392, 897) + (504, 785) _rotation @@ -3350,7 +2768,7 @@ _coordinate - (792, 319) + (808, 111) _rotation @@ -4425,7 +3843,7 @@ _coordinate - (504, 694) + (512, 462) _rotation @@ -4578,12 +3996,6 @@ analog_const_source_x_0 - blocks_float_to_complex_0 - 0 - 1 - - - blocks_add_xx_0 blocks_add_xx_1 0 0 @@ -4600,12 +4012,6 @@ 0 0 - - blocks_add_xx_1 - qtgui_time_sink_x_0 - 0 - 0 - blocks_add_xx_1 uhd_usrp_sink_0 @@ -4618,12 +4024,6 @@ 0 0 - - blocks_complex_to_mag_0 - blocks_float_to_complex_0 - 0 - 0 - blocks_complex_to_mag_squared_0 blocks_moving_average_xx_0_0 @@ -4636,36 +4036,12 @@ 0 0 - - blocks_file_source_0 - blocks_complex_to_mag_0 - 0 - 0 - blocks_file_source_0 blocks_multiply_const_vxx_1 0 0 - - blocks_file_source_0 - blocks_multiply_xx_0 - 0 - 0 - - - blocks_float_to_complex_0 - blocks_multiply_xx_0 - 0 - 1 - - - blocks_float_to_complex_0 - blocks_multiply_xx_0_0 - 0 - 0 - blocks_message_burst_source_0 blocks_null_sink_0_2 @@ -4696,42 +4072,12 @@ 0 0 - - blocks_multiply_const_vxx_0 - blocks_add_xx_0 - 0 - 0 - blocks_multiply_const_vxx_1 blocks_add_xx_1 0 1 - - blocks_multiply_const_vxx_2 - blocks_add_xx_0 - 0 - 1 - - - blocks_multiply_xx_0 - blocks_multiply_const_vxx_2 - 0 - 0 - - - blocks_multiply_xx_0 - blocks_multiply_xx_0_0 - 0 - 1 - - - blocks_multiply_xx_0_0 - blocks_multiply_const_vxx_0 - 0 - 0 - fir_filter_xxx_0 blocks_null_sink_0_0 @@ -4750,6 +4096,12 @@ 0 0 + + logpwrfft_x_0 + blocks_file_sink_0 + 0 + 0 + rational_resampler_xxx_0_0 qtgui_freq_sink_x_0_0 @@ -4774,6 +4126,12 @@ 0 1 + + uhd_usrp_source_0 + logpwrfft_x_0 + 0 + 0 + uhd_usrp_source_0 rational_resampler_xxx_0_0 diff --git a/run.ipynb b/run.ipynb new file mode 100644 index 0000000..5c61960 --- /dev/null +++ b/run.ipynb @@ -0,0 +1,254 @@ +{ + "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 +} diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/gen_source.py b/src/gen_source.py new file mode 100644 index 0000000..a8c2ef2 --- /dev/null +++ b/src/gen_source.py @@ -0,0 +1,32 @@ +import numpy as np + + +def complex_exp(freq, samp_rate, periods, phase_deg=0): + t_max = 1.0 * samp_rate / freq * periods + t = np.arange(t_max) + fac = t / samp_rate * freq + phase = 1j * phase_deg/360*2*np.pi + ret = np.exp(phase + 1j * 2 * np.pi * fac - 1j * np.pi / 2, dtype=np.complex64) + return ret +ret = complex_exp(10,40,2) + + +def gen_file(frequency_0, frequency_1, x1 = 0, x2 = 0, x3 = 0, x4 = 0, samp_rate = 4000000, path = "./np_twotone"): + tone_0 = complex_exp(frequency_0, samp_rate, samp_rate/frequency_1) + tone_1 = complex_exp(frequency_1, samp_rate, samp_rate/frequency_0) + + two_tone = (tone_0 + tone_1) + two_tone = two_tone \ + + np.abs(two_tone)**1 * x1 \ + + np.abs(two_tone)**2 * x2 \ + + np.abs(two_tone)**3 * x3 \ + + np.abs(two_tone)**4 * x4 + + two_tone = two_tone / np.max(two_tone) * 0.9 + + two_tone.tofile(path) + + a_load = np.fromfile(path, dtype=np.complex64) + assert(np.isclose(a_load, two_tone).all()), "Inconsistent stored file" + + return path diff --git a/tcp_async.py b/tcp_async.py index 5cd4a20..05cadd7 100644 --- a/tcp_async.py +++ b/tcp_async.py @@ -28,7 +28,7 @@ class _TcpAsyncClient(threading.Thread): #Establish connection sock = None print("Connecting to asynchronous uhd message tcp port " + str(self.port)) - while 1: + while self.q_quit.empty(): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.ip_address, self.port)) diff --git a/tcp_sync.py b/tcp_sync.py index faffe51..6a2e619 100644 --- a/tcp_sync.py +++ b/tcp_sync.py @@ -14,10 +14,12 @@ class _TcpSyncClient(threading.Thread): ip_address = None port = None - def __init__(self, ip_address, port): + def __init__(self, ip_address, port, packet_size, packet_type): super(_TcpSyncClient, self).__init__() self.ip_address = ip_address self.port = port + self.packet_size = packet_size + self.packet_type = packet_type def __exit__(self): self.stop() @@ -28,7 +30,7 @@ class _TcpSyncClient(threading.Thread): #Establish connection sock = None print("Connecting to synchronous uhd message tcp port " + str(self.port)) - while 1: + while self.q_quit.empty(): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.ip_address, self.port)) @@ -44,13 +46,24 @@ class _TcpSyncClient(threading.Thread): sock.settimeout(None) while self.q_quit.empty(): try: - s = sock.recv(12) + s = "" + + #concatenate to one package + while self.q_quit.empty(): + s += sock.recv(self.packet_size) + if (len(s)) == self.packet_size: + break + if (len(s)) > self.packet_size: + print("received wrong size of length " + str(len(s))) + time.sleep(0.01) + return -1 + res_tuple = struct.unpack( - "fff", + self.packet_type, s) - assert(type(res_tuple) is tuple), (type(res_list), res_tuple) self.queue.put(res_tuple) except socket.timeout: + self.stop() traceback.print_exc() pass @@ -65,8 +78,8 @@ class _TcpSyncClient(threading.Thread): class UhdSyncMsg(object): """Creates a thread to connect to the synchronous uhd messages tcp port""" - def __init__(self, ip_address = "127.0.0.1", port = 47009): - self.tcpa = _TcpSyncClient(ip_address, port) + def __init__(self, ip_address = "127.0.0.1", port = 47009, packet_size = 3, packet_type = "fff"): + self.tcpa = _TcpSyncClient(ip_address, port, packet_size, packet_type) self.tcpa.start() def __exit__(self): @@ -76,6 +89,13 @@ class UhdSyncMsg(object): """stop tcp thread""" self.tcpa.stop() + def get_msgs(self, num): + """get received messages as string of integer""" + out = [] + while len(out) < num: + out.append(self.tcpa.queue.get()) + return out + def get_res(self): """get received messages as string of integer""" out = [] -- cgit v1.2.3