def test_linearfilter_y0(allclose): # --- y0 sets initial state correctly for high-order filter synapse = LinearFilter(butter_num, butter_den, analog=False) v = 9.81 x = v * np.ones(10) assert allclose(synapse.filt(x, y0=v), v) assert not allclose(synapse.filt(x, y0=0), v, record_rmse=False) # --- y0 does not work for high-order synapse when DC gain is zero synapse = LinearFilter([1, 0], [1, 1]) with pytest.raises(ValidationError, match="Cannot solve for state"): synapse.filt(np.ones(10), y0=1)
def test_linearfilter(Simulator, plt, seed, allclose): dt = 1e-3 synapse = LinearFilter(butter_num, butter_den, analog=False) t, x, yhat = run_synapse(Simulator, seed, synapse, dt=dt) y = synapse.filt(x, dt=dt, y0=0) assert signals_allclose(t, y, yhat, delay=dt, plt=plt, allclose=allclose)
def test_direct(Simulator, plt, seed, allclose): dt = 1e-3 a = 0.7 synapse = LinearFilter([a], [1], analog=False) t, x, yhat = run_synapse(Simulator, seed, synapse, dt=dt) y = synapse.filt(x, dt=dt, y0=0) assert signals_allclose(t, y, yhat, delay=dt, allclose=allclose) assert signals_allclose(t, a * x, y, plt=plt, allclose=allclose)
def test_linearfilter(Simulator, plt, seed): dt = 1e-3 # The following num, den are for a 4th order analog Butterworth filter, # generated with `scipy.signal.butter(4, 0.2, analog=False)` num = np.array( [0.00482434, 0.01929737, 0.02894606, 0.01929737, 0.00482434]) den = np.array([1., -2.36951301, 2.31398841, -1.05466541, 0.18737949]) synapse = LinearFilter(num, den, analog=False) t, x, yhat = run_synapse(Simulator, seed, synapse, dt=dt) y = synapse.filt(x, dt=dt, y0=0) assert allclose(t, y, yhat, delay=dt, plt=plt)
def test_linear_analog(Simulator, seed): dt = 1e-3 # The following num, den are for a 4th order analog Butterworth filter, # generated with `scipy.signal.butter(4, 200, analog=True)` num = np.array([1.60000000e+09]) den = np.array([1.00000000e+00, 5.22625186e+02, 1.36568542e+05, 2.09050074e+07, 1.60000000e+09]) synapse = LinearFilter(num, den, analog=True) t, x, yhat = run_synapse(Simulator, seed, synapse, dt=dt) y = synapse.filt(x, dt=dt, y0=0) assert allclose(t, y, yhat, delay=dt, atol=5e-4)
def test_linear_analog(Simulator, seed): dt = 1e-3 # The following num, den are for a 4th order analog Butterworth filter, # generated with `scipy.signal.butter(4, 200, analog=True)` num = np.array([1.60000000e09]) den = np.array( [1.00000000e00, 5.22625186e02, 1.36568542e05, 2.09050074e07, 1.60000000e09] ) synapse = LinearFilter(num, den, analog=True) t, x, yhat = run_synapse(Simulator, seed, synapse, dt=dt) y = synapse.filt(x, dt=dt, y0=0) assert signals_allclose(t, y, yhat, delay=dt, atol=5e-4)
def test_lti_lowpass(rng, plt): dt = 1e-3 tend = 3. t = dt * np.arange(tend / dt) nt = len(t) tau = 1e-2 lti = LinearFilter([1], [tau, 1]) u = rng.normal(size=(nt, 10)) x = Lowpass(tau).filt(u, dt=dt) y = lti.filt(u, dt=dt) plt.plot(t, x[:, 0], label="Lowpass") plt.plot(t, y[:, 0], label="LTI") plt.legend(loc="best") assert np.allclose(x, y)
def test_zero_matrices(Simulator, zero, seed): dt = 1e-3 A = np.diag(np.ones(2) * dt) B = np.zeros((2, 1)) B[0] = 1 C = np.ones((1, 2)) D = np.ones((1, )) if zero == "C": C[...] = 0 elif zero == "D": D[...] = 0 num, den = ss2tf(A, B, C, D) num = num.flatten() synapse = LinearFilter(num, den, analog=False) t, x, yhat = run_synapse(Simulator, seed, synapse, dt=dt) y = synapse.filt(x, dt=dt, y0=0) assert allclose(t, y, yhat, delay=dt * 2 if zero == "D" else dt, atol=5e-5)
def test_zero_matrices(Simulator, zero, seed): dt = 1e-3 A = np.diag(np.ones(2) * dt) B = np.zeros((2, 1)) B[0] = 1 C = np.ones((1, 2)) D = np.ones((1,)) if zero == "C": C[...] = 0 elif zero == "D": D[...] = 0 num, den = ss2tf(A, B, C, D) num = num.flatten() synapse = LinearFilter(num, den, analog=False) t, x, yhat = run_synapse(Simulator, seed, synapse, dt=dt) y = synapse.filt(x, dt=dt, y0=0) assert allclose(t, y, yhat, delay=dt * 2 if zero == "D" else dt, atol=5e-5)