3. Encoding information as spikes
The problem
A spike is just an event. It carries no number inside it. So a value such as the brightness of a pixel or the loudness of a sound has to be turned into a pattern of spikes. This is called encoding. The way you encode changes how many spikes you need, how quickly you get an answer and how sensitive the result is to noise.
Rate coding: the value is how many spikes
A brighter pixel produces more spikes in a time window. There are two common ways to produce them:
- Feed the value to a LIF neuron as a constant input. A stronger input makes the neuron fire more often (see the table below).
- Use randomness: at every step, emit a spike with a probability equal to the value. This is common in tutorials.
Strengths: simple, and not bothered by small timing errors. Weaknesses: reading the value accurately takes many time steps, and it uses many spikes. In event-driven hardware the cost generally grows with the number of spikes.
The random version is noisy. For a value p over T steps, the spike count is about p × T, with a typical spread of the square root of T × p × (1 - p). For p = 0.3 and T = 100 that is about 30 spikes give or take 5, which is roughly a 15% error in the estimated value. At T = 1,000 the error falls to about 5%.
Temporal coding: the value is when the first spike comes
A stronger input makes the neuron fire sooner. A LIF neuron already does this. With beta = 0.9 and threshold = 1.0, starting from zero:
| Input per step | First spike at step | Spikes per 100 steps (rate) |
|---|---|---|
| 0.12 | 18 | 5 |
| 0.15 | 11 | 9 |
| 0.2 | 7 | 14 |
| 0.3 | 4 | 25 |
| 0.5 | 3 | 33 |
The same neuron shows both codes. The middle column is a latency code: one spike, and its timing carries the value. The last column is a rate code: the value shows up as how often it fires. Because the neuron resets to zero after each spike, the gap between spikes equals the first-spike step.
Strengths: a single spike can carry a value, so it is fast and uses few spikes. Weaknesses: it depends on precise timing, so noise or jitter changes the value, and it needs a known starting time such as the moment a stimulus appears.
Event-based data
An event camera does not capture frames. Each pixel reports only when its brightness changes. An event carries the pixel position (x, y), a timestamp and a polarity (brighter or darker). A still scene produces almost no events and fast motion produces many, so the data is already sparse and spike-like. It can be fed to a spiking network without converting frames into spikes first.
Public datasets exist, such as N-MNIST (digits recorded with an event camera) and DVS Gesture (hand gestures). The Tonic library can load them. Check each project's own site for current details.
Which one to use?
- Rate coding: robust and easy, but slow and spike-hungry.
- Temporal coding: fast and sparse, but sensitive to timing noise.
- Event data: no conversion step, but you need an event sensor or an event dataset.
Real systems often mix these ideas.
Try it
Open the LIF neuron playground and keep Leak at 0.9 and Threshold at 1.0. Set the input to 0.12 and count the steps between spikes with the step counter: it should be 18. Raise the input to 0.5: the gap should shrink to 3.
Exercise
With beta = 0.9 and threshold = 1.0, what is the smallest input (in the playground's 0.005 slider steps) that makes the neuron fire by step 5? Predict, then test.
Show the answer
After n steps from zero, the potential is input × (1 - 0.9^n) / 0.1. At n = 5 the factor is about 4.095, so the input must be at least 1 / 4.095, which is about 0.244. On the slider, the smallest value that works is 0.245. At 0.24 the neuron fires on step 6 instead.
Limits
These are the engineering codes used in spiking networks. How real brains encode information is still an active research question, and this lesson does not claim to describe it.