Skip to content

Commit 0214001

Browse files
committed
fix iv; add example 3
1 parent 5b8ee85 commit 0214001

File tree

11 files changed

+505
-87
lines changed

11 files changed

+505
-87
lines changed

examples/example1.png

50.1 KB
Loading

examples/1_example.py renamed to examples/example1.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919
import scipy.signal as scipysig
2020
from vrft import *
2121

22-
def generateNoise(t):
23-
omega = 2*np.pi*100
24-
xi = 0.9
25-
dt = t[1] - t[0]
26-
noise = np.random.normal(0,0.1,t.size)
27-
tf = scipysig.TransferFunction([10*omega**2], [1, 2*xi*omega, omega**2])
28-
# Second order system
29-
_, yn, _ = scipysig.lsim(tf, noise, t)
30-
return yn
22+
# Example 1
23+
# ------------
24+
# In this example we see how to apply VRFT to a simple SISO model
25+
# without any measurement noise.
26+
# Input data is generated using a square signal
27+
#
3128

3229
#Generate time and u(t) signals
3330
t_start = 0
@@ -43,8 +40,7 @@ def generateNoise(t):
4340
den = [1, -0.9]
4441
sys = ExtendedTF(num, den, dt=t_step)
4542
t, y = scipysig.dlsim(sys, u, t)
46-
y = y[:, 0]
47-
#y += generateNoise(t)
43+
y = y.flatten()
4844
data = iddata(y, u, t_step, [0])
4945

5046

@@ -78,7 +74,7 @@ def generateNoise(t):
7874
yr = np.array(yr).flatten()
7975
ys = np.array(ys).flatten()
8076
yc = np.array(yc).flatten()
81-
fig, ax = plt.subplots(4, sharex=True)
77+
fig, ax = plt.subplots(4, sharex=True, figsize=(12,8), dpi= 100, facecolor='w', edgecolor='k')
8278
ax[0].plot(t, yr,label='Ref System')
8379
ax[0].plot(t, yc, label='CL System')
8480
ax[0].set_title('Systems response')
File renamed without changes.

examples/2_example.py renamed to examples/example2.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@
1111
# along with PythonVRFT. If not, see <http://www.gnu.org/licenses/>.
1212
#
1313
# Code author: [Alessio Russo - alessior@kth.se]
14-
# Last update: 07th January 2020, by alessior@kth.se
14+
# Last update: 08th January 2020, by alessior@kth.se
1515
#
1616

1717
import numpy as np
1818
import matplotlib.pyplot as plt
1919
import scipy.signal as scipysig
2020
from vrft import *
2121

22+
# Example 2
23+
# ------------
24+
# In this example we see how to apply VRFT to a simple SISO model
25+
# with colored measurement noise (no instrumental variables)
26+
# Input data is generated using random normal noise
27+
#
28+
2229
def generateNoise(t):
30+
# Generate colored noise
2331
omega = 2*np.pi*100
2432
xi = 0.9
2533
dt = t[1] - t[0]
@@ -34,17 +42,14 @@ def generateNoise(t):
3442
t_end = 10
3543
t_step = 1e-2
3644
t = np.arange(t_start, t_end, t_step)
37-
u = np.ones(len(t))
38-
u[200:400] = np.zeros(200)
39-
u[600:800] = np.zeros(200)
45+
u = np.random.normal(size=t.size)
4046

4147
#Experiment
4248
num = [0.5]
4349
den = [1, -0.9]
4450
sys = ExtendedTF(num, den, dt=t_step)
4551
t, y = scipysig.dlsim(sys, u, t)
46-
y = y.flatten() + 0.5 * np.random.normal(size = t.size)
47-
#y += generateNoise(t)
52+
y = y.flatten() + generateNoise(t)
4853
data = iddata(y, u, t_step, [0])
4954

5055

@@ -57,9 +62,8 @@ def generateNoise(t):
5762

5863
#Experiment filter
5964
L = refModel * (1 - refModel)
60-
6165
#VRFT
62-
theta, r, loss, C = compute_vrft(data, refModel, base, L, iv=True)
66+
theta, r, loss, C = compute_vrft(data, refModel, base, L)
6367

6468
#Obtained controller
6569
print("Controller: {}".format(C))
@@ -79,7 +83,7 @@ def generateNoise(t):
7983
yr = np.array(yr).flatten()
8084
ys = np.array(ys).flatten()
8185
yc = np.array(yc).flatten()
82-
fig, ax = plt.subplots(4, sharex=True)
86+
fig, ax = plt.subplots(4, sharex=True, figsize=(12,8), dpi= 100, facecolor='w', edgecolor='k')
8387
ax[0].plot(t, yr,label='Ref System')
8488
ax[0].plot(t, yc, label='CL System')
8589
ax[0].set_title('Systems response')

examples/example3.png

122 KB
Loading

