Commit 8f15ac15 authored by Hafiz Emin Kosar's avatar Hafiz Emin Kosar
Browse files

- added notebook for discrete convolution

parent e117810a
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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 IPython.display import clear_output, display, HTML\n",
"\n",
"from scipy import signal # convolution\n",
"\n",
"from ient_nb.ient_plots import *\n",
"from ient_nb.ient_signals import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Demonstrator Diskrete Faltung"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Im Folgenden wird die Faltung\n",
"$g(n)=s(n)\\ast h(n)$\n",
"betrachtet. <br>\n",
"$s(n)$ und $h(n)$ können gewählt werden als:\n",
"* $\\delta(n)$\n",
"* $\\epsilon(n)$\n",
"* $\\epsilon(n)\\cdot\\mathrm{b}^{n}$\n",
"* $rect(n) = \\epsilon(n+M)-\\epsilon(n-M-1)$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"M = 1\n",
"b = 0.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = np.linspace(-10, 10, 21)\n",
"m = np.linspace(-40, 40, 81) # m Achse\n",
"\n",
"n0 = -2\n",
"\n",
"def convolution(s, h):\n",
" # Convolve s and h numerically\n",
" return signal.convolve(s(m), h(m), mode='same')\n",
"\n",
"signal_types = {'Dirac-Impuls' : lambda n: np.where(n==0, 1, 0),\n",
" 'Sprungfunktion' : unitstep,\n",
" 'Exponentialimpuls' : lambda n: unitstep(n)*b**n,\n",
" 'Rechteck' : lambda n: unitstep(n+M) - unitstep(n-M-1)\n",
" }\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",
" 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",
" gn = convolution(s, h) # numerical convolution\n",
" try:\n",
" global n0\n",
" update_plot(n0)\n",
" except NameError:\n",
" pass\n",
" \n",
" if container_s is None:\n",
" ax = axs0[0]; \n",
" container_s = ient_stem(ax, m, s(m), 'rwth')\n",
" ax.set_xticks(np.arange(-10, 11, step=2))\n",
" ax.set_xlabel(r'$\\rightarrow n$'); ax.set_ylabel(r'$\\uparrow s(n)$')\n",
" ax.set_xlim([-10.9, 10.9]); ax.set_ylim([-1.19, 1.19]); ient_axis(ax); ient_grid(ax);\n",
" \n",
" ax = axs0[1]; \n",
" container_h = ient_stem(ax, m, h(m), 'rwth')\n",
" 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",
" else:\n",
" ient_stem_set_ydata(container_s, s(m))\n",
" ient_stem_set_ydata(container_h, h(m))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, axs = plt.subplots(2, 1, figsize=(8,6),) # gridspec_kw = {'width_ratios':[3, 1]}\n",
"global n0\n",
"container_ss = container_hh = container_gg = None\n",
"@widgets.interact(n=widgets.FloatSlider(min=-10, max=10, value=n0, step=1, description='Verschiebung $n$', style=ient_wdgtl_style))\n",
"def update_plot(n):\n",
" global container_ss, container_hh, container_gg\n",
" global n0\n",
" n0 = n\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\n",
" if container_gg is None:\n",
" ax = axs[1]\n",
" container_gg = ient_stem(ax, m, g_plot)\n",
" \n",
" if container_ss is not None:\n",
" ient_stem_set_ydata(container_ss, s(m))\n",
" ient_stem_set_ydata(container_hh, h(n-m))\n",
" ient_stem_set_ydata(container_gg, g_plot)\n",
" ax = axs[0]\n",
" ax.texts[0].set_x(n); ax.lines[3].set_xdata([n,n]) # update labels\n",
" ax = axs[1]\n",
" ient_update_ylim(ax, gn, 0.19, 20);\n",
" \n",
" else:\n",
" ax = axs[0];\n",
" container_ss = ient_stem(ax, m, s(m), 'grun', label=r'$s(m)$')\n",
" container_ss[0].set_markerfacecolor('none'); \n",
" container_ss[0].set_markersize(8); \n",
" container_ss[0].set_markeredgewidth(2);\n",
" \n",
" container_hh = ient_stem(ax, m, h(n-m), 'rwth', label=r'$h(n-m)$')\n",
" \n",
" ient_annotate_xtick(ax, r'$n$', n, -0.1, 'rwth', 15); # mark n on m axis\n",
" ax.set_xlabel(r'$\\rightarrow m$');\n",
" ax.set_xlim([-10.2,10.2]); ient_update_ylim(ax, np.concatenate((h(m), s(m))), 0.19, 5);\n",
" ax.set_xticks(np.arange(-10, 11, step=2))\n",
" ax.legend(); ient_grid(ax); ient_axis(ax);\n",
" \n",
" ax = axs[1]\n",
" ax.set_xlabel(r'$\\rightarrow n$'); ax.set_ylabel(r'$\\uparrow g(n)=s(n)\\ast h(n)$'); \n",
" ax.set_xlim(axs[0].get_xlim()); ient_update_ylim(ax, gn, 0.19, 20);\n",
" ax.set_xticks(np.arange(-10, 11, step=2))\n",
" ient_grid(ax); ient_axis(ax);"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% 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 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
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)
}
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):
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));
gn = convolution(s, h) # numerical convolution
try:
global n0
update_plot(n0)
except NameError:
pass
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))
```
%% 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);
```
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