Example #1
0
def test_passthrough_filter(Simulator):
    m = nengo.Network(label="test_passthrough", seed=0)
    with m:
        omega = 2 * np.pi * 5
        u = nengo.Node(output=lambda t: np.sin(omega * t))
        passthrough = nengo.Node(size_in=1)
        v = nengo.Node(output=lambda t, x: x, size_in=1)

        synapse = 0.3
        nengo.Connection(u, passthrough, synapse=None)
        nengo.Connection(passthrough, v, synapse=synapse)

        up = nengo.Probe(u)
        vp = nengo.Probe(v)

    dt = 0.001
    sim = Simulator(m, dt=dt)
    sim.run(1.0)

    t = sim.trange()
    x = sim.data[up]
    y = filt(x, synapse / dt)
    z = sim.data[vp]

    with Plotter(Simulator) as plt:
        plt.plot(t, x)
        plt.plot(t, y)
        plt.plot(t, z)
        plt.savefig("test_node.test_passthrough_filter.pdf")
        plt.close()

    # TODO: we have a two-step delay here since the passthrough node is not
    #   actually optimized out. We should look into doing this.
    assert np.allclose(y[:-2], z[2:])
Example #2
0
def test_passthrough_filter(Simulator):
    m = nengo.Network(label="test_passthrough", seed=0)
    with m:
        omega = 2 * np.pi * 5
        u = nengo.Node(output=lambda t: np.sin(omega * t))
        passthrough = nengo.Node(size_in=1)
        v = nengo.Node(output=lambda t, x: x, size_in=1)

        synapse = 0.3
        nengo.Connection(u, passthrough, synapse=None)
        nengo.Connection(passthrough, v, synapse=synapse)

        up = nengo.Probe(u)
        vp = nengo.Probe(v)

    dt = 0.001
    sim = Simulator(m, dt=dt)
    sim.run(1.0)

    t = sim.trange()
    x = sim.data[up]
    y = filt(x, synapse / dt)
    z = sim.data[vp]

    with Plotter(Simulator) as plt:
        plt.plot(t, x)
        plt.plot(t, y)
        plt.plot(t, z)
        plt.savefig("test_node.test_passthrough_filter.pdf")
        plt.close()

    assert np.allclose(y[:-1], z[1:])
Example #3
0
def test_passthrough_filter(Simulator):
    m = nengo.Network(label="test_passthrough", seed=0)
    with m:
        omega = 2 * np.pi * 5
        u = nengo.Node(output=lambda t: np.sin(omega * t))
        passthrough = nengo.Node(size_in=1)
        v = nengo.Node(output=lambda t, x: x, size_in=1)

        synapse = 0.3
        nengo.Connection(u, passthrough, synapse=None)
        nengo.Connection(passthrough, v, synapse=synapse)

        up = nengo.Probe(u)
        vp = nengo.Probe(v)

    dt = 0.001
    sim = Simulator(m, dt=dt)
    sim.run(1.0)

    t = sim.trange()
    x = sim.data[up]
    y = filt(x, synapse / dt)
    z = sim.data[vp]

    with Plotter(Simulator) as plt:
        plt.plot(t, x)
        plt.plot(t, y)
        plt.plot(t, z)
        plt.savefig("test_node.test_passthrough_filter.pdf")
        plt.close()

    # TODO: we have a two-step delay here since the passthrough node is not
    #   actually optimized out. We should look into doing this.
    assert np.allclose(y[:-2], z[2:])
Example #4
0
def test_lowpass(Simulator, plt):
    dt = 1e-3
    tau = 0.03

    t, x, yhat = run_synapse(Simulator, nengo.synapses.Lowpass(tau), dt=dt)
    y = filt(x, tau / dt)

    assert allclose(t, y, yhat, delay=dt, plt=plt)
