-
Notifications
You must be signed in to change notification settings - Fork 7
/
IZHI.py
76 lines (57 loc) · 1.77 KB
/
IZHI.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''Izhikevich neuron
paper: https://www.izhikevich.org/publications/spikes.pdf
'''
class IZHI:
def __init__(self):
self.a = .1
self.b = .2
self.c = -65
self.d = 8
self.thrs = 30
self.v = -65
self.u = -14
'''Step to get the next voltage
Parameters
-------
dt: float
Time variantion [mS]
I: float
Actual current [mA]
method: integer
Choose the diferentiation solve method
0 - Euler Method
1 - Runge Kutta 4th Order method
'''
def step(self, dt, I, method=0):
if method != 1 and method != 0:
print("Invalid method\n0 - EULER\n1 - RK4\n")
if self.v >= self.thrs:
self.v = self.c
self.u += self.d
else:
if method == 1:
self.solve_rk4(dt, I)
elif method == 0:
self.solve_euler(dt, I)
def solve_euler(self,dt,I):
dv = self.f_v(I,self.v,dt)
du = self.f_u(self.u,dt)
self.v += dv
self.u += du
def solve_rk4(self, dt, I):
dv1 = self.f_v(I, self.v,dt)
dv2 = self.f_v(I, self.v + dv1*0.5,dt)
dv3 = self.f_v(I, self.v + dv2*0.5,dt)
dv4 = self.f_v(I, self.v + dv3,dt)
dv = 1/6*(dv1 + dv2*2 + dv3*2 + dv4)
du1 = self.f_u(self.u,dt)
du2 = self.f_u(self.u + du1*0.5,dt)
du3 = self.f_u(self.u + du2*0.5,dt)
du4 = self.f_u(self.u + du3,dt)
du = 1/6*(du1 + du2*2 + du3*2 + du4)
self.v += dv
self.u += du
def f_v(self, i, v, dt):
return (0.04 * v * v + 5 * v + 140 - self.u + i) *dt
def f_u(self, u,dt):
return self.a * (self.b * self.v - u) *dt