-
Notifications
You must be signed in to change notification settings - Fork 7
/
LIF.py
46 lines (33 loc) · 1.05 KB
/
LIF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'''Leaky Integrate and Fire neuron
source: https://neuronaldynamics.epfl.ch/online/Ch1.S3.html
'''
class LIF:
def __init__(self):
self.R = 5
self.C = 3
self.uR = -40
self.thrs = 30
self.maxV = 90
self.v = -65
def step(self, I, dt, method=0):
if self.v >= self.thrs:
self.v = self.uR
else:
if method == 1:
self.solve_rk4(dt, I)
elif method == 0:
self.solve_euler(dt, I)
if self.v >= self.thrs:
self.v = self.maxV
def solve_euler(self, I, dt):
dv = self.fu(self.v, I) * dt
self.v += dv
def solve_rk4(self, I, dt):
dv1 = self.fu(self.v, I) * dt
dv2 = self.fu(self.v + dv1 * 0.5, I) * dt
dv3 = self.fu(self.v + dv2 * 0.5, I) * dt
dv4 = self.fu(self.v + dv3, I) * dt
dv = 1 / 6 * (dv1 + dv2 * 2 + dv3 * 2 + dv4)
self.v += dv
def fu(self, v, I):
return (-v + self.R * I) / (self.R * self.C)