+10 XP

Adding Spike Detection

After each Euler step, check: did V reach threshold? If yes → record the spike time, reset voltage to vr. This is the fire-and-reset rule.

spike_times.append(t) adds the current time to a list. After the loop, spike_times contains every moment the neuron fired — this is a spike train, the fundamental output of any real neuron.

python
import numpy as np
dt=1e-3; tau=20e-3; el=-60e-3; vr=-70e-3; vth=-50e-3
r=100e6; i_mean=25e-11; t_max=150e-3
v = el
spike_times = []
for step in range(int(t_max/dt)):
    t = step * dt
    i_t = i_mean * (1 + np.sin(2 * np.pi * t / 0.01))
    v = v + (dt / tau) * (el - v + r * i_t)
    if v >= vth:
Euler update first, then check
        spike_times.append(t)
If V hit threshold → record and reset
        v = vr  # reset!
print(f"Total spikes: {len(spike_times)}")
print(f"First 5 spike times (ms): {[round(t*1000) for t in spike_times[:5]]}")

Run this — you should see ~10-15 spikes in 150 ms.