def test_identity(radii): sys = Alpha(0.1) identity = Identity() assert repr(identity) == "Identity()" I = np.eye(len(sys)) realize_result = identity(sys, radii) assert realize_result.sys is sys assert np.allclose(realize_result.T, I * radii) assert np.allclose(realize_result.Tinv, inv(I * radii)) rsys = realize_result.realization assert ss_equal(rsys, sys.transform(realize_result.T)) # Check that it's still the same system, even though different matrices assert sys_equal(sys, rsys) if radii == 1: assert ss_equal(sys, rsys) else: assert not np.allclose(sys.B, rsys.B) assert not np.allclose(sys.C, rsys.C) # Check that the state vectors have scaled power assert np.allclose(state_norm(sys) / radii, state_norm(rsys))
def test_scale_state(radius): sys = Alpha(0.1) scaled = scale_state(sys, radii=radius) assert not np.allclose(sys.B, scaled.B) assert not np.allclose(sys.C, scaled.C) # Check that it's still the same system, even though different matrices assert sys_equal(sys, scaled) # Check that the state vectors have scaled power assert np.allclose(state_norm(sys) / radius, state_norm(scaled))
def test_double_exp(): tau1 = 0.005 tau2 = 0.008 sys = DoubleExp(tau1, tau2) assert sys_equal(sys, ([1], [tau1*tau2, tau1 + tau2, 1])) assert sys == Lowpass(tau1) * Lowpass(tau2) assert sys == 1 / ((tau1*s + 1) * (tau2*s + 1)) # this equality follows from algebraic manipulation of the above equality # however there will be a ZeroDivisionError when tau1 == tau2 assert sys == (tau1*Lowpass(tau1) - tau2*Lowpass(tau2)) / (tau1 - tau2)
def test_highpass(tau, order): sys = Highpass(tau, order) num, den = map(np.poly1d, ([tau, 0], [tau, 1])) assert sys_equal(sys, LinearSystem((num**order, den**order))) length = 1000 dt = 0.001 response = sys.impulse(length, dt) dft = np.fft.rfft(response, axis=0) p = abs(dft) # Check that the power is monotonically increasing assert np.allclose(np.sort(p), p)
def test_balreal_normalization(sys): radii = np.arange(len(sys)) + 1 balanced = Balanced() assert repr(balanced) == "Balanced()" realizer_result = balanced(sys, radii) T, Tinv, _ = balanced_transformation(sys) assert np.allclose(realizer_result.T / radii[None, :], T) assert np.allclose(realizer_result.Tinv * radii[:, None], Tinv) assert np.allclose(inv(T), Tinv) assert sys_equal(sys, realizer_result.realization)
def test_bandpass(freq, Q): sys = Bandpass(freq, Q) w_0 = freq * (2*np.pi) assert sys_equal(sys, ([1], [1./w_0**2, 1./(w_0*Q), 1])) length = 10000 dt = 0.0001 response = sys.impulse(length, dt) dft = np.fft.rfft(response, axis=0) freqs = np.fft.rfftfreq(length, d=dt) cp = abs(dft).cumsum() # Check that the cumulative power reaches its mean at Q frequency np.allclose(freqs[np.where(cp >= cp[-1] / 2)[0][0]], Q)
def test_similarity_transform(): sys = Alpha(0.1) TA, TB, TC, TD = similarity_transform(sys, np.eye(2)).ss A, B, C, D = sys2ss(sys) assert np.allclose(A, TA) assert np.allclose(B, TB) assert np.allclose(C, TC) assert np.allclose(D, TD) T = [[1, 1], [-0.5, 0]] TA, TB, TC, TD = similarity_transform(sys, T).ss assert not np.allclose(A, TA) assert not np.allclose(B, TB) assert not np.allclose(C, TC) assert np.allclose(D, TD) assert sys_equal(sys, (TA, TB, TC, TD))
def test_nengo_analogs(): assert sys_equal(BaseLinearFilter([1], [1, 0]), LinearFilter([1], [1, 0])) assert sys_equal(BaseLowpass(0.1), Lowpass(0.1)) assert sys_equal(BaseAlpha(0.1), Alpha(0.1)) assert sys_equal(BaseAlpha(0.1), DoubleExp(0.1, 0.1))
def test_nengo_analogs(): assert sys_equal(BaseLinearFilter([1], [1, 0]), LinearSystem(([1], [1, 0]))) assert sys_equal(BaseLowpass(0.1), Lowpass(0.1)) assert sys_equal(BaseAlpha(0.1), Alpha(0.1)) assert sys_equal(BaseAlpha(0.1), DoubleExp(0.1, 0.1))