examples/example3.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright [2020] [Alessio Russo - alessior@kth.se]
2+
# This file is part of PythonVRFT.
3+
# PythonVRFT is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, version 3 of the License.
6+
# PythonVRFT is distributed in the hope that it will be useful,
7+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9+
# GNU General Public License for more details.
10+
# You should have received a copy of the GNU General Public License
11+
# along with PythonVRFT. If not, see <http://www.gnu.org/licenses/>.
12+
#
13+
# Code author: [Alessio Russo - alessior@kth.se]
14+
# Last update: 08th January 2020, by alessior@kth.se
15+
#
16+
17+
import numpy as np
18+
import matplotlib.pyplot as plt
19+
import scipy.signal as scipysig
20+
from vrft import *
21+
22+
# Example 3
23+
# ------------
24+
# In this example we see how to apply VRFT to a simple SISO model
25+
# with measurement noise using instrumental variables
26+
# Input data is generated using random normal noise
27+
#
28+
29+
#Generate time and u(t) signals
30+
t_start = 0
31+
t_end = 10
32+
dt = 1e-2
33+
t = np.array([i * dt for i in range(int(t_end/dt ))])
34+
35+
#Experiment
36+
num = [0.5]
37+
den = [1, -0.9]
38+
sys = ExtendedTF(num, den, dt=dt)
39+
40+
def generate_data(sys, u, t):
41+
t, y = scipysig.dlsim(sys, u, t)
42+
y = y.flatten() + 0.5 * np.random.normal(size = t.size)
43+
return iddata(y, u, dt, [0])
44+
45+
u = np.random.normal(size=t.size)
46+
data1 = generate_data(sys, u, t)
47+
data2 = generate_data(sys, u, t)
48+
data = [data1, data2]
49+
50+
#Reference Model
51+
refModel = ExtendedTF([0.6], [1, -0.4], dt=dt)
52+
53+
#PI Controller
54+
control = [ExtendedTF([1], [1, -1], dt=dt),
55+
ExtendedTF([1, 0], [1, -1], dt=dt)]
56+
57+
#Experiment filter
58+
prefilter = refModel * (1 - refModel)
59+
60+
# VRFT method with Instrumental variables
61+
theta_iv, r_iv, loss_iv, C_iv = compute_vrft(data, refModel, control, prefilter, iv=True)
62+
63+
# VRFT method without Instrumental variables
64+
theta_noiv, r_noiv, loss_noiv, C_noiv = compute_vrft(data1, refModel, control, prefilter, iv=False)
65+
66+
#Obtained controller
67+
print('------IV------')
68+
print("Loss: {}\nTheta: {}\nController: {}".format(loss_iv, theta_iv, C_iv))
69+
print('------No IV------')
70+
print("Loss: {}\nTheta: {}\nController: {}".format(loss_noiv, theta_noiv, C_noiv))
71+
72+
73+
# Closed loop system
74+
closed_loop_iv = (C_iv * sys).feedback()
75+
closed_loop_noiv = (C_noiv * sys).feedback()
76+
77+
t = t[:len(r_iv)]
78+
u = np.ones(len(t))
79+
80+
_, yr = scipysig.dlsim(refModel, u, t)
81+
_, yc_iv = scipysig.dlsim(closed_loop_iv, u, t)
82+
_, yc_noiv = scipysig.dlsim(closed_loop_noiv, u, t)
83+
_, ys = scipysig.dlsim(sys, u, t)
84+
85+
yr = yr.flatten()
86+
ys = ys.flatten()
87+
yc_noiv = yc_noiv.flatten()
88+
yc_iv = yc_iv.flatten()
89+
90+
fig, ax = plt.subplots(4, sharex=True, figsize=(12,8), dpi= 100, facecolor='w', edgecolor='k')
91+
ax[0].plot(t, yr,label='Ref System')
92+
ax[0].plot(t, yc_iv, label='CL System - IV')
93+
ax[0].plot(t, yc_noiv, label='CL System - No IV')
94+
ax[0].set_title('CL Systems response')
95+
ax[0].grid(True)
96+
ax[1].plot(t, ys, label='OL System')
97+
ax[1].set_title('OL Systems response')
98+
ax[1].grid(True)
99+
ax[2].plot(t, data1.y[:len(r_iv)])
100+
ax[2].grid(True)
101+
ax[2].set_title('Experiment data')
102+
ax[3].plot(t, r_iv)
103+
ax[3].grid(True)
104+
ax[3].set_title('Virtual Reference')
105+
106+
# Now add the legend with some customizations.
107+
legend = ax[0].legend(loc='lower right', shadow=True)
108+
109+
# The frame is matplotlib.patches.Rectangle instance surrounding the legend.
110+
frame = legend.get_frame()
111+
frame.set_facecolor('0.90')
112+
113+
114+
plt.show()

examples/notebook_example_1.ipynb

Lines changed: 23 additions & 16 deletions
Large diffs are not rendered by default.

examples/notebook_example_2.ipynb

Lines changed: 35 additions & 23 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)