-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlookahead.pyx
71 lines (58 loc) · 1.92 KB
/
lookahead.pyx
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
# cython: infer_types=True
# cython: profile=False
# cython: boundscheck=False
# cython: wraparound=False
import numpy as np
def evolve_return_best(object speaker, float position1, float position2, float voltage1, float voltage2, float[:, ::1] voltages, float[::1] data):
cdef double c1 = speaker.c1
cdef double c2 = speaker.c2
cdef double b1 = speaker.b1
cdef double b2 = speaker.b2
cdef double scale = speaker.scale
cdef int i, j
cdef double y, y1, y2
cdef float x1, x2
cdef int lowest_idx
cdef double lowest_err = 1e9
cdef double error
for i in range(voltages.shape[0]):
x1 = voltage1
x2 = voltage2
y1 = position1
y2 = position2
error = 0
for j in range(voltages.shape[1]):
y = c1 * y1 - c2 * y2 + b1 * x1 + b2 * x2
error += (y * scale - data[j]) ** 2
if error > lowest_err:
break
x2 = x1
x1 = voltages[i, j] # XXX does this really always lag?
y2 = y1
y1 = y
if error < lowest_err:
lowest_err = error
lowest_idx = i
return lowest_idx
def evolve(object speaker, float position1, float position2, float voltage1, float voltage2, float[:, ::1] voltages):
cdef double[:,::1] output = np.empty_like(voltages, dtype=np.float64)
cdef double c1 = speaker.c1
cdef double c2 = speaker.c2
cdef double b1 = speaker.b1
cdef double b2 = speaker.b2
cdef int i, j
cdef double y, y1, y2
cdef float x1, x2
for i in range(voltages.shape[0]):
x1 = voltage1
x2 = voltage2
y1 = position1
y2 = position2
for j in range(voltages.shape[1]):
y = c1 * y1 - c2 * y2 + b1 * x1 + b2 * x2
output[i, j] = y
x2 = x1
x1 = voltages[i, j] # XXX does this really always lag?
y2 = y1
y1 = y
return output