forked from rssalessio/PythonVRFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reference.py
76 lines (61 loc) · 2.28 KB
/
test_reference.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# test_reference.py - Unittest for virtual reference algorithm
#
# Code author: [Alessio Russo - alessior@kth.se]
# Last update: 10th January 2021, by alessior@kth.se
#
# Copyright (c) [2017-2021] Alessio Russo [alessior@kth.se]. All rights reserved.
# This file is part of PythonVRFT.
# PythonVRFT is free software: you can redistribute it and/or modify
# it under the terms of the MIT License. You should have received a copy of
# the MIT License along with PythonVRFT.
# If not, see <https://opensource.org/licenses/MIT>.
#
from unittest import TestCase
import numpy as np
import scipy.signal as scipysig
from vrft.iddata import *
from vrft.utils import *
from vrft.vrft_algo import *
class TestReference(TestCase):
def test_virtualReference(self):
# wrong system
with self.assertRaises(ValueError):
virtual_reference(1, 1, 0)
# cant be constant the system
with self.assertRaises(ValueError):
virtual_reference([1],[1], 0)
# cant be constant the system
with self.assertRaises(ValueError):
virtual_reference(np.array(2), np.array(3), 0)
# wrong data
with self.assertRaises(ValueError):
virtual_reference([1], [1, 1], 0)
t_start = 0
t_end = 10
t_step = 1e-2
t = np.arange(t_start, t_end, t_step)
u = np.ones(len(t)).tolist()
num = [0.1]
den = [1, -0.9]
sys = scipysig.TransferFunction(num, den, dt=t_step)
t,y = scipysig.dlsim(sys, u, t)
y = y[:,0]
data = iddata(y,u,t_step,[0,0])
# wrong initial conditions
with self.assertRaises(ValueError):
r, _ = virtual_reference(data, num, den)
#test good data, first order
data = iddata(y,u,t_step,[0])
r, _ = virtual_reference(data, num, den)
for i in range(len(r)):
self.assertTrue(np.isclose(r[i], u[i]))
num = [1-1.6+0.63]
den = [1, -1.6, 0.63]
sys = scipysig.TransferFunction(num, den, dt=t_step)
t, y = scipysig.dlsim(sys, u, t)
y = y[:,0]
data = iddata(y,u,t_step,[0,0])
#test second order
r, _ = virtual_reference(data, num, den)
for i in range(len(r)):
self.assertTrue(np.isclose(r[i], u[i]))