m-thesis-introduction/02_discrete_signal_translation.ipynb

411 lines
201 KiB
Text
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Translation of a discrete 1D signal\n",
"\n",
"\n",
"#### wave equation\n",
"\n",
"$$\n",
"\\frac{\\partial^2 u}{\\partial t^2}\n",
" = \n",
" v^2 \\frac{\\partial^2 u}{\\partial x^2}\n",
"$$\n",
"\n",
"\n",
"##### simple sine solution\n",
"\n",
"$$\n",
"u(x, t) = \\sin\\left( \\frac{2 \\pi x}{\\lambda} -2\\pi f t\\right)\n",
"= \\sin\\left( 2 \\pi f \\, ( \\frac{x}{v(f)} -t) \\right)\n",
"$$\n",
"\n",
"$$\\lambda = \\dfrac{v}{f}$$\n",
"##### plane wave per mode\n",
"\n",
"\n",
"\n",
"$$ \n",
"u_f(x,t) \n",
"= \n",
" A \\exp(- 2\\pi i f \\left[ \\frac{x}{v(f)} - t \\right] )\n",
" +\n",
" B \\exp(- 2\\pi i f \\left[ \\frac{x}{v(f)} + t \\right] )\n",
"$$\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"import scipy.fft as ft\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.gridspec as gridspec\n",
"import matplotlib.ticker as tck\n",
"import scipy.interpolate as interp\n",
"\n",
"rng = np.random.default_rng()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class TravelSignal:\n",
" \"\"\"\n",
" Model an arbitrary signal that can be translated to another position and time.\n",
" \"\"\"\n",
"\n",
" def __init__(self, signal, sample_rate, t_0 = 0, x_0 = 0, periodic=True):\n",
" assert periodic, \"Non-periodic signal is not yet supported\"\n",
"\n",
" self.raw = signal\n",
"\n",
" self.sample_rate = sample_rate # Hz\n",
" self.sample_length = len(self.raw)\n",
" self.time_length = self.sample_length*sample_rate # s\n",
" \n",
" self.x_0 = x_0\n",
" self.t_0 = t_0\n",
"\n",
" # choose interpolation method\n",
" if not interp1d_kw:\n",
" self.interp_f = None\n",
"\n",
" else:\n",
" # offload interpolation to scipy.interpolate\n",
" import scipy.interpolate as interp\n",
"\n",
" interp1d_kw_defaults = {\n",
" \"copy\": False,\n",
" \"kind\": 'linear',\n",
" \"assume_sorted\": True,\n",
" \"bounds_error\": True\n",
" }\n",
"\n",
" if self.periodic:\n",
" interp1d_kw_defaults['bounds_error'] = False\n",
" interp1d_kw_defaults['fill_value'] = (self.raw[-1], self.raw[0])\n",
" \n",
" # merge kwargs\n",
" if interp1d_kw is not True:\n",
" interp1d_kw = { **interp1d_kw_defaults, **interp1d_kw }\n",
"\n",
" self.interp_f = interp.interp1d(\n",
" np.arange(0, self.sample_length),\n",
" self.raw,\n",
" **interp1d_kw\n",
" )\n",
" else:\n",
" self.interp_f = None\n",
" \n",
" def __len__(self):\n",
" return self.sample_length\n",
" \n",
" def __call__(self, t_f = None, x_f = None, **kwargs):\n",
" \"\"\"\n",
" Allow this class to be used as a function.\n",
" \"\"\"\n",
" return self._translate(t_f, x_f, **kwargs)[0]\n",
" \n",
" def _translate(self, t_f = None, x_f = None, t_0 = None, x_0 = None, velocity = None):\n",
" \"\"\"\n",
" Translate the signal from (t_0, x_0) to (t_f, x_f) with optional velocity.\n",
" \n",
" Returns the signal at (t_f, x_f)\n",
" \"\"\"\n",
" \n",
" if t_0 is None:\n",
" t_0 = self.t_0\n",
" \n",
" if velocity is None:\n",
" velocity = 1\n",
"\n",
"\n",
" ## spatial offset\n",
" if x_f is None:\n",
" spatial_time_offset = 0\n",
" else:\n",
" x_f = np.asarray(x_f)\n",
" if x_0 is None:\n",
" x_0 = self.x_0\n",
"\n",
" spatial_time_offset = np.sum(np.sqrt( (x_f - x_0)**2 )/velocity)\n",
"\n",
" ## temporal offset\n",
" if t_f is None:\n",
" temporal_time_offset = 0\n",
" else:\n",
" t_f = np.asarray(t_f)\n",
" \n",
" if t_0 is None:\n",
" t_0 = self.t_0\n",
" \n",
" temporal_time_offset = t_f - t_0\n",
"\n",
" # total offset\n",
" total_time_offset = spatial_time_offset + temporal_time_offset\n",
"\n",
" # periodic signal\n",
" n_offset = (total_time_offset * sample_rate ) % self.sample_length\n",
"\n",
" # offload to scipy interpolation\n",
" if self.interp_f:\n",
" amplitude = self.interp_f(n_offset)\n",
" else:\n",
" # self written interpolation\n",
" n_offset_eps, n_offset_int = np.modf(n_offset)\n",
" n_offset_int = n_offset.astype(int)\n",
"\n",
" if True:\n",
" amplitude = (1-n_offset_eps) * self.raw[n_offset_int] \\\n",
" + n_offset_eps * self.raw[(n_offset_int + 1) % self.sample_length]\n",
"\n",
" else:\n",
" amplitude = self.raw[n_offset_int]\n",
"\n",
" \n",
" return amplitude, total_time_offset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Testing:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Interpolation: Sine wave"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA8MAAAEGCAYAAABBz0cYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nOzdeXxU5dXA8d+9syQhIQQSAgESSCAgSyBsYQcBxQW1rbRWigtVUKu41L3VVuurtmqpG1UrUaQuUPcFFdn3JSRh2CFAFrKHQBYSSGa59/3jJpMFgihhZhLO108+MHfuTB5MZuae5znPOYqu6wghhBBCCCGEEBcT1dsDEEIIIYQQQgghPE2CYSGEEEIIIYQQFx0JhoUQQgghhBBCXHQkGBZCCCGEEEIIcdGRYFgIIYQQQgghxEXH7O0BNLewsDC9R48e3h6GEEIIIYQQQogLICUlpVjX9Y7n+zytLhju0aMHycnJ3h6GEEIIIYQQQogLQFGUrOZ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ihz3sTbmNnRr240j5UeMH5SqMiZiDBvzN+LSXFhUC69Neo0xXcdIwFxfbYpI97HGm8aORbDnC6guN0rKD7wBOvU3KupJGokQQgghhPCmrM2w/QM4kQ9HNoPjJHSIgUHTjevW9j0u6hTos8U5ALf/cLt7wfGq6Ksot5ezMXcjmq5hNVkBqHJVNXjOAWED2Fu8F61BW9c6oyJGkRCRwOupr7vPCbIEUeGo4NDTh5olGG51rZUUFPxN/sydMBeA2ctm49AcWFQLl0VdRmphqvv2G5e9wSnnKR5c8yBOzYlFtdCnQx9yK3LR0VFQ6BXSC1VVsbvsaGhomsb
"text/plain": [
"<Figure size 1152x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Sine wave\n",
"sample_rate = 1e3 # Hz\n",
"f_sine = 10\n",
"time = np.arange(0, 1, 1/(sample_rate)) #s\n",
"time2 = np.arange(0, 1, 1/(sample_rate*2)) #s\n",
"phase_offset = 0\n",
"\n",
"signal_func = lambda t: np.cos(2*np.pi*f_sine*t + phase_offset)\n",
"\n",
"mysignal = TravelSignal(signal_func(time), sample_rate)\n",
"\n",
"fig, ax = plt.subplots(figsize=(16,4))\n",
"ax.plot(time, signal_func(time), '.-', label=\"Raw signal\")\n",
"ax.plot(time, mysignal(time)+0.5, '.-', label=\"MySignal +0.5\")\n",
"ax.plot(time2, mysignal(time2)+0.25, '.--', label=\"MySignal(interp) +0.25\")\n",
"ax.set_ylabel(\"Amplitude\")\n",
"ax.set_xlabel(\"Time\")\n",
"ax.set_xlim(0, 1/f_sine)\n",
"ax.legend()\n",
"plt.show();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Extrapolation: Sine wave"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7gAAAEGCAYAAABPZjl3AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAA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+rB1t7z4tkrcp3U3wAkLC56PknJpNVcEROWKz7p5PrZRGPBhuHJJHlvpQzH9NxpXghZy5eBz8jgcV2AnrnAsPZmBgYGBgUEDtsC1EpnKNEGLRFVSs+Uxr7HBDgJEnlqgeYe8rU1dzLCBD5XXtKsPT7wpb080HwJAZoFiwyIbnenyOOw8WyxA5KkFzioWXBNtnu3ClVM18vZE8yEDAwMDAwMDW+BaCjuIqvQ0KzY0NhRXA1d/Vt62QlRFGdUJ1w7DDj4c6lJshPGh1QJEkcSRRKgEiCzyYSRxpHDHrZrnSxHEkQDqz8Wfkrct40qseRYPW+TDQWW5QRgfVijaajGuhEcsrjAwMDAwMPwvB1vgWgk7iKpIadKO8GIllY/IYytEVaIJvojHRQEiy8RzxDq9CIIvVgsQld4o14dG9GEqHVvlw5Ib5LFSHElE6Y2Qvq6s4kpgTBhE4IrVAkTRhMTE4w6RKw5r5lm5eAznwwqFzZZxJYYPreZK+mRhwASmGBgYGBgYwoEtcK0GsVCQxlMLHH6RjrXCPiLsIKoSTRypuBpY+Q065iwQz/HUArt+RsfhRHNEWC3iFE0cqbgauP1J+TwrBIiGuxUbEQSGrObK0b/ScSSuSCnKgG25co2wCOeC1nDl41/QcVSuWCzWFU10r7gauOX7wmGLvm8+/C4dE8IEphgYGBgYGMKALXCtROtuQd0WFqbjieJIEYR9oolQmYGWj+SxVvBFBFHUPgZ95trYulutuhrJh7yFPlTNYQQ
"text/plain": [
"<Figure size 1152x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Sine wave\n",
"sample_rate = 1e3 # Hz\n",
"f_sine = 10\n",
"time = np.arange(0, 1, 1/(sample_rate)) #s\n",
"time2 = np.arange(3/4, 2, 1/(sample_rate*2)) #s\n",
"phase_offset = 0\n",
"\n",
"signal_func = lambda t: np.cos(2*np.pi*f_sine*t + phase_offset)\n",
"\n",
"mysignal = TravelSignal(signal_func(time), sample_rate)\n",
"\n",
"fig, ax = plt.subplots(figsize=(16,4))\n",
"ax.plot(time, signal_func(time), '.-', label=\"Raw signal\")\n",
"ax.plot(time, mysignal(time)+0.5, '.-', label=\"MySignal +0.5\")\n",
"ax.plot(time2, mysignal(time2)+0.25, '.--', label=\"MySignal(interp) +0.25\")\n",
"ax.set_ylabel(\"Amplitude\")\n",
"ax.set_xlabel(\"Time\")\n",
"ax.legend()\n",
"plt.show();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Interpolation: Delta function"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P Raw S1 S2\n",
"a 1.0 1.0 0.9130434782608696\n",
"T 0.4666666666666667 0.4666666666666667 0.4782608695652174\n",
"[0. 0.03333333 0.06666667 0.1 0.13333333 0.16666667\n",
" 0.2 0.23333333 0.26666667 0.3 0.33333333 0.36666667\n",
" 0.4 0.43333333 0.46666667 0.5 0.53333333 0.56666667\n",
" 0.6 0.63333333 0.66666667 0.7 0.73333333 0.76666667\n",
" 0.8 0.83333333 0.86666667 0.9 0.93333333 0.96666667] [0. 0.01449275 0.02898551 0.04347826 0.05797101 0.07246377\n",
" 0.08695652 0.10144928 0.11594203 0.13043478 0.14492754 0.15942029\n",
" 0.17391304 0.1884058 0.20289855 0.2173913 0.23188406 0.24637681\n",
" 0.26086957 0.27536232 0.28985507 0.30434783 0.31884058 0.33333333\n",
" 0.34782609 0.36231884 0.37681159 0.39130435 0.4057971 0.42028986\n",
" 0.43478261 0.44927536 0.46376812 0.47826087 0.49275362 0.50724638\n",
" 0.52173913 0.53623188 0.55072464 0.56521739 0.57971014 0.5942029\n",
" 0.60869565 0.62318841 0.63768116 0.65217391 0.66666667 0.68115942\n",
" 0.69565217 0.71014493 0.72463768 0.73913043 0.75362319 0.76811594\n",
" 0.7826087 0.79710145 0.8115942 0.82608696 0.84057971 0.85507246\n",
" 0.86956522 0.88405797 0.89855072 0.91304348 0.92753623 0.94202899\n",
" 0.95652174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scff2TSpEkcPnzYfm7v3r386le/Yvv27QQFBbFo0SIAJk+ezPr169m8eTMxMTG8+eab5/VtPXr0KF27drW/joyM5OjRow22feWVV4iPj2f69Onk5ekDyIa4ssA1GjhmNtTQNM3XTNNMMU0zJSwszMmxRERE6uoXVImnUcU4yzrc3WpvU75YDY4KZrx1PX5GOSHW8ov6vYpI2xEfH096ejrvvvsuV155ZZ1z06dP5513ap+KnD9/PtOmTQNg2rRp7Ny5kylTprBixQoGDx5MRUVFnWtXrVrFTTfdBMC4cePq3Bbco0cPEhMTAUhOTiY9PR2Abdu2MWLECOLi4li4cCHbt29vMvtbb71lf242LS2NK6+8ksTERCZNmgSAadYvgQyjfqk0c+ZM9u/fz6ZNm+jcuTO/+c1vmhy3vXLlKsoZQNfTXkcCx1yURUREpFGRXuUA9LFk8I/Lgy/q51KDivcSYWQD8MyVERf1exWRZhr/tEuHnzBhAg888AArVqwgJ+fnu0u6du1KeHg4y5Yt44cffrDP5gJ06dKF6dOnM336dGJjY9m2re76tw0Vl6d4enrav7ZarfZblKdOncrHH39MQkICCxYsYMWKFU3mnjZtmr3obugZ3MjIyDp9ZGRkkJqaWq+f8PBw+9d33XXXWRfZaq9cOYP7KXD7T6spDwYKTNM87sI8IiIiDSrLP2n/2vvgEhcmcb6T6z+yf93Z/fyeKxMRcaTp06fz6KOP2p+lPd2dd97Jrbfeyg033IDVagXg66+/pqqqCoATJ06Qk5NTb3Gq4cO
"text/plain": [
"<Figure size 1152x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"## 1d signal: Delta peak with time interval\n",
"sample_rate = 3e1 # Hz\n",
"interp_sample_rate = 2.3*sample_rate\n",
"time = np.arange(0, 1, 1/sample_rate) #s\n",
"time2 = np.arange(0, 1, 1/(interp_sample_rate)) #s\n",
"\n",
"signal = np.zeros(len(time))\n",
"signal[len(signal)//2-1] = 1\n",
"signal[len(signal)//2] = 0.75\n",
"signal[len(signal)//2+1] = 1\n",
"\n",
"mysignal = TravelSignal(signal, sample_rate+20)\n",
"time_signal, time_offsets = mysignal._translate(time)\n",
"time2_signal, time2_offsets = mysignal._translate(time2)\n",
"\n",
"raw_max_idx = np.argmax(signal)\n",
"my_max_idx = np.argmax(mysignal(time))\n",
"my2_max_idx = np.argmax(mysignal(time2))\n",
"\n",
"\n",
"fig, ax = plt.subplots(1,1,sharex=True, figsize=(16,8))\n",
"ax.plot(time, mysignal.raw, '.-', label=\"Raw signal\")\n",
"ax.plot(time, time_signal, '-', label=\"MySignal +0.5\")\n",
"ax.plot(time2, time2_signal, '--', label=\"MySignal(interp) +0.25\")\n",
"ax.plot(np.array([0, 1/sample_rate]) + time[raw_max_idx], [1.02, 1.02], label=\"Orig Samplerate\")\n",
"ax.plot(np.array([0, 1/interp_sample_rate]) + time[raw_max_idx], [1.01, 1.01], label=\"Interp Samplerate\")\n",
"ax.set_ylabel(\"Amplitude\")\n",
"ax.set_xlabel(\"Time\")\n",
"ax.set_xlim(0, 1)\n",
"ax.legend()\n",
"\n",
"\n",
"print(\"P\", \"Raw\", \"S1\", \"S2\")\n",
"print(\"a\", signal[raw_max_idx], mysignal(time)[my_max_idx], mysignal(time2)[my2_max_idx])\n",
"print(\"T\", time[raw_max_idx], time[my_max_idx], time2[my2_max_idx])\n",
"\n",
"print(time_offsets, time2_offsets)\n",
"plt.show();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}