aboutsummaryrefslogtreecommitdiffstats
path: root/sync-measurement.ipynb
diff options
context:
space:
mode:
authorandreas128 <Andreas>2017-01-29 15:18:40 +0000
committerandreas128 <Andreas>2017-01-29 15:18:40 +0000
commit6e0b2512e45b7a6ca03187814742cb0fe08964cb (patch)
tree763a30041c7bb539a45a3af6df17465ea6a13c7a /sync-measurement.ipynb
parenta88e67cb485d6b4b7bc21aa3c9dedbab37190cb9 (diff)
downloadODR-StaticPrecorrection-6e0b2512e45b7a6ca03187814742cb0fe08964cb.tar.gz
ODR-StaticPrecorrection-6e0b2512e45b7a6ca03187814742cb0fe08964cb.tar.bz2
ODR-StaticPrecorrection-6e0b2512e45b7a6ca03187814742cb0fe08964cb.zip
Add Amp characterization in sync-measurement.ipynb
Diffstat (limited to 'sync-measurement.ipynb')
-rw-r--r--sync-measurement.ipynb345
1 files changed, 173 insertions, 172 deletions
diff --git a/sync-measurement.ipynb b/sync-measurement.ipynb
index 5b713e7..0221c3c 100644
--- a/sync-measurement.ipynb
+++ b/sync-measurement.ipynb
@@ -8,8 +8,10 @@
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
+ "import time;\n",
"from scipy import signal\n",
"import matplotlib.pyplot as plt\n",
+ "import matplotlib.colors as mpcol\n",
"import src.dab_util as du"
]
},
@@ -30,7 +32,13 @@
"metadata": {},
"outputs": [],
"source": [
- "sg.gen_ramps(amplitudes=np.linspace(0.001, 0.95, num = 20))"
+ "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)"
]
},
{
@@ -39,8 +47,7 @@
"metadata": {},
"outputs": [],
"source": [
- "a_in = np.fromfile(\"./input.dat\", dtype=np.complex64)\n",
- "a_out = np.fromfile(\"./output.dat\", dtype=np.complex64)"
+ "from grc.amam_amap import amam_amap"
]
},
{
@@ -49,21 +56,7 @@
"metadata": {},
"outputs": [],
"source": [
- "def crop_signal(signal, n_window = 1000, n_zeros = 50000, debug = False):\n",
- " #signal = signal[-10:-1]\n",
- " mag = abs(signal)\n",
- " window = np.ones(n_window) / float(n_window)\n",
- " mag = scipy.signal.convolve(window, mag)\n",
- " mag = scipy.signal.convolve(window, mag)\n",
- " thr = 0.01 * np.max(mag)\n",
- " idx_start = np.argmax(mag > thr)\n",
- " idx_end = mag.shape[0] - np.argmax(np.flipud(mag > thr))\n",
- " if debug:\n",
- " plt.plot(mag < thr)\n",
- " plt.plot((idx_start,idx_start), (0,0.1), color='g', linewidth=2)\n",
- " plt.plot((idx_end,idx_end), (0,0.1), color='r', linewidth=2)\n",
- " signal = signal[idx_start - n_zeros: idx_end + n_zeros]\n",
- " return signal"
+ "top = amam_amap()\n"
]
},
{
@@ -72,7 +65,7 @@
"metadata": {},
"outputs": [],
"source": [
- "a_in = du.crop_signal(a_in)"
+ "sg.gen_ramps(amplitudes=amps)"
]
},
{
@@ -81,7 +74,31 @@
"metadata": {},
"outputs": [],
"source": [
- "a_out = du.crop_signal(a_out)"
+ "def fftlag(signal_original, signal_rec):\n",
+ " \"\"\"\n",
+ " Efficient way to find lag between two signals\n",
+ " Args:\n",
+ " signal_original: The signal that has been sent\n",
+ " signal_rec: The signal that has been recored\n",
+ " \"\"\"\n",
+ " c = np.flipud(signal.fftconvolve(signal_original,np.flipud(signal_rec)))\n",
+ " #plt.plot(c)\n",
+ " return np.argmax(c) - signal_original.shape[0] + 1\n",
+ " \n",
+ "#pattern = np.array([-2,2,-1,+3,-5,+7])\n",
+ "#delays = [0,1,2,3,4]\n",
+ "#padding = [0]\n",
+ "#padding_fil = [0]\n",
+ "#\n",
+ "#res = []\n",
+ "#for d in delays:\n",
+ "# for p in padding:\n",
+ "# for p2 in padding_fil:\n",
+ "# a = np.concatenate((pattern, np.zeros(p2)))\n",
+ "# b = np.concatenate((np.zeros(d), pattern, np.zeros(p)))\n",
+ "# res.append((d,conv(a,b)))\n",
+ "#res = np.array(res)\n",
+ "#plt.plot(zip(*res)[0], zip(*res)[1], 'p')"
]
},
{
@@ -96,18 +113,6 @@
"execution_count": null,
"metadata": {},
"outputs": [],
- "source": [
- "#plt.plot(a_in.real[780000 + 230000:2000000]+1, color='b');\n",
- "#plt.plot(a_out.real[780000:2000000], color='g');\n",
- "plt.plot(a_in.real+1, color='b');\n",
- "plt.plot(a_out.real, color='g');"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
"source": []
},
{
@@ -116,70 +121,15 @@
"metadata": {},
"outputs": [],
"source": [
- "def lagcorr(x,y,lag=None,verbose=True):\n",
- " '''Compute lead-lag correlations between 2 time series.\n",
- "\n",
- " <x>,<y>: 1-D time series.\n",
- " <lag>: lag option, could take different forms of <lag>:\n",
- " if 0 or None, compute ordinary correlation and p-value;\n",
- " if positive integer, compute lagged correlation with lag\n",
- " upto <lag>;\n",
- " if negative integer, compute lead correlation with lead\n",
- " upto <-lag>;\n",
- " if pass in an list or tuple or array of integers, compute \n",
- " lead/lag correlations at different leads/lags.\n",
- "\n",
- " Note: when talking about lead/lag, uses <y> as a reference.\n",
- " Therefore positive lag means <x> lags <y> by <lag>, computation is\n",
- " done by shifting <x> to the left hand side by <lag> with respect to\n",
- " <y>.\n",
- " Similarly negative lag means <x> leads <y> by <lag>, computation is\n",
- " done by shifting <x> to the right hand side by <lag> with respect to\n",
- " <y>.\n",
- "\n",
- " Return <result>: a (n*2) array, with 1st column the correlation \n",
- " coefficients, 2nd column correpsonding p values.\n",
- "\n",
- " Currently only works for 1-D arrays.\n",
- " '''\n",
- "\n",
- " import numpy\n",
- " from scipy.stats import pearsonr\n",
- "\n",
- " if len(x)!=len(y):\n",
- " raise Exception('Input variables of different lengths.')\n",
- "\n",
- " #--------Unify types of <lag>-------------\n",
- " if numpy.isscalar(lag):\n",
- " if abs(lag)>=len(x):\n",
- " raise Exception('Maximum lag equal or larger than array.')\n",
- " if lag<0:\n",
- " lag=-numpy.arange(abs(lag)+1)\n",
- " elif lag==0:\n",
- " lag=[0,]\n",
- " else:\n",
- " lag=numpy.arange(lag+1) \n",
- " elif lag is None:\n",
- " lag=[0,]\n",
- " else:\n",
- " lag=numpy.asarray(lag)\n",
- "\n",
- " #-------Loop over lags---------------------\n",
- " result=[]\n",
- " if verbose:\n",
- " print '\\n#<lagcorr>: Computing lagged-correlations at lags:',lag\n",
- "\n",
- " for ii in lag:\n",
- " if ii<0:\n",
- " result.append(pearsonr(x[:ii],y[-ii:]))\n",
- " elif ii==0:\n",
- " result.append(pearsonr(x,y))\n",
- " elif ii>0:\n",
- " result.append(pearsonr(x[ii:],y[:-ii]))\n",
- "\n",
- " result=numpy.asarray(result)\n",
+ "def get_amp_ratio(ampl_1, ampl_2, a_out_abs, a_in_abs):\n",
+ " idxs = (a_in_abs > ampl_1) & (a_in_abs < ampl_2)\n",
+ " ratio = a_out_abs[idxs] / a_in_abs[idxs]\n",
+ " return ratio.mean(), ratio.var()\n",
"\n",
- " return result"
+ "def get_phase(ampl_1, ampl_2, a_out, a_in):\n",
+ " idxs = (np.abs(a_in) > ampl_1) & (np.abs(a_in) < ampl_2)\n",
+ " ratio = np.angle(a_out[idxs], deg=True) - np.angle(a_in[idxs], deg=True)\n",
+ " return ratio.mean(), ratio.var()"
]
},
{
@@ -188,11 +138,52 @@
"metadata": {},
"outputs": [],
"source": [
- "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 = lagcorr(abs(a_out), abs(a_in), 1000)[:,0]"
+ "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 = fftlag(np.abs(a_in), np.abs(a_out))\n",
+ " \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",
+ " a_out_abs = np.abs(a_out)\n",
+ " a_in_abs = np.abs(a_in)\n",
+ " for ampl_1, ampl_2 in zip(bins, bins[1:]):\n",
+ " res.append(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(get_phase(ampl_1, ampl_2, a_out, a_in))\n",
+ " mean_phase, var_phase = zip(*res)\n",
+ " return mean_amp, var_amp, mean_phase, var_phase, db"
]
},
{
@@ -208,48 +199,40 @@
"metadata": {},
"outputs": [],
"source": [
- "#def corr(a_out, a_in):\n",
- "# import scipy\n",
- "# corr = np.correlate(\n",
- "# a_out,\n",
- "# a_in,\n",
- "# 'full')\n",
- "# delay = np.argmax(corr) - a_in.shape[0] + 1\n",
- "# #plt.plot(range(-corr.shape[0]/2, corr.shape[0]/2),corr)\n",
- "# return delay"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "#delay = corr(a_out, a_in)\n",
- "delay = np.argmax(c)\n",
- "a_out = a_out[delay - 1:]\n",
- "delay"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "l = min(a_out.shape[0], a_in.shape[0])\n",
- "a_out = a_out[0:l]\n",
- "a_in = a_in[0:l]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "plt.plot(a_in, color='g');\n",
- "plt.plot(a_out - 1, color='y');"
+ "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",
+ " if is_finite(res_tmp) and 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)"
]
},
{
@@ -257,12 +240,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
- "source": [
- "def get_amp_ratio(ampl_1, ampl_2):\n",
- " idxs = (a_in > ampl_1) & (a_in < ampl_2)\n",
- " ratio = (a_out[idxs] / a_in[idxs])\n",
- " return ratio.mean(), ratio.var()"
- ]
+ "source": []
},
{
"cell_type": "code",
@@ -270,10 +248,49 @@
"metadata": {},
"outputs": [],
"source": [
- "def get_phase(ampl_1, ampl_2):\n",
- " idxs = (a_in > ampl_1) & (a_in < ampl_2)\n",
- " ratio = np.angle(a_out[idxs]) - np.angle(a_in[idxs])\n",
- " return ratio.mean(), ratio.var()"
+ "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()"
]
},
{
@@ -281,44 +298,28 @@
"execution_count": null,
"metadata": {},
"outputs": [],
- "source": [
- "bins = np.linspace(0.1,1,num=10)\n",
- "res = []\n",
- "for ampl_1, ampl_2 in zip(bins, bins[1:]):\n",
- " res.append(get_amp_ratio(ampl_1, ampl_2))\n",
- "mean, var = zip(*res)"
- ]
+ "source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
- "source": [
- "plt.plot(mean)"
- ]
+ "source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
- "source": [
- "bins = np.linspace(0.1,1,num=10)\n",
- "res = []\n",
- "for ampl_1, ampl_2 in zip(bins, bins[1:]):\n",
- " res.append(get_phase(ampl_1, ampl_2))\n",
- "mean, var = zip(*res)"
- ]
+ "source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
- "source": [
- "plt.plot(mean)"
- ]
+ "source": []
},
{
"cell_type": "code",