-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sb10yd #203
Merged
Merged
Add sb10yd #203
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aa22020
Add sb01yd, first commit
KybernetikJo 374434a
Improve docstring, restore the order of procedurces
KybernetikJo 459d501
Fix import order in __init__.py
KybernetikJo 57d0b08
Improve unitest
KybernetikJo 59adbfd
Delete sb10yd.pyf artifact created by f2py
KybernetikJo 2545fe4
Update docstring
KybernetikJo 0b1530f
Add unittest for discrete time case
KybernetikJo 9567694
Delete matplotlib import in unittest
KybernetikJo 4206fef
Clean up pytest, get rid of unittest
KybernetikJo 6ff1902
Update test, add case n=0
KybernetikJo 42b8885
Delint
bnavigator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import numpy as np | ||
from scipy import signal | ||
|
||
from slycot import synthesis | ||
|
||
|
||
class Test_sb10yd(): | ||
|
||
# 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) | ||
num, den = sys_tf.num, sys_tf.den | ||
|
||
omega, H = signal.freqs(num, den) | ||
|
||
real_H_resp = np.real(H) | ||
imag_H_resp = np.imag(H) | ||
|
||
n = 0 | ||
dico = 0 # 0 for continuous time | ||
flag = 0 # 0 for no constraints on the poles | ||
n_id, *_ = synthesis.sb10yd( | ||
dico, flag, len(omega), | ||
real_H_resp, imag_H_resp, omega, n, tol=0) | ||
|
||
# Because flag = 0, we expect n == n_id | ||
np.testing.assert_equal(n, n_id) | ||
|
||
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)) | ||
|
||
sys_tf = signal.ss2tf(A, B, C, D) | ||
num, den = sys_tf | ||
|
||
omega, H = signal.freqs(num.squeeze(), den) | ||
|
||
real_H_resp = np.real(H) | ||
imag_H_resp = np.imag(H) | ||
|
||
n = 2 | ||
dico = 0 # 0 for continuous time | ||
flag = 0 # 0 for no constraints on the poles | ||
n_id, *_ = synthesis.sb10yd( | ||
dico, flag, len(omega), | ||
real_H_resp, imag_H_resp, omega, n, tol=0) | ||
|
||
# Because flag = 0, we expect n == n_id | ||
np.testing.assert_equal(n, n_id) | ||
|
||
def test_sb10yd_cont_allclose(self): | ||
"""Compare given and identified frequency response. 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)) | ||
|
||
sys_tf = signal.ss2tf(A, B, C, D) | ||
num, den = sys_tf | ||
|
||
omega, H = signal.freqs(num.squeeze(), den) | ||
|
||
real_H_resp = np.real(H) | ||
imag_H_resp = np.imag(H) | ||
|
||
n = 2 | ||
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), | ||
real_H_resp, imag_H_resp, omega, n, tol=0) | ||
|
||
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) | ||
|
||
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) | ||
num, den = sys_tf.num, sys_tf.den | ||
|
||
omega, H = signal.freqz(num.squeeze(), den) | ||
|
||
real_H_resp = np.real(H) | ||
imag_H_resp = np.imag(H) | ||
|
||
n = 0 | ||
dico = 1 # 0 for discrete time | ||
flag = 0 # 0 for no constraints on the poles | ||
n_id, *_ = synthesis.sb10yd( | ||
dico, flag, len(omega), | ||
real_H_resp, imag_H_resp, omega, n, tol=0) | ||
|
||
# Because flag = 0, we expect n == n_id | ||
np.testing.assert_equal(n, n_id) | ||
|
||
def test_sb10yd_disc_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)) | ||
|
||
sys_tf = signal.ss2tf(A, B, C, D) | ||
num, den = sys_tf | ||
|
||
dt = 0.1 | ||
num, den, dt = signal.cont2discrete((num, den), dt, method="zoh") | ||
print(den) | ||
|
||
omega, H = signal.freqz(num.squeeze(), den) | ||
|
||
real_H_resp = np.real(H) | ||
imag_H_resp = np.imag(H) | ||
|
||
n = 2 | ||
dico = 1 # 0 for discrete time | ||
flag = 0 # 0 for no constraints on the poles | ||
n_id, *_ = synthesis.sb10yd( | ||
dico, flag, len(omega), | ||
real_H_resp, imag_H_resp, omega, n, tol=0) | ||
|
||
# Because flag = 0, we expect n == n_id | ||
np.testing.assert_equal(n, n_id) | ||
|
||
def test_sb10yd_disc_allclose(self): | ||
"""Compare given and identified frequency response. 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)) | ||
|
||
sys_tf = signal.ss2tf(A, B, C, D) | ||
num, den = sys_tf | ||
|
||
dt = 0.01 | ||
num, den, dt = signal.cont2discrete((num, den), dt, method="zoh") | ||
|
||
omega, H = signal.freqz(num.squeeze(), den) | ||
|
||
real_H_resp = np.real(H) | ||
imag_H_resp = np.imag(H) | ||
|
||
n = 2 | ||
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), | ||
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_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) | ||
|
||
np.testing.assert_allclose(abs(H_id), abs(H), rtol=0, atol=1.0) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please cover this case in the unit tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to look into that.