diff options
Diffstat (limited to 'sync-measurement.ipynb')
-rw-r--r-- | sync-measurement.ipynb | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/sync-measurement.ipynb b/sync-measurement.ipynb new file mode 100644 index 0000000..5b713e7 --- /dev/null +++ b/sync-measurement.ipynb @@ -0,0 +1,358 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "import numpy as np\n", + "from scipy import signal\n", + "import matplotlib.pyplot as plt\n", + "import src.dab_util as du" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import src.signal_gen as sg\n", + "reload(sg)\n", + "reload(du)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sg.gen_ramps(amplitudes=np.linspace(0.001, 0.95, num = 20))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a_in = np.fromfile(\"./input.dat\", dtype=np.complex64)\n", + "a_out = np.fromfile(\"./output.dat\", dtype=np.complex64)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a_in = du.crop_signal(a_in)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a_out = du.crop_signal(a_out)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "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": [] + }, + { + "cell_type": "code", + "execution_count": null, + "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", + "\n", + " return result" + ] + }, + { + "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]\n", + "\n", + "c = lagcorr(abs(a_out), abs(a_in), 1000)[:,0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "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');" + ] + }, + { + "cell_type": "code", + "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()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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()" + ] + }, + { + "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_amp_ratio(ampl_1, ampl_2))\n", + "mean, var = zip(*res)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(mean)" + ] + }, + { + "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)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(mean)" + ] + }, + { + "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 +} |