-
Notifications
You must be signed in to change notification settings - Fork 1
/
tfSim.py
52 lines (37 loc) · 1.26 KB
/
tfSim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from scipy import signal
from scipy.signal import tf2ss, lsim, StateSpace
import matplotlib.pyplot as plt
import numpy as np
def tfSim(inputType, mag, wn, zeta, init_xdot, init_x, step_increment, endtime):
num = [0, 0, wn**2]
den = [1, 2*zeta*wn, wn**2]
timesteps = np.arange(0, endtime, step_increment)
if inputType == "sine":
u = mag*np.sin(2*np.pi*timesteps)
elif inputType == "step":
u = mag*np.ones(timesteps.size)
else:
raise ValueException("unrecognized input type")
[A,B,C,D] = tf2ss(num,den)
sys = StateSpace(A, B, C, D)
print("C: {} ; shape: {}".format(C,C.shape))
init_xdot = init_xdot / C[0][1]
init_x = init_x / C[0][1]
init_cond = np.array([init_xdot,init_x])
print(init_cond)
print("initial conditions shape: {}".format(init_cond.shape))
y = lsim(sys, u, timesteps, init_cond )
return y
if __name__ == "__main__":
mag = 20
w_n = 2*np.pi*3
zeta = 0.00
init_x = 20
init_xdot = 1
step_increment = 0.01
endtime = 10
y = tfSim("step", mag, w_n, zeta, init_xdot, init_x, step_increment, endtime)
plt.plot(y[0], y[1])
plt.plot((y[0] - ((y[0][1]-y[0][0])/2))[1:] , np.diff(y[1])/np.diff(y[0]) )
plt.grid(True)
plt.show()