From 4d2020b052f3c5e59078d70d19efe1c0ae4c9fb2 Mon Sep 17 00:00:00 2001
From: Adam Friedrich Schrey <adam.schrey@gmx.de>
Date: Sun, 29 Aug 2021 22:41:14 +0200
Subject: [PATCH] Add systheo2functions.py

---
 notebooks/systheo2functions.py | 92 ++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 notebooks/systheo2functions.py

diff --git a/notebooks/systheo2functions.py b/notebooks/systheo2functions.py
new file mode 100644
index 0000000..a3944e1
--- /dev/null
+++ b/notebooks/systheo2functions.py
@@ -0,0 +1,92 @@
+import numpy as np
+import control
+from scipy import signal
+from scipy import linalg
+import matplotlib.pyplot as plt
+inv = np.linalg.inv
+pi = np.pi
+sin = np.sin
+cos = np.cos
+sqrt = np.sqrt
+exp = np.exp
+atan2 = np.arctan2
+log10 = np.log10
+
+s = control.TransferFunction.s
+
+def print_det(M):
+    print("Determinante: "+str(np.around(np.linalg.det(M))))
+
+def print_rank(M):
+    print("Rang: "+str(np.linalg.matrix_rank(M)))
+    
+def print_eig(M):
+    print("Eigenwerte:")
+    w,v = np.linalg.eig(M)
+    for i in w:
+        print(np.around(i))
+        
+def matmul_loop(mats):
+    temp = mats[0]
+    for i in range(1,len(mats)):
+        temp = np.matmul(temp,mats[i])
+    return temp
+
+#Benutze eigene Funktion statt control.bode(), da control.bode() oft unerwünscht arrays ausgibt:
+def plt_bode(G):
+    print("Bode-Diagramm:")
+    temp = signal.lti(G.num[0][0],G.den[0][0])
+    w, mag, phase = temp.bode()
+    plt.plot(w,mag,'blue')
+    plt.xscale("log")
+    plt.grid()
+    plt.legend(['Amplitude mit 20*log10(...)'])
+    plt.show()
+
+    plt.plot(w,phase,'orange')
+    plt.xscale("log")
+    plt.grid()
+    plt.legend(['Phase in Grad'])
+    plt.show()
+
+#Benutze eigene Funktion um Skalierung an die Ausgabe in Matlab anzupassen:
+def plt_nyquist(G,scale_x,scale_y,x0,x1):
+    plt.axhline(y=0, color='lightgrey')
+    plt.axvline(x=0, color='lightgrey')
+    
+    control.nyquist(G)
+    plt.grid(True)
+    plt.title('Nyquist-Diagramm von:\n'+str(G))
+    
+    xmin, xmax = plt.xlim()
+    ymin, ymax = plt.ylim()
+    plt.xlim(xmin * scale_x + x0, xmax * scale_x  +x1)
+    plt.ylim(ymin * scale_y, ymax * scale_y)
+    
+    plt.xlabel('Re(s)')
+    plt.ylabel('Im(s)')
+    plt.show()
+
+#Benutze eigene Funktion um aussagekräftige Ausgabe zu erhalten:
+def margin(G):
+    gm, pm, wg, wp = control.margin(G)
+    if str(pm) == "inf":
+        print("keinen Phasenrand gefunden")
+    else:
+        print("Phasenrand: "+str(np.around(pm,3))+"° bei Frequenz: "+str(np.around(wp,3))+"rad/s")
+    if str(gm) == "inf":
+        print("keine Amplitudenreserve gefunden")
+    else:
+        print("Amplitudenreserve: "+str(np.around(gm,3))+" bei Frequenz: "+str(np.around(wg,3))+"rad/s")
+
+import sys
+sys.path.append('Pysim')
+from Schema import Schema
+from Block import (Constant, StateSpace, Gain, StepSource,
+Division, Product, Saturation, PI,PID, Sum, SquareSignal,
+SinusoidalSignalSource, Integrator, Not, TransferFunction)
+from DTBlock import ZOH, DTStateSpace, FIR, DTIntegral, DTDelay, IIR, DTTransferFunction
+
+import ipywidgets as widgets
+from ipywidgets import interact
+
-- 
GitLab