Commit 4854c28c authored by Hafiz Emin Kosar's avatar Hafiz Emin Kosar
Browse files

refactoring

parent 776e1365
%% Cell type:code id: tags:
``` python
# Copyright 2019 Institut für Nachrichtentechnik, RWTH Aachen University
%matplotlib notebook
from ipywidgets import interact, interactive, fixed, HBox, VBox
import ipywidgets as widgets
from IPython.display import clear_output, display, HTML
from IPython.display import Markdown as md
from ient_nb.ient_plots import *
from ient_nb.ient_signals import *
from ient_nb.ient_transforms import *
from src.laplace.laplace_plot import pzPoint, pzPlot
from src.funcs import calc_Hf, calc_ht
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
t = np.linspace(-6,6,1024)
f = np.linspace(-6,6,1024)
```
%% Cell type:markdown id: tags:
<div>
<img src="ient_nb/figures/rwth_ient_logo@2x.png" style="float: right;height: 5em;">
</div>
# Laplace-Transformation
%% Cell type:markdown id: tags:
Beispiele
$s_1(t) = \mathrm{e}^{-bt}\cdot\epsilon(t)$
transformiert sich zu
$S_1(p) = \frac{1}{b+p}$ mit Konvergenzbereich $\mathrm{Re}\{p\}>\mathrm{Re}\{-b\}$
%% Cell type:code id: tags:
``` python
b = 2
pp = np.array([-b]); pz = np.array([])
roc = np.array([-b, np.inf])
fig,axs = plt.subplots(1,2)
# Laplace-Bereich
ax = axs[0]
ient_plot_lroc(ax, roc)
ax.plot(np.real(pp), np.imag(pp), **ient_style_poles); ax.plot(np.real(pp), -np.imag(pp), **ient_style_poles)
ax.set_xlabel(r'$\rightarrow \mathrm{Re}$'); ax.set_ylabel(r'$\uparrow \mathrm{Im}$');
ax.set_xlim(-5, 5); ax.set_ylim(-5, 5); ient_grid(ax); ient_axis(ax)
# Zeitbereich
ax = axs[1]
s1, _ = calc_ht(t, 1, pp, pz, [1], [], roc)
s1, _, _ = ient_ilaplace_ht(t, 1, pp, pz, [1], [], roc)
ax.plot(t, np.real(s1), **ient_style_graph);
ax.set_xlabel(r'$\rightarrow t$'); ax.set_ylabel(r'$\uparrow s_1(t)$'); ient_grid(ax); ient_axis(ax)
```
%% Cell type:markdown id: tags:
Konvergenzbereich umschließt imaginäre Achse, also existiert auch die Fouriertransformierte $S_1(f)$
%% Cell type:code id: tags:
``` python
# Frequenzbereich
S1f = 10**(calc_Hf(f, 1, pp, pz, [1], [])/20)
S1f = ient_ilaplace_Hf(f, 1, pp, pz, [1], [], dB=False)
fig, ax = plt.subplots(1,1)
ax.plot(f, S1f, **ient_style_graph);
ax.set_xlabel(r'$\rightarrow f$'); ax.set_ylabel(r'$\uparrow S_1(f)$');
ax.set_xlim([-5.5,5.5]); ax.set_ylim([-0.1,0.55]); ient_grid(ax); ient_axis(ax)
```
%% Cell type:markdown id: tags:
$s_2(t) = -\mathrm{e}^{-bt}\cdot\epsilon(-t)$
transformiert sich zu
$S_2(p) = \frac{1}{b+p}$ mit Konvergenzbereich $\mathrm{Re}\{p\}<\mathrm{Re}\{-b\}$
%% Cell type:code id: tags:
``` python
roc = np.array([-np.inf, -b])
fig,axs = plt.subplots(1,2)
# Laplace-Bereich
ax = axs[0]
ient_plot_lroc(ax, roc); ient_annotate_order(ax, pp, [1]);
ax.plot(np.real(pp), np.imag(pp), **ient_style_poles); ax.plot(np.real(pp), -np.imag(pp), **ient_style_poles)
ax.set_xlabel(r'$\rightarrow \mathrm{Re}$'); ax.set_ylabel(r'$\uparrow \mathrm{Im}$');
ax.set_xlim(-5, 5); ax.set_ylim(-5, 5); ient_axis(ax); ient_grid(ax)
# Zeitbereich
ax = axs[1]
s2, _ = calc_ht(t, 1, pp, pz, [1], [], roc)
s2, _, _ = ient_ilaplace_ht(t, 1, pp, pz, [1], [], roc)
ax.plot(t, np.real(s2), **ient_style_graph);
ax.set_ylim([-45,5]); ax.grid();
ax.set_xlabel(r'$\rightarrow t$'); ax.set_ylabel(r'$\uparrow s_2(t)$'); ient_axis(ax)
```
%% Cell type:markdown id: tags:
## Interaktive Demo
%% Cell type:markdown id: tags:
Laplace-Übertragungsfunktion
$$
H(p) = H_0 \frac{
\prod\limits_{q=1}^Q \left( p-p_{\mathrm{N},q} \right)
}{
\prod\limits_{r=1}^R \left( p-p_{\mathrm{P},r} \right)
}
$$
mit $Q$ Nullstellen $p_{\mathrm{N},q}$ und $R$ Polstellen $p_{\mathrm{P},r}$.
%% Cell type:code id: tags:
``` python
pzp = pzPlot()
filter_type = interactive(pzp.update_filter,filtr=widgets.Dropdown(options=list(pzp.filter_types.keys()), value="Sprungfunktion", description='Filter'))
action_type = interactive(pzp.update_action,action=widgets.Dropdown(options=list(pzp.action_types.keys()),value="Hinzufügen", description='Modus'))
point_type = interactive(pzp.update_mode,mode=widgets.Dropdown(options=list(pzp.mode_types.keys()), value="Polstelle", description='Typ'))
amp_type = interactive(pzp.update_amp, H0=widgets.IntSlider(min=1,max=10,step=1,value=1), description="H0")
left_box = VBox([filter_type, amp_type])
right_box = VBox([action_type, point_type])
HBox([left_box, right_box])
```
%% Cell type:markdown id: tags:
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).
Please attribute the work as follows:
*Emin Kosar, Christian Rohlfing, Übungsbeispiele zur Vorlesung "Grundgebiete der Elektrotechnik 3 - Signale und Systeme"*, gehalten von Jens-Rainer Ohm, 2019, Institut für Nachrichtentechnik, RWTH Aachen University.
......
import bisect
import src.funcs as func
#from src.gdet3plot import plot
from ient_nb.ient_plots import *
from ient_nb.ient_transforms import *
class pzPoint():
......@@ -11,9 +11,6 @@ class pzPoint():
h_order = None
def __init__(self, p=0 + 0j, order=1):
"""
"""
# super().__init__(*args, **kwargs)
self.p = p
self.order = order
......@@ -43,6 +40,8 @@ class pzPlot():
H = None
Hlog = None
dirac_in_plot = False
def __init__(self, pp=np.array([0]), pz=np.array([]), ord_p=np.array([1]), ord_z=np.array([])):
self.H0 = 1
......@@ -196,9 +195,9 @@ class pzPlot():
plotstyle = ient_style_zeros
# Plot points
pPoint.h_point, = self.ax.plot(p.real, p.imag, **plotstyle, mfc='none')
pPoint.h_point, = self.ax.plot(p.real, p.imag, **plotstyle)
if np.abs(p.imag) > 0: # plot complex conjugated poles/zeroes
pPoint.h_point_conj, = self.ax.plot(p.real, -p.imag, **plotstyle, mfc='none')
pPoint.h_point_conj, = self.ax.plot(p.real, -p.imag, **plotstyle)
elif self.action == 'del':
return
......@@ -293,7 +292,7 @@ class pzPlot():
self.H0_txt.remove()
except AttributeError:
pass
self.H0_txt = self.ax.text(4.5, 5, r'$H_0$: ' + str(self.H0), fontsize=12, bbox=ient_wbbox)
self.update_plot()
......@@ -318,12 +317,9 @@ class pzPlot():
# clear plot
def clear_all_points():
for point in self.pp:
point.clear()
for point in self.pz:
point.clear()
self.pp = np.array([])
self.pz = np.array([])
list(map(lambda p: p.clear(), list(self.pp) + list(self.pz)));
self.pp = self.pz = np.array([])
# add default filter poles and zeroes to plot
def def_points():
......@@ -385,12 +381,12 @@ class pzPlot():
# TODO - butterworth? -> verändere H0 nicht
roc = self.roc['sigma']
s_t, signal_type = func.calc_ht(self.t, self.H0, poles, zeroes, poles_order, zeroes_order, roc)
s_t, td, sd = ient_ilaplace_ht(self.t, self.H0, poles, zeroes, poles_order, zeroes_order, roc)
if not (roc[0] <= 0 <= roc[1]):
S_f = np.full(self.f.shape, np.NaN)
else:
S_f = func.calc_Hf(self.f, self.H0, poles, zeroes, poles_order, zeroes_order)
S_f = ient_ilaplace_Hf(self.f, self.H0, poles, zeroes, poles_order, zeroes_order, dB=True)
# process signals
# delete existing dirac
......@@ -403,20 +399,14 @@ class pzPlot():
if not np.isnan(s_t[0]): # if pbz was successful
self.pbzErrortxt.set_visible(False)
if signal_type == '': # if signal does not contain any diracs -> set y-lims according to min, max in array
s_t_extr = [np.amin(np.real(s_t)), np.amax(np.real(s_t))]
self.handles['axh'].set_ylim(s_t_extr[0] + s_t_extr[0] / 10, s_t_extr[1] + s_t_extr[1] / 10)
elif signal_type == 'dirac': # if signal contains diracs -> plot dirac and set y-data
# just one dirac is plotted for now
ind, value = func.findDirac(s_t)
x_value = self.t[ind[0]]
y_value = value[0]
self.arrow = self.handles['axh'].arrow(x_value, 0, 0, y_value, width=0.1, head_width=0.5,
head_length=2 * y_value/10, fc="k", ec="k", length_includes_head=False)
self.arrow_txt = self.handles['axh'].text(x_value, y_value, "(" + str(y_value) + ")")
s_t[ind[0]] = np.NaN
self.handles['axh'].set_ylim(np.nanmin(np.append(s_t, value[0])) - 0.2, np.nanmax(np.append(s_t, value[0])) + 0.2)
if len(sd): # if signal contains any diracs
if self.dirac_in_plot:
ient_dirac_set_data((self.cp, self.cn), td, sd)
else:
(self.cp, self.cn) = ient_plot_dirac(self.handles['axh'], td, sd, 'rwth')
self.dirac_in_plot = True
elif not len(sd) and self.dirac_in_plot:
ient_dirac_set_data((self.cp, self.cn), [0], [0])
else: # if pbz was not successful
self.handles['axh'].set_ylim(-1, 1)
self.pbzErrortxt.set_visible(True)
......@@ -425,4 +415,4 @@ class pzPlot():
self.handles['lineH'].set_ydata(S_f)
self.handles['axH'].relim()
self.handles['axH'].autoscale_view()
self.handles['axH'].xaxis.set_label_coords(1, 0)
\ No newline at end of file
self.handles['axH'].xaxis.set_label_coords(1, 0)
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