def test_mapping(Simulator, plt, seed): sys = Alpha(0.1) syn = Lowpass(0.01) gsyn = 2*syn # scaled lowpass isyn = 2/s # scaled integrator dt = 0.001 ss = ss2sim(sys, syn, None) # normal lowpass, continuous dss = ss2sim(sys, syn, dt) # normal lowpass, discrete gss = ss2sim(sys, gsyn, None) # scaled lowpass, continuous gdss = ss2sim(sys, gsyn, dt) # scaled lowpass, discrete iss = ss2sim(sys, isyn, None) # scaled integrator, continuous idss = ss2sim(sys, isyn, dt) # scaled integrator, discrete assert ss.analog and gss.analog and iss.analog assert not (dss.analog or gdss.analog or idss.analog) with Network(seed=seed) as model: stim = nengo.Node(output=lambda t: np.sin(20*np.pi*t)) probes = [] for mapped, synapse in ((ss, syn), (dss, syn), (gss, gsyn), (gdss, gsyn), (iss, isyn), (idss, isyn)): A, B, C, D = mapped.ss x = nengo.Node(size_in=2) y = nengo.Node(size_in=1) nengo.Connection(stim, x, transform=B, synapse=synapse) nengo.Connection(x, x, transform=A, synapse=synapse) nengo.Connection(x, y, transform=C, synapse=None) nengo.Connection(stim, y, transform=D, synapse=None) probes.append(nengo.Probe(y)) p_stim = nengo.Probe(stim) pss, pdss, pgss, pgdss, piss, pidss = probes with Simulator(model, dt=dt) as sim: sim.run(1.0) expected = shift(sys.filt(sim.data[p_stim], dt)) plt.plot(sim.trange(), sim.data[pss], label="Continuous", alpha=0.5) plt.plot(sim.trange(), sim.data[pdss], label="Discrete", alpha=0.5) plt.plot(sim.trange(), sim.data[pgss], label="Gain Cont.", alpha=0.5) plt.plot(sim.trange(), sim.data[pgdss], label="Gain Disc.", alpha=0.5) plt.plot(sim.trange(), sim.data[piss], label="Integ Cont.", alpha=0.5) plt.plot(sim.trange(), sim.data[pidss], label="Integ Disc.", alpha=0.5) plt.plot(sim.trange(), expected, label="Expected", linestyle='--') plt.legend() assert np.allclose(sim.data[pss], expected, atol=0.01) assert np.allclose(sim.data[pdss], expected) assert np.allclose(sim.data[pgss], expected, atol=0.01) assert np.allclose(sim.data[pgdss], expected) assert np.allclose(sim.data[piss], expected, atol=0.01) assert np.allclose(sim.data[pidss], expected)
def test_balred(rng): dt = 0.001 sys = Alpha(0.01) + Lowpass(0.001) u = rng.normal(size=2000) expected = sys.filt(u, dt) def check(order, within, tol, method='del'): redsys = balred(sys, order, method=method) assert redsys.order_den <= order actual = redsys.filt(u, dt) assert abs(rmse(expected, actual) - within) < tol with warns(UserWarning): check(4, 0, 1e-13) with warns(UserWarning): check(3, 0, 1e-13) check(2, 0.03, 0.01) check(1, 0.3, 0.1)