Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • Adam.Schrey/lecture-tutorials
1 result
Select Git revision
Loading items
Show changes
Showing
with 2888 additions and 63 deletions
__pycache__
.vscode
.ipynb_checkpoints
\ No newline at end of file
......@@ -2,6 +2,7 @@ import numpy
sin = numpy.sin
from random import gauss
#########################################################################
#### Abstract class for continuous time block in a block diagram ########
#########################################################################
......@@ -55,7 +56,6 @@ class Block(object):
self.x[i] = self.x[i]+(dx1[i]+dx2[i])/2*dt
#########################################################################
################### Step function as a signal source ####################
#########################################################################
......@@ -76,6 +76,7 @@ class StepSource(Block):
if t>=self.ts:
self.y[0] = self.endv
#########################################################################
####################### Sinusoidal signal source ########################
#########################################################################
......@@ -117,6 +118,7 @@ class UnknownWaveSource(Block):
opt = (self.om*t)+self.phi
self.y[0] = self.Am*(sin(opt)-0.3*(sin(opt))**2+0.2*(sin(opt-1))**2)
#########################################################################
################################## Noise ################################
#########################################################################
......@@ -138,6 +140,7 @@ class Noise(Block):
noise = numpy.array([gauss(0,self.sigma)])
self.y = self.u + noise
#########################################################################
######################### Square signal source ##########################
#########################################################################
......@@ -164,7 +167,6 @@ class SquareSignal(Block):
self.y[0] = self.hi;
#########################################################################
##################### Constant as a signal source #######################
#########################################################################
......@@ -199,7 +201,6 @@ class Division(Block):
self.y[0] = self.u[0]/self.u[1]
#########################################################################
########################## Product operation ###########################
#########################################################################
......@@ -244,6 +245,7 @@ class Sum(Block):
def Step(self,t,dt):
self.y[0] = self.u[0]*self.sign1+self.u[1]*self.sign2
#########################################################################
############################## Saturation ##############################
#########################################################################
......@@ -262,7 +264,6 @@ class Saturation(Block):
self.u = numpy.zeros((self.ninput))
self.y = numpy.zeros((self.noutput))
def Step(self,t,dt):
self.y[0] = self.u[0]
if self.y[0] > self.maxout:
......@@ -391,7 +392,6 @@ class PID(Block):
self.Kd = value[2]
#########################################################################
############################# Integrator ################################
#########################################################################
......@@ -473,7 +473,6 @@ class StateSpace(Block):
self.x[i] = xo[i]
self.xo[i] = xo[i]
def Step(self,t,dt):
for i in range(0,self.noutput):
self.y[i] = 0
......@@ -566,5 +565,3 @@ class TransferFunction(Block):
for j in range (0,self.nstate):
dxdt[i] = dxdt[i] + self.A[i,j]*x[j]
return dxdt
# -*- coding: utf-8 -*-
import numpy
class LinProb(object):
def __init__(self, nodeNum):
self.G = numpy.zeros((nodeNum,nodeNum), dtype=numpy.complex)
......@@ -20,6 +20,7 @@ class LinProb(object):
print(self.G)
print(self.b)
class Circuit(object):
def __init__(self, nnode):
self.n = nnode
......@@ -72,8 +73,3 @@ class Circuit(object):
k = k+1
return self.vout
\ No newline at end of file
import numpy
try:
from .Block import Block
except:
from Block import Block
inv = numpy.linalg.inv
dot = numpy.dot
#########################################################################
#### Abstract class for discrete time block in a block diagram ########
#########################################################################
......@@ -33,6 +37,7 @@ class DTBlock(Block):
def Reset(self):
self.lastt = -10;
#########################################################################
################################ FIR ####################################
#########################################################################
......@@ -68,7 +73,6 @@ class FIR(DTBlock):
self.lastt = -10
#########################################################################
#########################Discrete Integral###############################
#########################################################################
......@@ -97,7 +101,6 @@ class DTIntegral(DTBlock):
self.x = self.xo
#########################################################################
######################### ZOH #################################
#########################################################################
......@@ -120,8 +123,6 @@ class ZOH(DTBlock):
self.y[0] = self.u[0]
#########################################################################
####################### Delay #################################
#########################################################################
......@@ -153,9 +154,6 @@ class DTDelay(DTBlock):
self.oldv = self.init
#########################################################################
################################ IIR ####################################
#########################################################################
......@@ -195,15 +193,12 @@ class IIR(DTBlock):
for i in range (0,self.pd):
self.y[0] = self.y[0]-self.b[i]*self.bufd[i]
def Reset(self):
self.bufn = zeros(self.pn,1)
self.bufd = zeros(self.pd,1)
self.lastt = -10
#########################################################################
############### Discrete Time Transfer Function ########################
#########################################################################
......@@ -260,13 +255,11 @@ class DTTransferFunction(DTBlock):
for i in range (0,self.nstate):
self.x[i] = xn[i]
def Reset(self):
for i in range(0,self.nstate):
self.x[i] = 0
#########################################################################
########################### DT State Space #############################
#########################################################################
......@@ -313,15 +306,11 @@ class DTStateSpace(DTBlock):
print("WARNING: Can not handle self.D with dimension "+str(self.C.ndim))
self.x = numpy.matmul(self.B,self.u.T) + numpy.matmul(self.A,self.x)
def Reset(self):
for i in range(0,self.nstate):
self.x[i] = 0
#########################################################################
########################### Kalman-Filter ##############################
#########################################################################
......@@ -347,9 +336,7 @@ class KalmanFilter(DTBlock):
self.y = numpy.zeros((self.noutput))
self.lastt = -10
def updatediscrete(self):
uu = numpy.zeros((self.nstate))
m = numpy.zeros((self.nstate))
if (self.nin != 0):
......
# -*- coding: utf-8 -*-
import numpy
class RCComponent(object):
def __init__(self, p, n):
self.Pos = p
......@@ -22,6 +21,7 @@ class RCComponent(object):
def postStep(self,vsol,dt):
return vsol
class Resistance(RCComponent):
def __init__(self,p,n,par):
super(Resistance, self).__init__(p,n)
......@@ -136,6 +136,7 @@ class Inductor(RCComponent):
self.Il = self.Gl*self.Vl+self.Bl;
class IdealACVoltageSource(RCComponent):
def __init__(self,p,n,V,omega,phase):
super(IdealACVoltageSource, self).__init__(p,n)
......@@ -168,4 +169,3 @@ class IdealACVoltageSource(RCComponent):
def step(self,P,dt,t):
P.b[self.possource,0] = self.Vs * numpy.sin(self.om*t+self.ph)
return P
\ No newline at end of file
# -*- coding: utf-8 -*-
import numpy as np
......
This diff is collapsed.
This diff is collapsed.
%% Cell type:markdown id:04105e65-6705-4c92-a009-48c54bd38ad4 tags:
# <span style='color:OrangeRed'>V1 DAS ABTASTTHEOREM </span>
%% Cell type:markdown id:ffe7d73f-7986-44b6-a9fd-a678b9aaa182 tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
Hier wird analysiert, wie man die geeignete Abtastfrequenz für ein bestimmtes System auswählt. Am Ende überprüfen wir das Ergebnis mit einer einfachen Simulation.
%% Cell type:code id:3a336fb9-2f73-44ce-ab67-91b96ccfc1c1 tags:
``` octave
% Necessary to use control toolbox
pkg load control
clear all
% Set the Octsim Engine to run the simulation
addpath('../Octsim');
```
%% Cell type:markdown id:53866cc1-a0ea-4d77-8d9c-c8345704d874 tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
Es ist das folgende System in Form einer Übertragungsfunktion gegeben:
%% Cell type:code id:a0b9d775-f176-483e-aaca-43da284aa2e4 tags:
``` octave
num = [100]
den = [1 10 100]
G = tf(num,den)
```
%% Output
num = 100
den =
1 10 100
Transfer function 'G' from input 'u1' to output ...
100
y1: ----------------
s^2 + 10 s + 100
Continuous-time model.
%% Cell type:markdown id:1cb98bc6-138b-45bc-9178-74f8493d4171 tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
Berechnet man die Nullstellen des Nenners, erhält man die Pole des Systems:
%% Cell type:code id:03459a21-1072-416b-a04b-5de7b9a30813 tags:
``` octave
p = roots(den)
```
%% Output
p =
-5.0000 + 8.6603i
-5.0000 - 8.6603i
%% Cell type:markdown id:071800da-0e16-4189-bb65-5746c0415165 tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
Aus den Polen berechnen wir die Zeitkonstanten:
%% Cell type:code id:5adb101b-4286-428a-a4bd-eeb57e860e9c tags:
``` octave
tau = -1./real(p)
```
%% Output
tau =
0.100000 0.100000
%% Cell type:markdown id:eae75561-2795-400d-802d-f649281ff316 tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
und aus den Zeitkonstanten die Eigenfrequenzen:
%% Cell type:code id:acf57137-23eb-4be3-87ef-eb9d08076f07 tags:
``` octave
om = 2*pi./tau
```
%% Output
om =
62.832 62.832
%% Cell type:markdown id:ab36a55a-6239-4214-9d36-85f4d111c61e tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
Die minimale Abtastfrequenz ist durch das Zweifache der maximalen Eigenfrequenz des Systems gegeben.
%% Cell type:code id:c4c1fce4-7677-40d3-813e-5a5e4758f00f tags:
``` octave
minomt = 2*max(om)
```
%% Output
minomt = 125.66
%% Cell type:markdown id:c5fb33ea-47d7-49bf-a8ce-f1d81917a1d0 tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
Wir wählen dann einen Wert, der größer ist als das Minimum durch den Koeffizienten K.
%% Cell type:code id:14c39a24-fc50-4924-8075-84125990776f tags:
``` octave
K = 2
omt = K*minomt
```
%% Output
K = 2
omt = 251.33
%% Cell type:markdown id:25ea2215-45ca-4980-a862-372d0eccf237 tags:
<div style="font-family: 'times'; font-size: 13pt; text-align: justify">
Schließlich testen wir die Wahl der Abtastzeit, indem wir die Sprungantwort simulieren und den Ausgang abtasten.
%% Cell type:code id:578daff8-f4fd-4001-bb09-aea89d809eec tags:
``` octave
addpath("../Octsim");
tini = 0 # Start time
tfinal = 3 # End time
dt = 0.001 # Time Step
nflows = 3 #Number of data flows in the schematic, Zahlenwert entspricht nicht dem in Matlab (hier um 1 größer)
Ts = 2*pi/omt # Sampling time for discrete time
c1{1} = StepSource(1,0,1,0.1); #StepSource(self,out,startv,endv,ts)
c1{2} = TransferFunction(1,2,num,den); #TransferFunction(self,inp,out,num,den)
c1{3} = DTTransferFunction(2,3,1,1,Ts); #DTTransferFunction(self,inp,out,num,den,Ts)
% Instance of the simulation schematic
sc1 = Schema(tini,tfinal,dt,nflows);
sc1.AddListComponents(c1);
% Run the schematic and plot
out1 = sc1.Run([1 2 3]);
plot(out1(1,:),out1(2,:),out1(1,:),out1(3,:),out1(1,:),out1(4,:));
```
%% Output
tini = 0
tfinal = 3
dt = 0.0010000
nflows = 3
Ts = 0.025000
%% Cell type:code id:9e1a668f-07ac-442a-bb62-950dacd49832 tags:
``` octave
```
%% Cell type:code id:65f46b62-e607-456a-80b6-42f452d0abfc tags:
``` octave
```
This diff is collapsed.