GDET3 Faltungsbeispiel.ipynb 12.7 KB
Newer Older
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
1
2
3
4
5
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
6
7
8
9
10
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
11
12
   "outputs": [],
   "source": [
13
    "# Copyright 2020 Institut für Nachrichtentechnik, RWTH Aachen University\n",
14
    "%matplotlib widget\n",
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's avatar
Christian Rohlfing committed
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's avatar
- init  
Christian Rohlfing committed
28
29
30
31
32
33
34
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div>\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
35
    "    <img src=\"ient_nb/figures/rwth_ient_logo@2x.png\" style=\"float: right;height: 5em;\">\n",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
36
37
    "</div>\n",
    "\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
38
    "# Beispiel zur Berechnung des Faltungsintegrals\n",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
39
    "## Einleitung\n",
40
    "Das Faltungsintegral ist definiert als\n",
41
42
    "$$g(t) \n",
    "= s(t)\\ast h(t)\n",
43
    "= \\int\\limits_{-\\infty}^{\\infty} s(\\tau) h(t-\\tau) \\mathrm{d}\\tau\\text{ .}$$"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
44
45
46
47
48
49
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Christian Rohlfing's avatar
Christian Rohlfing committed
50
    "## Herleitung\n",
51
    "### Eingangssignal und Impulsantwort\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
52
    "Wie im Abschnitt 1.6 des Skripts gezeigt, wird hier die Faltung anhand der Impulsantwort des $RC$-Systems \n",
53
    "$$h(t) = \\frac{1}{T}\\varepsilon(t)\\mathrm{e}^{-t/T}$$ \n",
Christian Rohlfing's avatar
Christian Rohlfing committed
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",
56
    "untersucht werden. Beide Funktionen sind in der folgenden Abbildung dargestellt."
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
57
58
59
60
61
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
62
   "metadata": {},
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
63
64
   "outputs": [],
   "source": [
65
66
67
    "# Input signal\n",
    "T0 = 4 # duration of rect\n",
    "a = 1/4 # amplitude of rect\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
68
    "s = lambda t: a*rect((t-T0/2)/T0) # Rect impuls with duration T0 and amplitude a\n",
69
70
71
    "\n",
    "# Impulse response h(t) of RC system\n",
    "T  = 2\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
72
    "h = lambda t: 1/T*unitstep(t)*np.exp(-t/T) # RC system with T=R*C\n",
73
74
    "\n",
    "# Plot both signals\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
75
    "fig,ax = plt.subplots(1,1); ax.plot(tau, h(tau), 'rwth'); ax.plot(tau, s(tau), 'grun'); \n",
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's avatar
Christian Rohlfing committed
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); "
79
80
81
82
83
84
85
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Herleitung Faltungsintegral"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
86
87
88
89
90
91
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
92
    "$h(\\tau)$ wird zunächst gespiegelt, das resultierende Signal ist $h(-\\tau)$. "
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
93
94
95
96
97
98
99
100
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
Christian Rohlfing's avatar
Christian Rohlfing committed
101
    "fig,ax = plt.subplots(1,1); ax.plot(tau, h(-tau), 'rwth'); \n",
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's avatar
- init  
Christian Rohlfing committed
104
105
106
107
108
109
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
110
    "Die gespiegelte Version $h(-\\tau)$ wird um $t$ verschoben, wobei $t$ sowohl positive als auch negative Werte annehmen kann."
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
111
112
113
114
115
116
117
118
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
119
    "fig,ax = plt.subplots(1,1); \n",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
120
    "\n",
121
    "t = 2\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
122
123
    "ax.plot(tau, h(t-tau), 'rwth'); \n",
    "ient_annotate_xtick(ax, r'$t>0$', t, -0.05, 'rwth')\n",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
124
    "\n",
125
    "t = -3\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
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's avatar
- init  
Christian Rohlfing committed
128
    "\n",
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's avatar
- init  
Christian Rohlfing committed
131
132
133
134
135
136
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
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",
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's avatar
- init  
Christian Rohlfing committed
142
143
144
145
146
147
148
149
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
150
151
    "t = -3\n",
    "\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
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",
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's avatar
- init  
Christian Rohlfing committed
156
157
158
159
160
161
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
162
163
    "### Intervall II: $0<t\\leq T_0$\n",
    "\n",
164
    "Nun taucht $h(t-\\tau)$ in $s(\\tau)$ ein, und zwar für Zeiten $0<t\\leq T_0$:"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
165
166
167
168
169
170
171
172
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
173
    "t = T0/2 # ensure 0<t<=T0\n",
174
    "\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
175
    "fig,ax = plt.subplots(1,1); ax.plot(tau, h(t-tau), 'rwth'); ax.plot(tau, s(tau), '-.', color='grun'); \n",
176
177
    "mask = (tau>0) & (tau<t);\n",
    "ax.fill_between(tau[mask],0,h(t-tau[mask]), facecolor=\"none\", hatch=\"//\", edgecolor='k', linewidth=0.0);\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
178
179
    "ient_annotate_xtick(ax, r'$0<t\\leq T_0$', t, -0.075, 'rwth'); \n",
    "ient_annotate_xtick(ax, r'$T_0$', T0, -0.125, 'grun'); \n",
180
181
    "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's avatar
- init  
Christian Rohlfing committed
182
183
184
185
   ]
  },
  {
   "cell_type": "markdown",
186
187
188
189
190
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
191
   "source": [
192
    "In diesem Fall ($0<t\\leq T_0$) ist das Produkt $s(\\tau)h(t-\\tau)$ nur im Intervall $0<\\tau <t$ von Null verschieden. Dieses Interval begrenzt nun das Faltungsintegral\n",
193
194
    "$$g(t) = \\int\\limits_0^t s(\\tau)h(t-\\tau) \\mathrm{d}\\tau = a \\left(1-\\mathrm{e}^{-t/T}\\right)\\qquad \\text{für } 0<t\\leq T_0\\text{.}$$\n",
    "Mehr Zwischenschritte werden im Skript gezeigt.\n",
195
    "\n",
196
197
    "### Intervall III: $t>T_0$\n",
    "Für alle Zeiten $t>T_0$ liegt $s(\\tau)$ komplett in $h(t-\\tau)$:"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
198
199
200
201
202
203
204
205
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
206
    "t = T0 + 2 # ensure t>T0\n",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
207
    "\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
208
    "fig,ax = plt.subplots(1,1); ax.plot(tau, h(t-tau), 'rwth'); ax.plot(tau, s(tau), '-.', color='grun'); \n",
209
210
    "mask = (tau>0) & (tau<T0);\n",
    "ax.fill_between(tau[mask],0,h(t-tau[mask]), facecolor=\"none\", hatch=\"//\", edgecolor='k', linewidth=0.0);\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
211
212
    "ient_annotate_xtick(ax, r'$t>T_0$', t, -0.075, 'rwth'); \n",
    "ient_annotate_xtick(ax, r'$T_0$', T0, -0.125, 'grun'); \n",
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's avatar
- init  
Christian Rohlfing committed
215
216
217
218
219
220
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
221
    "In diesem Fall ($t>T_0$) ist das Produkt $s(\\tau)h(t-\\tau)$ in dem festen Intervall $0<\\tau<T_0$, welches von $s(\\tau)$ bestimmt wird, von Null verschieden\n",
222
    "$$g(t) = \\int\\limits_0^{T_0} s(\\tau)h(t-\\tau) \\mathrm{d}\\tau = a \\left(\\mathrm{e}^{T_0/T}-1\\right)\\mathrm{e}^{-t/T}\\quad \\text{für } t>T_0\\text{.}$$\n"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
223
224
225
226
227
228
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
229
230
    "### Betrachtung aller Intervalle gemeinsam\n",
    "\n",
231
    "Insgesamt lässt sich $g(t)$ nun wie folgt darstellen:"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
232
233
234
235
236
237
238
239
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
240
241
    "def g(t):\n",
    "    out = np.zeros_like(t) # initialize with zeros, covers first interval t<0\n",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
242
    "    \n",
243
244
245
246
247
248
    "    # Second interval 0<t<= T0 (cropped with rect(t/T0-1/2))\n",
    "    out += a*(1-np.exp(-t/T))*rect(t/T0-1/2)\n",
    "    \n",
    "    # Third interval t>T0 (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's avatar
- init  
Christian Rohlfing committed
249
    "\n",
250
    "t = tau # now we need the whole t axis\n",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
251
    "\n",
252
    "# Plot both s(t) and g(t)\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
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",
257
    "ax.set_xlabel(r'$\\rightarrow t$'); ax.set_ylabel(r'$\\uparrow g(t)/a$'); \n",
Christian Rohlfing's avatar
Christian Rohlfing committed
258
    "ax.set_xlim([-6,16]); ax.set_ylim([-0.15,1.19]); ient_axis(ax);\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
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's avatar
- init  
Christian Rohlfing committed
261
262
263
   ]
  },
  {
264
   "cell_type": "markdown",
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
265
266
   "metadata": {},
   "source": [
267
    "### Interaktive Demo"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
268
269
270
271
272
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
Christian Rohlfing's avatar
Christian Rohlfing committed
273
274
275
276
277
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
278
   "outputs": [],
279
   "source": [
280
    "fig, axs = plt.subplots(2, 1, figsize=(8,6))\n",
281
282
    "gt = g(t)\n",
    "\n",
283
    "@widgets.interact(t=widgets.FloatSlider(min=-5, max=15, step=.2, value=5,description='$t$', continuous_update=True))\n",
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",
286
287
    "    g_plot = gt.copy()/a; g_plot[t_ind:] = np.nan; # cropped g(t)\n",
    "    mask = (tau>0) & (tau<t) & (tau < T0); # mask for hatch\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
288
289
290
291
    "    if not axs[0].lines: # call plot() and decorate axes. Usually, these functions take some processing time\n",
    "        ax = axs[0]; line_h,=ax.plot(tau,h(t-tau), 'rwth'); # plot h(t-tau)\n",
    "        ient_annotate_xtick(ax, r'$t$', t, -0.075, line_h.get_color(), 14); \n",
    "        ax.plot(tau, s(tau),'-.',color='grun', zorder=1)\n",
292
293
    "        ax.fill_between(tau[mask],0,h(t-tau[mask]), facecolor=\"none\", hatch=\"//\", edgecolor='k', linewidth=0.0);\n",
    "        \n",
294
295
    "        ax.set_xlabel(r'$\\rightarrow \\tau$'); ax.set_ylabel(r'$\\uparrow h(t-\\tau)$'); \n",
    "        ax.set_xlim([-6,16]); ax.set_ylim([-.19,.65]); ient_axis(ax)\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
296
297
    "                \n",
    "        ax = axs[1]; ax.plot(tau,g_plot, 'rwth'); # plot g(t)\n",
298
299
300
    "        ax.plot([t, t], [0, gt[t_ind]/a], 'ko--', lw=1);\n",
    "        ax.set_xlabel(r'$\\rightarrow t$'); ax.set_ylabel(r'$\\uparrow g(t)/a$'); \n",
    "        ax.set_xlim([-6,16]); ax.set_ylim([-.15,1.19]);ient_axis(ax)\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
301
302
303
304
305
306
307
308
309
310
    "\n",
    "    else: # If lines exist, replace only xdata and ydata since plt.plot() takes longer time\n",
    "        axs[0].lines[0].set_ydata(h(t-tau));\n",
    "        axs[0].texts[0].set_x(t); axs[0].lines[1].set_xdata([t,t])\n",
    "        axs[0].collections[0].remove(); \n",
    "        axs[0].fill_between(tau[mask],0,h(t-tau[mask]), facecolor=\"none\", hatch=\"//\", edgecolor='k', linewidth=0.0);\n",
    "        \n",
    "        axs[1].lines[0].set_ydata(g_plot);\n",
    "        axs[1].lines[1].set_xdata([t, t]); axs[1].lines[1].set_ydata([0, gt[t_ind]/a]);\n",
    "        "
311
312
313
314
   ]
  },
  {
   "cell_type": "markdown",
Christian Rohlfing's avatar
Christian Rohlfing committed
315
316
   "metadata": {},
   "source": [
Iris Heisterklaus's avatar
Iris Heisterklaus committed
317
    "Verschiebe den Schieberegler langsam von links nach rechts. Beobachte die oben beschriebenen Intervalle und ihre Grenzen. An welchen Stellen ändert sich das Verhalten von $g(t)$? \n",
Christian Rohlfing's avatar
Christian Rohlfing committed
318
    "\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
319
320
321
322
323
    "Eine ausführlichere Demo zur Faltung mit einer Auswahl für $s(t)$ und $h(t)$ findet man [hier](GDET3%20Faltung%20GUI.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
324
325
   "metadata": {},
   "source": [
Christian Rohlfing's avatar
Christian Rohlfing committed
326
    "---\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
327
    "This notebook is provided as [Open Educational Resource](https://en.wikipedia.org/wiki/Open_educational_resources) (OER). Feel free to use the notebook for your own purposes. The code is licensed under the [MIT license](https://opensource.org/licenses/MIT). \n",
328
    "\n",
Christian Rohlfing's avatar
Christian Rohlfing committed
329
    "Please attribute the work as follows: \n",
330
    "*Christian Rohlfing, Übungsbeispiele zur Vorlesung \"Grundgebiete der Elektrotechnik 3 - Signale und Systeme\"*, gehalten von Jens-Rainer Ohm, 2020, Institut für Nachrichtentechnik, RWTH Aachen University."
331
   ]
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  }
 ],
 "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",
Christian Rohlfing's avatar
Christian Rohlfing committed
350
   "version": "3.8.1"
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
351
352
353
  }
 },
 "nbformat": 4,
354
 "nbformat_minor": 4
Christian Rohlfing's avatar
- init  
Christian Rohlfing committed
355
}