Skip to content

Commit

Permalink
Delint
Browse files Browse the repository at this point in the history
  • Loading branch information
bnavigator committed Aug 26, 2023
1 parent 6ff1902 commit 42b8885
Showing 1 changed file with 38 additions and 50 deletions.
88 changes: 38 additions & 50 deletions slycot/tests/test_sb10yd.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from slycot import synthesis
import numpy as np
from scipy import signal

from slycot import synthesis


class Test_sb10yd():

# TODO: There are might be better systems/filters to do these tests.
# TODO: There might be better systems/filters to do these tests.

def test_sb10yd_cont_exec_case_n0(self):
"""A simple execution test. Case n=0.
"""

sys_tf = signal.TransferFunction(1,1)
sys_tf = signal.TransferFunction(1, 1)
num, den = sys_tf.num, sys_tf.den

omega, H = signal.freqs(num, den)
Expand All @@ -19,10 +20,10 @@ def test_sb10yd_cont_exec_case_n0(self):
imag_H_resp = np.imag(H)

n = 0
dico = 0 # 0 for continuous time
flag = 0 # 0 for no constraints on the poles
dico = 0 # 0 for continuous time
flag = 0 # 0 for no constraints on the poles
n_id, *_ = synthesis.sb10yd(
dico, flag, len(omega),
dico, flag, len(omega),
real_H_resp, imag_H_resp, omega, n, tol=0)

# Because flag = 0, we expect n == n_id
Expand All @@ -31,13 +32,12 @@ def test_sb10yd_cont_exec_case_n0(self):
def test_sb10yd_cont_exec(self):
"""A simple execution test. Case n=2.
"""

A = np.array([[0.0, 1.0], [-0.5, -0.1]])
B = np.array([[0.0], [1.0]])
C = np.array([[1.0, 0.0]])
D = np.zeros((1,1))
D = np.zeros((1, 1))

sys_tf = signal.ss2tf(A,B,C,D)
sys_tf = signal.ss2tf(A, B, C, D)
num, den = sys_tf

omega, H = signal.freqs(num.squeeze(), den)
Expand All @@ -46,10 +46,10 @@ def test_sb10yd_cont_exec(self):
imag_H_resp = np.imag(H)

n = 2
dico = 0 # 0 for continuous time
flag = 0 # 0 for no constraints on the poles
dico = 0 # 0 for continuous time
flag = 0 # 0 for no constraints on the poles
n_id, *_ = synthesis.sb10yd(
dico, flag, len(omega),
dico, flag, len(omega),
real_H_resp, imag_H_resp, omega, n, tol=0)

# Because flag = 0, we expect n == n_id
Expand All @@ -62,9 +62,9 @@ def test_sb10yd_cont_allclose(self):
A = np.array([[0.0, 1.0], [-0.5, -0.1]])
B = np.array([[0.0], [1.0]])
C = np.array([[1.0, 0.0]])
D = np.zeros((1,1))
D = np.zeros((1, 1))

sys_tf = signal.ss2tf(A,B,C,D)
sys_tf = signal.ss2tf(A, B, C, D)
num, den = sys_tf

omega, H = signal.freqs(num.squeeze(), den)
Expand All @@ -73,29 +73,23 @@ def test_sb10yd_cont_allclose(self):
imag_H_resp = np.imag(H)

n = 2
dico = 0 # 0 for continuous time
flag = 0 # 0 for no constraints on the poles
dico = 0 # 0 for continuous time
flag = 0 # 0 for no constraints on the poles
n_id, A_id, B_id, C_id, D_id = synthesis.sb10yd(
dico, flag, len(omega),
dico, flag, len(omega),
real_H_resp, imag_H_resp, omega, n, tol=0)
sys_tf_id = signal.ss2tf(A_id,B_id,C_id,D_id)

sys_tf_id = signal.ss2tf(A_id, B_id, C_id, D_id)
num_id, den_id = sys_tf_id
w_id, H_id = signal.freqs(num_id.squeeze(), den_id, worN=omega)

#print(np.max(abs(H)-abs(H_id)), np.max(abs(H_id)-abs(H)))
#print(np.max(abs(H_id)/abs(H)))

