diff options
author | andreas128 <Andreas> | 2016-11-29 12:00:01 +0100 |
---|---|---|
committer | andreas128 <Andreas> | 2016-11-29 12:00:01 +0100 |
commit | d31ea5a51f0cc17b0556423896b7b0b7b3c2c217 (patch) | |
tree | e0104aaa0892055f8a65c24e9ab61eab17582016 | |
parent | a0da3afec9670c8aee3af1cbd8d2de75972d4698 (diff) | |
download | ODR-StaticPrecorrection-d31ea5a51f0cc17b0556423896b7b0b7b3c2c217.tar.gz ODR-StaticPrecorrection-d31ea5a51f0cc17b0556423896b7b0b7b3c2c217.tar.bz2 ODR-StaticPrecorrection-d31ea5a51f0cc17b0556423896b7b0b7b3c2c217.zip |
Add lut_generator.ipynb to generate a lut from a measurement
-rwxr-xr-x | amplitude_ramp.py | 28 | ||||
-rw-r--r-- | lut_generator.ipynb | 184 | ||||
-rw-r--r-- | tcp_async.py | 4 |
3 files changed, 214 insertions, 2 deletions
diff --git a/amplitude_ramp.py b/amplitude_ramp.py index 7672cda..7ef72e8 100755 --- a/amplitude_ramp.py +++ b/amplitude_ramp.py @@ -26,6 +26,7 @@ from gnuradio import gr from gnuradio import uhd from grc_gnuradio import blks2 as grc_blks2 import argparse +import pickle import time import socket import struct @@ -50,6 +51,7 @@ def xrange(start, stop, step): class RampGenerator(threading.Thread): tcpa = None + lut_dict = None def __init__(self, options, tcpa): threading.Thread.__init__(self) @@ -62,8 +64,19 @@ class RampGenerator(threading.Thread): self.ampl_step = float(options.ampl_step) self.ampl_stop = float(options.ampl_stop) + self.output_file = options.out + + if not options.lut is '': + self.lut_dict = pickle.load(open(options.lut, "rb")) + self.tcpa = tcpa + def lut(self, ampl): + if self.lut_dict is None: + return 1 + else: + return np.interp(ampl, self.lut_dict["ampl"], self.lut_dict["fac"]) + def set_source_ampl(self, ampl): self.event_queue_.put(ampl) self.in_queue_.get() @@ -95,12 +108,13 @@ class RampGenerator(threading.Thread): measurements = [] for ampl in amplitudes: + ampl_lut = self.lut(ampl) * ampl measurement_correct = False max_iter = 10 while measurement_correct == False and max_iter > 0: max_iter -= 1 - self.set_source_ampl(ampl) + self.set_source_ampl(ampl_lut) mag_gen_sum = 0 phase_diff_sum = 0 @@ -140,6 +154,8 @@ class RampGenerator(threading.Thread): print("Retry measurements") + name = self.output_file + pickle.dump(measurements, open(name, "wb")) self.tcpa.stop() self.event_queue_.put("done") self.event_queue_.put(measurements) @@ -182,6 +198,16 @@ parser.add_argument('--decim', help='Interval in samples between when to take the average of the measurements', required=False) +parser.add_argument('--lut', + default='', + help='Path to look up table file', + required=False) + +parser.add_argument('--out', + default='measurements.pkl', + help='Output file for measurements (.pkl)', + required=False) + cli_args = parser.parse_args() tcpa = tcp_async.UhdAsyncMsg() diff --git a/lut_generator.ipynb b/lut_generator.ipynb new file mode 100644 index 0000000..489203f --- /dev/null +++ b/lut_generator.ipynb @@ -0,0 +1,184 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np, pandas as pd\n", + "import pickle\n", + "import os\n", + "\n", + "if not os.path.isdir(\"./lut_plot\"):\n", + " os.mkdir(\"./lut_plot\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_meas(path):\n", + " measurements = pickle.load(open(path, \"rb\"))\n", + " df = pd.DataFrame(measurements, columns=[\"ampl\",\"mag_gen_sq\",\"mag_feedback_sq\",\"phase_diff\"])\n", + " df[\"mag_gen\"] = np.sqrt(df[\"mag_gen_sq\"])\n", + " df[\"mag_feedback\"] = np.sqrt(df[\"mag_feedback_sq\"])\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df = get_meas(\"./measurements.pkl\")\n", + "df_lut = get_meas(\"./measurements_lut.pkl\")\n", + "df_lut_sq = get_meas(\"./measurements_lut_sq.pkl\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_slope(ampl, mag_feedback):\n", + " slope, intersect = np.polyfit(x = ampl[0:20], y = mag_feedback[0:20], deg = 1)\n", + " return slope, intersect\n", + "\n", + "#get_slope(df[\"ampl\"], df[\"mag_feedback\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_fac(ampl, mag_feedback):\n", + " slope, intersect = get_slope(df[\"ampl\", df[\"mag_feedback\"]])\n", + " return[(x*slope + intersect) / y for (x,y) in zip(df[\"ampl\"], df[\"mag_feedback\"])]\n", + "def interp(x): return np.interp(x, df[\"ampl\"], fac)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = np.linspace(0.1, 0.5, num = 50)\n", + "\n", + "plt.plot(df[\"ampl\"], df[\"mag_feedback\"], label=\"measurement\")\n", + "plt.plot(x, x*slope + intersect, label = \"linear model\")\n", + "\n", + "plt.legend(loc=0)\n", + "plt.title(\"Original Measurement\")\n", + "\n", + "plt.savefig(\"./lut_plot/original_measurement.png\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "slope, intersect = get_slope(df[\"ampl\"], df[\"mag_feedback\"])\n", + "\n", + "plt.plot(df_lut[\"ampl\"], df_lut[\"mag_feedback\"], label=\"measurement\")\n", + "plt.plot(x, x*slope + intersect, label = \"linear model\")\n", + "\n", + "plt.legend(loc=0)\n", + "plt.title(\"Lut Measurement\")\n", + "\n", + "plt.savefig(\"./lut_plot/lut_measurement.png\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "slope, intersect = get_slope(df[\"ampl\"], df[\"mag_feedback\"])\n", + "\n", + "plt.plot(df_lut_sq[\"ampl\"], df_lut_sq[\"mag_feedback\"], label=\"measurement\")\n", + "plt.plot(x, x*slope + intersect, label = \"linear model\")\n", + "\n", + "plt.legend(loc=0)\n", + "plt.title(\"Lut Squared Measurement\")\n", + "\n", + "plt.savefig(\"./lut_plot/lut_sq_measurement.png\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "slope, intersect = get_slope(df[\"ampl\"], df[\"mag_feedback\"])\n", + "\n", + "plt.plot(df[\"ampl\"], df[\"mag_feedback\"], label=\"measurement\")\n", + "plt.plot(df_lut[\"ampl\"], df_lut[\"mag_feedback\"], label=\"measurement lut\")\n", + "plt.plot(df_lut_sq[\"ampl\"], df_lut_sq[\"mag_feedback\"], label=\"measurement lut sq\")\n", + "plt.plot(x, x*slope + intersect, label = \"linear model\")\n", + "\n", + "plt.legend(loc=0)\n", + "plt.title(\"All Measurements\")\n", + "\n", + "plt.savefig(\"./lut_plot/all_measurement.png\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pickle.dump({\"ampl\":df[\"ampl\"],\"fac\":[f for f in fac]}, open(\"lut.pkl\", \"wb\"))\n", + "pickle.dump({\"ampl\":df[\"ampl\"],\"fac\":[f**2 for f in fac]}, open(\"lut_sq.pkl\", \"wb\"))" + ] + }, + { + "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/tcp_async.py b/tcp_async.py index 4697437..d09cf70 100644 --- a/tcp_async.py +++ b/tcp_async.py @@ -22,7 +22,8 @@ class TcpAsyncClient(threading.Thread): #Establish connection - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock = None + print("Connecting to asynchronous uhd message tcp port " + str(self.port)) while 1: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -33,6 +34,7 @@ class TcpAsyncClient(threading.Thread): #traceback.print_exc() sock.close() time.sleep(0.5) + print("Connected to asynchronous uhd message tcp port " + str(self.port)) #Read messages sock.settimeout(1) |