How to use lowpass filter at each time-step?


Given an initialized nengo.Lowpass() object, how do I apply it to an input stream at each time-step? I assumed I was supposed to call the make_step method of the object and use the function it returned, but it doesn’t seem to be filtering at all… Am I using the function incorrectly?

import nengo

import numpy as np
import matplotlib.pyplot as plt

# make some spikes

n_neurons = 10

with nengo.Network() as model:
    sin = nengo.Node(lambda t: np.sin(t * 10))

    ens = nengo.Ensemble(n_neurons, 1)

    nengo.Connection(sin, ens)

    p_spikes = nengo.Probe(ens.neurons, synapse=None)

with nengo.Simulator(model) as sim:

spikes =[p_spikes]

# filter those spikes

lp = nengo.Lowpass(0.01)
lps = lp.make_step(n_neurons, n_neurons, 0.001, None, dtype=np.float64)

res = []
for t_i, sim_t in enumerate(list(sim.trange())):
    res.append(lps(sim_t, spikes[t_i]))

res = np.array(res)



The problem was the result I was appending to the list was a reference. So I had a list of references to a single variable. When I copied the variable res.append(lps(sim_t, spikes[t_i]).copy()) the filter worked as expected. Thanks @jgosmann for finding this problem.



Note also that synapses have filt and filtfilt methods to do this easier:

This should be identical to your snippet:

res = nengo.Lowpass(0.01).filt(spikes)

While this filters the signal twice, once forward and once backward, which can give nicer results when you don’t care about causality:

res = nengo.Lowpass(0.01).filtfilt(spikes)