Example #5
0
def test_decoders(Simulator, nl, plt):
    dt = 1e-3
    tau = 0.01

    t, x, yhat = run_synapse(
        Simulator, nengo.synapses.Lowpass(tau), dt=dt, n_neurons=100)

    y = filt(x, tau / dt)
    assert allclose(t, y, yhat, delay=dt, plt=plt)
Example #6
0
def test_lowpass(Simulator):
    dt = 1e-3
    tau = 0.03

    t, x, yhat = run_synapse(Simulator, nengo.synapses.Lowpass(tau), dt=dt)
    y = filt(x, tau / dt)

    assert allclose(t, y.flatten(), yhat.flatten(), delay=1,
                    plotter=Plotter(Simulator),
                    filename='test_synapse.test_lowpass.pdf')
Example #7
0
def test_filtfilt():
    dt = 1e-3
    tend = 3.
    t = dt * np.arange(tend / dt)
    nt = len(t)

    tau = 0.03 / dt

    u = np.random.normal(size=nt)
    x = filt(u, tau)
    x = filt(x[::-1], tau, x0=x[-1])[::-1]
    y = filtfilt(u, tau)

    with Plotter(nengo.Simulator) as plt:
        plt.plot(t, x)
        plt.plot(t, y, '--')
        plt.savefig('utils.test_filtering.test_filtfilt.pdf')
        plt.close()

    assert np.allclose(x, y)
Example #8
0
def test_decoders(Simulator, nl):
    dt = 1e-3
    tau = 0.01

    t, x, yhat = run_synapse(
        Simulator, nengo.synapses.Lowpass(tau), dt=dt, n_neurons=100)

    y = filt(x, tau / dt)
    assert allclose(t, y.flatten(), yhat.flatten(), delay=1,
                    plotter=Plotter(Simulator, nl),
                    filename='test_synapse.test_decoders.pdf')
Example #9
0
def test_lowpass(Simulator):
    dt = 1e-3
    tau = 0.03

    t, x, yhat = run_synapse(Simulator, nengo.synapses.Lowpass(tau), dt=dt)
    y = filt(x, tau / dt)

    assert allclose(t,
                    y.flatten(),
                    yhat.flatten(),
                    delay=1,
                    plotter=Plotter(Simulator),
                    filename='test_synapse.test_lowpass.pdf')
Example #10
0
def test_lti_lowpass():
    dt = 1e-3
    tend = 3.
    t = dt * np.arange(tend / dt)
    nt = len(t)

    tau = 1e-2

    d = -np.expm1(-dt / tau)
    a = [d]
    b = [1, d - 1]

    u = np.random.normal(size=(nt, 10))
    x = filt(u, tau / dt)
    y = lti(u, (a, b))
    assert np.allclose(x, y)
Example #11
0
def test_decoders(Simulator, nl):
    dt = 1e-3
    tau = 0.01

    t, x, yhat = run_synapse(Simulator,
                             nengo.synapses.Lowpass(tau),
                             dt=dt,
                             n_neurons=100)

    y = filt(x, tau / dt)
    assert allclose(t,
                    y.flatten(),
                    yhat.flatten(),
                    delay=1,
                    plotter=Plotter(Simulator, nl),
                    filename='test_synapse.test_decoders.pdf')
Example #12
0
def test_filt():
    dt = 1e-3
    tend = 3.
    t = dt * np.arange(tend / dt)
    nt = len(t)

    tau = 0.1 / dt

    u = np.random.normal(size=nt)

    tk = np.arange(0, 30 * tau)
    k = 1. / tau * np.exp(-tk / tau)
    x = np.convolve(u, k, mode='full')[:nt]

    y = filt(u, tau)

    with Plotter(nengo.Simulator) as plt:
        plt.plot(t, x)
        plt.plot(t, y, '--')
        plt.savefig('utils.test_filtering.test_filt.pdf')
        plt.close()

    assert np.allclose(x, y, atol=1e-3, rtol=1e-2)