Commit 40ea7565 authored by Hafiz Emin Kosar's avatar Hafiz Emin Kosar
Browse files

- updated notebook for discrete time convolution

parent d49280f9
......@@ -3,14 +3,18 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Copyright 2019 Institut für Nachrichtentechnik, RWTH Aachen University\n",
"%matplotlib widget\n",
"\n",
"import ipywidgets as widgets\n",
"from ipywidgets import interact, interactive, fixed, Layout\n",
"from ipywidgets import interact, interactive, fixed, Layout, HBox, VBox\n",
"from IPython.display import clear_output, display, HTML\n",
"\n",
"from scipy import signal # convolution\n",
......@@ -43,21 +47,18 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"M = 1\n",
"b = 0.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"n = np.linspace(-10, 10, 21)\n",
"m = np.linspace(-40, 40, 81) # m Achse\n",
"s_type = h_type = ''; s_n0 = h_n0 = 0\n",
"M = 1\n",
"b = .5\n",
"\n",
"n0 = -2\n",
"\n",
......@@ -71,22 +72,56 @@
" 'Rechteck' : lambda n: unitstep(n+M) - unitstep(n-M-1)\n",
" }\n",
"\n",
"def _update_b(_b):\n",
" global b\n",
" b = _b\n",
" update_signals(s_type, s_n0, h_type, h_n0)\n",
"\n",
"def _update_M(_M):\n",
" global M\n",
" M = _M\n",
" update_signals(s_type, s_n0, h_type, h_n0)\n",
"\n",
"# widgets for setting parameters b and M\n",
"w_b = interactive(_update_b, _b=widgets.FloatSlider(min=.1, max=1, value=.5, step=.1, description=r'$b$', style=ient_wdgtl_style))\n",
"w_M = interactive(_update_M, _M=widgets.FloatSlider(min=0, max=5, value=1, step=1, description=r'$M$', style=ient_wdgtl_style))\n",
"box = HBox([])\n",
"\n",
"fig0, axs0 = plt.subplots(1, 2, figsize=(8,2)); container_s = container_h = None\n",
"@widgets.interact(s_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $s(n)$:'),\n",
" s_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style), \n",
" h_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $h(n)$:'),\n",
" h_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style))\n",
"def update_signals(s_type, s_n0, h_type, h_n0):\n",
"@widgets.interact(_s_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $s(n)$:'),\n",
" _s_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style), \n",
" _h_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $h(n)$:'),\n",
" _h_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style))\n",
"def update_signals(_s_type, _s_n0, _h_type, _h_n0):\n",
" # set global variables\n",
" global s_type, s_n0, h_type, h_n0\n",
" s_type = _s_type; s_n0 = _s_n0; h_type = _h_type; h_n0 = _h_n0\n",
" \n",
" global s, h, gn, container_s, container_h # reused in second interactive plot\n",
" s = lambda m: signal_types[s_type]((m-s_n0));\n",
" h = lambda m: signal_types[h_type]((m-h_n0));\n",
" s = lambda m: signal_types[_s_type]((m-_s_n0)); # s(m-n0)\n",
" h = lambda m: signal_types[_h_type]((m-_h_n0)); # h(m-n0)\n",
" gn = convolution(s, h) # numerical convolution\n",
" \n",
" # update second plot if existing\n",
" try:\n",
" global n0\n",
" update_plot(n0)\n",
" except NameError:\n",
" pass\n",
" \n",
" # show widgets according to chosen s and h\n",
" if _s_type == 'Exponentialimpuls' and _h_type == 'Rechteck':\n",
" box.children = [w_b, w_M]\n",
" elif _s_type == 'Rechteck' and _h_type == 'Exponentialimpuls':\n",
" box.children = [w_M, w_b]\n",
" elif _s_type == 'Exponentialimpuls' or _h_type == 'Exponentialimpuls':\n",
" box.children = [w_b]\n",
" elif _s_type == 'Rechteck' or _h_type == 'Rechteck':\n",
" box.children = [w_M]\n",
" else:\n",
" box.children = []\n",
" \n",
" # display s and h plots\n",
" if container_s is None:\n",
" ax = axs0[0]; \n",
" container_s = ient_stem(ax, m, s(m), 'rwth')\n",
......@@ -99,15 +134,23 @@
" ax.set_xticks(np.arange(-10, 11, step=2))\n",
" ax.set_xlabel(r'$\\rightarrow n$'); ax.set_ylabel(r'$\\uparrow h(n)$')\n",
" ax.set_xlim(axs0[0].get_xlim()); ax.set_ylim(axs0[0].get_ylim()); ient_axis(ax); ient_grid(ax);\n",
"\n",
" else:\n",
" ient_stem_set_ydata(container_s, s(m))\n",
" ient_stem_set_ydata(container_h, h(m))"
" ient_stem_set_ydata(container_h, h(m))\n",
" \n",
"\n",
"display(box)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"fig, axs = plt.subplots(2, 1, figsize=(8,6),) # gridspec_kw = {'width_ratios':[3, 1]}\n",
......@@ -153,6 +196,13 @@
" ax.set_xticks(np.arange(-10, 11, step=2))\n",
" ient_grid(ax); ient_axis(ax);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
%% Cell type:code id: tags:
``` python
# Copyright 2019 Institut für Nachrichtentechnik, RWTH Aachen University
%matplotlib widget
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed, Layout
from ipywidgets import interact, interactive, fixed, Layout, HBox, VBox
from IPython.display import clear_output, display, HTML
from scipy import signal # convolution
from ient_nb.ient_plots import *
from ient_nb.ient_signals import *
```
%% Cell type:markdown id: tags:
# Demonstrator Diskrete Faltung
%% Cell type:markdown id: tags:
Im Folgenden wird die Faltung
$g(n)=s(n)\ast h(n)$
betrachtet. <br>
$s(n)$ und $h(n)$ können gewählt werden als:
* $\delta(n)$
* $\epsilon(n)$
* $\epsilon(n)\cdot\mathrm{b}^{n}$
* $rect(n) = \epsilon(n+M)-\epsilon(n-M-1)$
%% Cell type:code id: tags:
``` python
M = 1
b = 0.5
```
%% Cell type:code id: tags:
``` python
n = np.linspace(-10, 10, 21)
m = np.linspace(-40, 40, 81) # m Achse
s_type = h_type = ''; s_n0 = h_n0 = 0
M = 1
b = .5
n0 = -2
def convolution(s, h):
# Convolve s and h numerically
return signal.convolve(s(m), h(m), mode='same')
signal_types = {'Dirac-Impuls' : lambda n: np.where(n==0, 1, 0),
'Sprungfunktion' : unitstep,
'Exponentialimpuls' : lambda n: unitstep(n)*b**n,
'Rechteck' : lambda n: unitstep(n+M) - unitstep(n-M-1)
}
def _update_b(_b):
global b
b = _b
update_signals(s_type, s_n0, h_type, h_n0)
def _update_M(_M):
global M
M = _M
update_signals(s_type, s_n0, h_type, h_n0)
# widgets for setting parameters b and M
w_b = interactive(_update_b, _b=widgets.FloatSlider(min=.1, max=1, value=.5, step=.1, description=r'$b$', style=ient_wdgtl_style))
w_M = interactive(_update_M, _M=widgets.FloatSlider(min=0, max=5, value=1, step=1, description=r'$M$', style=ient_wdgtl_style))
box = HBox([])
fig0, axs0 = plt.subplots(1, 2, figsize=(8,2)); container_s = container_h = None
@widgets.interact(s_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $s(n)$:'),
s_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style),
h_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $h(n)$:'),
h_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style))
def update_signals(s_type, s_n0, h_type, h_n0):
@widgets.interact(_s_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $s(n)$:'),
_s_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style),
_h_type=widgets.Dropdown(options=list(signal_types.keys()), description=r'Wähle $h(n)$:'),
_h_n0=widgets.FloatSlider(min=-5, max=5, value=0, step=1, description=r'Verschiebung $n_0$', style=ient_wdgtl_style))
def update_signals(_s_type, _s_n0, _h_type, _h_n0):
# set global variables
global s_type, s_n0, h_type, h_n0
s_type = _s_type; s_n0 = _s_n0; h_type = _h_type; h_n0 = _h_n0
global s, h, gn, container_s, container_h # reused in second interactive plot
s = lambda m: signal_types[s_type]((m-s_n0));
h = lambda m: signal_types[h_type]((m-h_n0));
s = lambda m: signal_types[_s_type]((m-_s_n0)); # s(m-n0)
h = lambda m: signal_types[_h_type]((m-_h_n0)); # h(m-n0)
gn = convolution(s, h) # numerical convolution
# update second plot if existing
try:
global n0
update_plot(n0)
except NameError:
pass
# show widgets according to chosen s and h
if _s_type == 'Exponentialimpuls' and _h_type == 'Rechteck':
box.children = [w_b, w_M]
elif _s_type == 'Rechteck' and _h_type == 'Exponentialimpuls':
box.children = [w_M, w_b]
elif _s_type == 'Exponentialimpuls' or _h_type == 'Exponentialimpuls':
box.children = [w_b]
elif _s_type == 'Rechteck' or _h_type == 'Rechteck':
box.children = [w_M]
else:
box.children = []
# display s and h plots
if container_s is None:
ax = axs0[0];
container_s = ient_stem(ax, m, s(m), 'rwth')
ax.set_xticks(np.arange(-10, 11, step=2))
ax.set_xlabel(r'$\rightarrow n$'); ax.set_ylabel(r'$\uparrow s(n)$')
ax.set_xlim([-10.9, 10.9]); ax.set_ylim([-1.19, 1.19]); ient_axis(ax); ient_grid(ax);
ax = axs0[1];
container_h = ient_stem(ax, m, h(m), 'rwth')
ax.set_xticks(np.arange(-10, 11, step=2))
ax.set_xlabel(r'$\rightarrow n$'); ax.set_ylabel(r'$\uparrow h(n)$')
ax.set_xlim(axs0[0].get_xlim()); ax.set_ylim(axs0[0].get_ylim()); ient_axis(ax); ient_grid(ax);
else:
ient_stem_set_ydata(container_s, s(m))
ient_stem_set_ydata(container_h, h(m))
display(box)
```
%% Cell type:code id: tags:
``` python
fig, axs = plt.subplots(2, 1, figsize=(8,6),) # gridspec_kw = {'width_ratios':[3, 1]}
global n0
container_ss = container_hh = container_gg = None
@widgets.interact(n=widgets.FloatSlider(min=-10, max=10, value=n0, step=1, description='Verschiebung $n$', style=ient_wdgtl_style))
def update_plot(n):
global container_ss, container_hh, container_gg
global n0
n0 = n
n_ind = np.where(m>=n); n_ind = n_ind[0][0]; g_plot = gn.copy(); g_plot[n_ind+1:] = 0; # hide g(n') with n'>n
if container_gg is None:
ax = axs[1]
container_gg = ient_stem(ax, m, g_plot)
if container_ss is not None:
ient_stem_set_ydata(container_ss, s(m))
ient_stem_set_ydata(container_hh, h(n-m))
ient_stem_set_ydata(container_gg, g_plot)
ax = axs[0]
ax.texts[0].set_x(n); ax.lines[3].set_xdata([n,n]) # update labels
ax = axs[1]
ient_update_ylim(ax, gn, 0.19, 20);
else:
ax = axs[0];
container_ss = ient_stem(ax, m, s(m), 'grun', label=r'$s(m)$')
container_ss[0].set_markerfacecolor('none');
container_ss[0].set_markersize(8);
container_ss[0].set_markeredgewidth(2);
container_hh = ient_stem(ax, m, h(n-m), 'rwth', label=r'$h(n-m)$')
ient_annotate_xtick(ax, r'$n$', n, -0.1, 'rwth', 15); # mark n on m axis
ax.set_xlabel(r'$\rightarrow m$');
ax.set_xlim([-10.2,10.2]); ient_update_ylim(ax, np.concatenate((h(m), s(m))), 0.19, 5);
ax.set_xticks(np.arange(-10, 11, step=2))
ax.legend(); ient_grid(ax); ient_axis(ax);
ax = axs[1]
ax.set_xlabel(r'$\rightarrow n$'); ax.set_ylabel(r'$\uparrow g(n)=s(n)\ast h(n)$');
ax.set_xlim(axs[0].get_xlim()); ient_update_ylim(ax, gn, 0.19, 20);
ax.set_xticks(np.arange(-10, 11, step=2))
ient_grid(ax); ient_axis(ax);
```
%% Cell type:code id: tags:
``` python
```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment