DiffEncoder

DiffEncoder#

class brainpy.encoding.DiffEncoder(threshold=0.1, padding=False, off_spike=False)[source]#

Generate spike only when the difference between two subsequent time steps meets a threshold.

Optionally include off_spikes for negative changes.

Example:

>>> a = bm.array([1, 2, 2.9, 3, 3.9])
>>> encoder = DiffEncoder(threshold=1)
>>> encoder.multi_steps(a)
Array([1., 0., 0., 0.])

>>> encoder = DiffEncoder(threshold=1, padding=True)
>>> encoder.multi_steps(a)
Array([0., 1., 0., 0., 0.])

>>> b = bm.array([1, 2, 0, 2, 2.9])
>>> encoder = DiffEncoder(threshold=1, off_spike=True)
>>> encoder.multi_steps(b)
Array([ 1.,  1., -1.,  1.,  0.])

>>> encoder = DiffEncoder(threshold=1, padding=True, off_spike=True)
>>> encoder.multi_steps(b)
Array([ 0.,  1., -1.,  1.,  0.])
Parameters:
  • threshold (float) – float. Input features with a change greater than the thresold across one timestep will generate a spike, defaults to 0.1.

  • padding (bool) – bool. Used to change how the first time step of spikes are measured. If True, the first time step will be repeated with itself resulting in 0’s for the output spikes. If False, the first time step will be padded with 0’s, defaults to False.

  • off_spike (bool) – bool. If True, negative spikes for changes less than -threshold, defaults to False.

multi_steps(x)[source]#

Encoding multistep inputs with the spiking trains.

Parameters:

x – Array. The array with the shape of (num_step, ….).

Returns:

Array. The spike train.

Return type:

out