# Compare given and identified frequency response up to some toleration.
# absolute(a-b) <= atol + rtol*abolute(b), element-wise true
# absolute(a-b) or absolute(b-a) <= atol, for rtol=0 element-wise true
np.testing.assert_allclose(abs(H_id),abs(H),rtol=0,atol=0.1)
np.testing.assert_allclose(abs(H_id), abs(H), rtol=0, atol=0.1)

def test_sb10yd_disc_exec_case_n0(self):
"""A simple execution test. Case n=0.
"""

sys_tf = signal.TransferFunction(1,1,dt=0.1)
sys_tf = signal.TransferFunction(1, 1, dt=0.1)
num, den = sys_tf.num, sys_tf.den

omega, H = signal.freqz(num.squeeze(), den)
Expand All @@ -104,10 +98,10 @@ def test_sb10yd_disc_exec_case_n0(self):
imag_H_resp = np.imag(H)

n = 0
dico = 1 # 0 for discrete time
flag = 0 # 0 for no constraints on the poles
dico = 1 # 0 for discrete time
flag = 0 # 0 for no constraints on the poles
n_id, *_ = synthesis.sb10yd(
dico, flag, len(omega),
dico, flag, len(omega),
real_H_resp, imag_H_resp, omega, n, tol=0)

# Because flag = 0, we expect n == n_id
Expand All @@ -120,9 +114,9 @@ def test_sb10yd_disc_exec(self):
A = np.array([[0.0, 1.0], [-0.5, -0.1]])
B = np.array([[0.0], [1.0]])
C = np.array([[1.0, 0.0]])
D = np.zeros((1,1))
D = np.zeros((1, 1))

sys_tf = signal.ss2tf(A,B,C,D)
sys_tf = signal.ss2tf(A, B, C, D)
num, den = sys_tf

dt = 0.1
Expand All @@ -135,10 +129,10 @@ def test_sb10yd_disc_exec(self):
imag_H_resp = np.imag(H)

n = 2
dico = 1 # 0 for discrete time
flag = 0 # 0 for no constraints on the poles
dico = 1 # 0 for discrete time
flag = 0 # 0 for no constraints on the poles
n_id, *_ = synthesis.sb10yd(
dico, flag, len(omega),
dico, flag, len(omega),
real_H_resp, imag_H_resp, omega, n, tol=0)

# Because flag = 0, we expect n == n_id
Expand All @@ -151,9 +145,9 @@ def test_sb10yd_disc_allclose(self):
A = np.array([[0.0, 1.0], [-0.5, -0.1]])
B = np.array([[0.0], [1.0]])
C = np.array([[1.0, 0.0]])
D = np.zeros((1,1))
D = np.zeros((1, 1))

sys_tf = signal.ss2tf(A,B,C,D)
sys_tf = signal.ss2tf(A, B, C, D)
num, den = sys_tf

dt = 0.01
Expand All @@ -165,21 +159,15 @@ def test_sb10yd_disc_allclose(self):
imag_H_resp = np.imag(H)

n = 2
dico = 1 # 0 for discrete time
flag = 0 # 0 for no constraints on the poles
dico = 1 # 0 for discrete time
flag = 0 # 0 for no constraints on the poles
n_id, A_id, B_id, C_id, D_id = synthesis.sb10yd(
dico, flag, len(omega),
dico, flag, len(omega),
real_H_resp, imag_H_resp, omega, n, tol=0)
sys_id = signal.dlti(A_id,B_id,C_id,D_id, dt=dt)

sys_id = signal.dlti(A_id, B_id, C_id, D_id, dt=dt)
sys_tf_id = signal.TransferFunction(sys_id)
num_id, den_id = sys_tf_id.num, sys_tf_id.den
w_id, H_id = signal.freqz(num_id.squeeze(), den_id, worN=omega)

#print(np.max(abs(H)-abs(H_id)), np.max(abs(H_id)-abs(H)))
#print(np.max(abs(H_id)/abs(H)))

# Compare given and identified frequency response up to some toleration.
# absolute(a-b) <= atol + rtol*abolute(b), element-wise true
# absolute(a-b) or absolute(b-a) <= atol, for rtol=0 element-wise true
np.testing.assert_allclose(abs(H_id),abs(H),rtol=0,atol=1.0)
np.testing.assert_allclose(abs(H_id), abs(H), rtol=0, atol=1.0)

0 comments on commit 42b8885

Please sign in to comment.