GDET3 Faltungsbeispiel.ipynb 12.7 KB
 Christian Rohlfing committed Nov 09, 2018 1 2 3 4 5 { "cells": [ { "cell_type": "code", "execution_count": null,  Christian Rohlfing committed Sep 03, 2019 6 7 8 9 10  "metadata": { "jupyter": { "source_hidden": true } },  Christian Rohlfing committed Nov 09, 2018 11 12  "outputs": [], "source": [  Hafiz Emin Kosar committed Mar 19, 2020 13  "# Copyright 2020 Institut für Nachrichtentechnik, RWTH Aachen University\n",  Christian Rohlfing committed Sep 03, 2019 14  "%matplotlib widget\n",  Christian Rohlfing committed Apr 26, 2019 15 16 17 18 19 20 21 22  "\n", "from ipywidgets import interact, interactive\n", "import ipywidgets as widgets\n", "from IPython.display import clear_output, display, HTML\n", "\n", "import numpy as np\n", "from scipy import signal\n", "\n",  Christian Rohlfing committed May 14, 2019 23 24 25 26 27  "from ient_nb.ient_plots import *\n", "from ient_nb.ient_signals import *\n", "\n", "# tau-axis\n", "(tau,deltat) = np.linspace(-20, 20, 4001, retstep=True)"  Christian Rohlfing committed Nov 09, 2018 28 29 30 31 32 33 34  ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",  Christian Rohlfing committed Apr 29, 2019 35  " \n",  Christian Rohlfing committed Nov 09, 2018 36 37  "
\n", "\n",  Christian Rohlfing committed Apr 29, 2019 38  "# Beispiel zur Berechnung des Faltungsintegrals\n",  Christian Rohlfing committed Nov 09, 2018 39  "## Einleitung\n",  Iris Heisterklaus committed Aug 26, 2019 40  "Das Faltungsintegral ist definiert als\n",  Christian Rohlfing committed Apr 26, 2019 41 42  "$$g(t) \n", "= s(t)\\ast h(t)\n",  Christian Rohlfing committed Sep 03, 2019 43  "= \\int\\limits_{-\\infty}^{\\infty} s(\\tau) h(t-\\tau) \\mathrm{d}\\tau\\text{ .}$$"  Christian Rohlfing committed Nov 09, 2018 44 45 46 47 48 49  ] }, { "cell_type": "markdown", "metadata": {}, "source": [  Christian Rohlfing committed May 14, 2019 50  "## Herleitung\n",  Christian Rohlfing committed Apr 27, 2019 51  "### Eingangssignal und Impulsantwort\n",  Christian Rohlfing committed May 14, 2019 52  "Wie im Abschnitt 1.6 des Skripts gezeigt, wird hier die Faltung anhand der Impulsantwort des $RC$-Systems \n",  Christian Rohlfing committed Apr 26, 2019 53  "$$h(t) = \\frac{1}{T}\\varepsilon(t)\\mathrm{e}^{-t/T}$$ \n",  Christian Rohlfing committed May 14, 2019 54 55  "mit $T=RC$ gezeigt. Es soll dessen Reaktion auf ein Rechteckimpuls der Dauer $T_0$ und der Amplitude $a$\n", "$$s(t) = a\\;\\mathrm{rect}\\left(\\frac{t-T_0/2}{T_0}\\right)$$\n",  Iris Heisterklaus committed Aug 26, 2019 56  "untersucht werden. Beide Funktionen sind in der folgenden Abbildung dargestellt."  Christian Rohlfing committed Nov 09, 2018 57 58 59 60 61  ] }, { "cell_type": "code", "execution_count": null,  Christian Rohlfing committed Apr 26, 2019 62  "metadata": {},  Christian Rohlfing committed Nov 09, 2018 63 64  "outputs": [], "source": [  Christian Rohlfing committed Apr 26, 2019 65 66 67  "# Input signal\n", "T0 = 4 # duration of rect\n", "a = 1/4 # amplitude of rect\n",  Christian Rohlfing committed Apr 30, 2019 68  "s = lambda t: a*rect((t-T0/2)/T0) # Rect impuls with duration T0 and amplitude a\n",  Christian Rohlfing committed Apr 26, 2019 69 70 71  "\n", "# Impulse response h(t) of RC system\n", "T = 2\n",  Christian Rohlfing committed Apr 30, 2019 72  "h = lambda t: 1/T*unitstep(t)*np.exp(-t/T) # RC system with T=R*C\n",  Christian Rohlfing committed Apr 26, 2019 73 74  "\n", "# Plot both signals\n",  Christian Rohlfing committed Apr 30, 2019 75  "fig,ax = plt.subplots(1,1); ax.plot(tau, h(tau), 'rwth'); ax.plot(tau, s(tau), 'grun'); \n",  Christian Rohlfing committed Apr 26, 2019 76  "ax.set_xlabel(r'$\\rightarrow \\tau$'); ax.set_xlim([-11,11]); ax.set_ylim([-0.05,0.55]); ient_axis(ax);\n",  Christian Rohlfing committed Apr 30, 2019 77 78  "ax.text(T/4, 1/T, r'$h(\\tau)$', color='rwth', fontsize=12);\n", "ax.text(T0, a, r'$s(\\tau)$', color='grun', fontsize=12); "  Christian Rohlfing committed Apr 26, 2019 79 80 81 82 83 84 85  ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Herleitung Faltungsintegral"  Christian Rohlfing committed Nov 09, 2018 86 87 88 89 90 91  ] }, { "cell_type": "markdown", "metadata": {}, "source": [  Iris Heisterklaus committed Aug 26, 2019 92  "$h(\\tau)$ wird zunächst gespiegelt, das resultierende Signal ist $h(-\\tau)$. "  Christian Rohlfing committed Nov 09, 2018 93 94 95 96 97 98 99 100  ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [  Christian Rohlfing committed Apr 30, 2019 101  "fig,ax = plt.subplots(1,1); ax.plot(tau, h(-tau), 'rwth'); \n",  Christian Rohlfing committed Apr 26, 2019 102 103  "ax.set_xlabel(r'$\\rightarrow \\tau$'); ax.set_ylabel(r'$\\uparrow h(-\\tau)$'); \n", "ax.set_xlim([-11,11]); ax.set_ylim([-0.05,0.55]); ient_axis(ax);"  Christian Rohlfing committed Nov 09, 2018 104 105 106 107 108 109  ] }, { "cell_type": "markdown", "metadata": {}, "source": [  Iris Heisterklaus committed Aug 26, 2019 110  "Die gespiegelte Version $h(-\\tau)$ wird um $t$ verschoben, wobei $t$ sowohl positive als auch negative Werte annehmen kann."  Christian Rohlfing committed Nov 09, 2018 111 112 113 114 115 116 117 118  ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [  Christian Rohlfing committed Apr 26, 2019 119  "fig,ax = plt.subplots(1,1); \n",  Christian Rohlfing committed Nov 09, 2018 120  "\n",  Christian Rohlfing committed Apr 26, 2019 121  "t = 2\n",  Christian Rohlfing committed Apr 30, 2019 122 123  "ax.plot(tau, h(t-tau), 'rwth'); \n", "ient_annotate_xtick(ax, r'$t>0$', t, -0.05, 'rwth')\n",  Christian Rohlfing committed Nov 09, 2018 124  "\n",  Christian Rohlfing committed Apr 26, 2019 125  "t = -3\n",  Christian Rohlfing committed Apr 30, 2019 126 127  "line_h2,= ax.plot(tau, h(t-tau), 'rot'); \n", "ient_annotate_xtick(ax, r'$t<0$', t, -0.05, 'rot')\n",  Christian Rohlfing committed Nov 09, 2018 128  "\n",  Christian Rohlfing committed Apr 26, 2019 129 130  "ax.set_xlim([-11,11]); ax.set_ylim([-0.09,0.55]);\n", "ax.set_xlabel(r'$\\rightarrow \\tau$'); ax.set_ylabel(r'$\\uparrow h(t-\\tau)$'); ient_axis(ax);"  Christian Rohlfing committed Nov 09, 2018 131 132 133 134 135 136  ] }, { "cell_type": "markdown", "metadata": {}, "source": [  Christian Rohlfing committed Apr 27, 2019 137 138 139  "### Intervall I: $t<0$\n", "\n", "Betrachtet man nun $s(\\tau)$ und $h(t-\\tau)$ für $t<0$ gemeinsam, wird klar, dass in diesem Beispiel für den Integranden des Faltungsintegrals gilt:\n",  Iris Heisterklaus committed Aug 26, 2019 140 141  "$$s(\\tau)h(t-\\tau)=0$$ und daher $$g(t)=0 \\qquad \\text{für } t<0 \\text{.}$$\n", "Dies gilt für alle Bereiche, in denen keine Überlappung der beiden Signale besteht. "  Christian Rohlfing committed Nov 09, 2018 142 143 144 145 146 147 148 149  ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [  Christian Rohlfing committed Apr 26, 2019 150 151  "t = -3\n", "\n",  Christian Rohlfing committed Apr 30, 2019 152 153  "fig,ax = plt.subplots(1,1); ax.plot(tau, h(t-tau), 'rwth'); ax.plot(tau, s(tau), '-.', color='grun');\n", "ient_annotate_xtick(ax, r'$t<0$', t, -0.05, 'rwth'); \n",  Christian Rohlfing committed Apr 26, 2019 154 155  "ax.set_xlabel(r'$\\rightarrow \\tau$'); ax.set_ylabel(r'$\\uparrow h(t-\\tau)$');\n", "ax.set_xlim([-11,11]); ax.set_ylim([-0.09,0.55]); ient_axis(ax);"  Christian Rohlfing committed Nov 09, 2018 156 157 158 159 160 161  ] }, { "cell_type": "markdown", "metadata": {}, "source": [  Christian Rohlfing committed Apr 27, 2019 162 163  "### Intervall II: $00) & (tauT_0$\n", "Für alle Zeiten $t>T_0$ liegt $s(\\tau)$ komplett in $h(t-\\tau)$:"  Christian Rohlfing committed Nov 09, 2018 198 199 200 201 202 203 204 205  ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [  Christian Rohlfing committed Apr 26, 2019 206  "t = T0 + 2 # ensure t>T0\n",  Christian Rohlfing committed Nov 09, 2018 207  "\n",  Christian Rohlfing committed Apr 30, 2019 208  "fig,ax = plt.subplots(1,1); ax.plot(tau, h(t-tau), 'rwth'); ax.plot(tau, s(tau), '-.', color='grun'); \n",  Christian Rohlfing committed Apr 26, 2019 209 210  "mask = (tau>0) & (tauT_0$', t, -0.075, 'rwth'); \n", "ient_annotate_xtick(ax, r'$T_0$', T0, -0.125, 'grun'); \n",  Christian Rohlfing committed Apr 26, 2019 213 214  "ax.set_xlabel(r'$\\rightarrow \\tau$'); ax.set_ylabel(r'$\\uparrow h(t-\\tau)$'); \n", "ax.set_xlim([-11,11]); ax.set_ylim([-0.19,0.55]); ient_axis(ax);"  Christian Rohlfing committed Nov 09, 2018 215 216 217 218 219 220  ] }, { "cell_type": "markdown", "metadata": {}, "source": [  Christian Rohlfing committed Apr 26, 2019 221  "In diesem Fall ($t>T_0$) ist das Produkt$s(\\tau)h(t-\\tau)$in dem festen Intervall$0<\\tauT_0\\text{.}\n"  Christian Rohlfing committed Nov 09, 2018 223 224 225 226 227 228  ] }, { "cell_type": "markdown", "metadata": {}, "source": [  Christian Rohlfing committed Apr 27, 2019 229 230  "### Betrachtung aller Intervalle gemeinsam\n", "\n",  Christian Rohlfing committed Apr 26, 2019 231  "Insgesamt lässt sich $g(t)$ nun wie folgt darstellen:"  Christian Rohlfing committed Nov 09, 2018 232 233 234 235 236 237 238 239  ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [  Christian Rohlfing committed Apr 26, 2019 240 241  "def g(t):\n", " out = np.zeros_like(t) # initialize with zeros, covers first interval t<0\n",  Christian Rohlfing committed Nov 09, 2018 242  " \n",  Christian Rohlfing committed Apr 26, 2019 243 244 245 246 247 248  " # Second interval 0T0 (cropped with unitstep(t-T0))\n", " out += a*(np.exp(T0/T)-1)*np.exp(-t/T)*unitstep(t-T0)\n", " return out\n",  Christian Rohlfing committed Nov 09, 2018 249  "\n",  Christian Rohlfing committed Apr 26, 2019 250  "t = tau # now we need the whole t axis\n",  Christian Rohlfing committed Nov 09, 2018 251  "\n",  Christian Rohlfing committed Apr 26, 2019 252  "# Plot both s(t) and g(t)\n",  Christian Rohlfing committed Apr 30, 2019 253 254 255 256  "fig,ax = plt.subplots(1,1, figsize=(8,4)); \n", "ax.plot(t, g(t)/a, 'rwth'); \n", "ax.plot(t, s(t)/a,'-.', color='grun',zorder=1); \n", "ient_annotate_xtick(ax, r'$T_0$', T0, -0.125, 'grun'); \n",  Christian Rohlfing committed Apr 26, 2019 257  "ax.set_xlabel(r'$\\rightarrow t$'); ax.set_ylabel(r'$\\uparrow g(t)/a$'); \n",  Christian Rohlfing committed Apr 29, 2019 258  "ax.set_xlim([-6,16]); ax.set_ylim([-0.15,1.19]); ient_axis(ax);\n",  Christian Rohlfing committed Apr 30, 2019 259 260  "ax.text(-5,0.5,'Intervall I', color='black-50', fontsize=12); \n", "ax.text(2.5,0.5,'II', color='black-50', fontsize=12); ax.text(7.5,0.5,'III', color='black-50', fontsize=12);"  Christian Rohlfing committed Nov 09, 2018 261 262 263  ] }, {  Christian Rohlfing committed Apr 26, 2019 264  "cell_type": "markdown",  Christian Rohlfing committed Nov 09, 2018 265 266  "metadata": {}, "source": [  Christian Rohlfing committed Apr 26, 2019 267  "### Interaktive Demo"  Christian Rohlfing committed Nov 09, 2018 268 269 270 271 272  ] }, { "cell_type": "code", "execution_count": null,  Christian Rohlfing committed Mar 09, 2020 273 274 275 276 277  "metadata": { "jupyter": { "source_hidden": true } },  Christian Rohlfing committed Nov 09, 2018 278  "outputs": [],  Christian Rohlfing committed Apr 26, 2019 279  "source": [  Christian Rohlfing committed Apr 27, 2019 280  "fig, axs = plt.subplots(2, 1, figsize=(8,6))\n",  Christian Rohlfing committed Apr 26, 2019 281 282  "gt = g(t)\n", "\n",  Christian Rohlfing committed Apr 29, 2019 283  "@widgets.interact(t=widgets.FloatSlider(min=-5, max=15, step=.2, value=5,description='$t$', continuous_update=True))\n",  Christian Rohlfing committed Apr 26, 2019 284 285  "def update_plot(t):\n", " t_ind = np.where(tau>=t); t_ind = t_ind[0][0]; # find index corresponding to t\n",  Christian Rohlfing committed Apr 29, 2019 286 287  " g_plot = gt.copy()/a; g_plot[t_ind:] = np.nan; # cropped g(t)\n", " mask = (tau>0) & (tau