-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres_utils.py
114 lines (88 loc) · 3.25 KB
/
res_utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import numpy as np
import pylab as plt
import scipy.signal
from scipy.stats import vonmises
import random
import copy
def roots(z, n):
""""
n roots of z.
"""
nthRootOfr = np.abs(z)**(1.0/n)
t = np.angle(z)
return map(lambda k: nthRootOfr*np.exp((t+2*k*np.pi)*1j/n), range(n))
def cvecl(N, loopsize=None):
""""
N random unit roots of order loopsize (e^2kpi*i/loopsize).
This choice is made for the translation to loop: translation to the right make the
image appear on the left.
If loopsize=None, then loopsize=N.
"""
if loopsize is None:
loopsize = N
unity_roots = np.array(list(roots(1.0 + 0.0j, loopsize)))
root_idxs = np.random.randint(loopsize, size=N)
X1 = unity_roots[root_idxs]
return X1
def crvec(N, D=1):
""""
Random complex vector of size N in the format a+jb
"""
rphase = 2*np.pi * np.random.rand(D, N)
return np.cos(rphase) + 1.0j * np.sin(rphase)
def norm(X):
# normalize between -1 and 1
return 2*(X-np.min(X, axis=0))/(np.max(X)-np.min(X))-1
def update_resonator_digit_async(codebooks, resonator, scene):
resonator_update = copy.copy(resonator)
for i in range(len(codebooks)):
new_code = scene
for j in range(len(codebooks)):
if i != j:
new_code = new_code*(resonator_update[j, :]**-1)
new_code = np.dot(codebooks[i].T, np.dot(
np.conj(codebooks[i]), new_code.T))
# new_code = np.dot(outer_products[i],new_code.T)
new_code = new_code / np.abs(new_code)
resonator_update[i, :] = new_code
return resonator_update
def update_resonator_digit(codebooks, resonator, scene):
resonator_update = np.ones((len(codebooks)), dtype=complex)
for i in range(len(codebooks)):
new_code = scene
for j in range(len(codebooks)):
if i != j:
new_code = new_code*(resonator[j, :]**-1)
new_code = np.dot(codebooks[i].T, np.dot(
np.conj(codebooks[i]), new_code.T))
new_code = new_code / np.abs(new_code)
resonator_update[i, :] = new_code
return resonator_update
def update_resonator_digit_async(codebooks, resonator, scene):
resonator_update = copy.copy(resonator)
for i in range(len(codebooks)):
new_code = scene
for j in range(len(codebooks)):
if i != j:
new_code = new_code*(resonator_update[j, :]**-1)
new_code = np.dot(codebooks[i].T, np.dot(
np.conj(codebooks[i]), new_code.T))
new_code = new_code / np.abs(new_code)
resonator_update[i, :] = new_code
return resonator_update
def g(x):
return x / np.abs(x)
def gen_res_digit(resonator, codebooks, max_iters, tree):
res_hist = []
res_curr = resonator
for i in range(max_iters):
res_hist.append(copy.copy(res_curr))
res_curr = update_resonator_digit_async(codebooks, res_curr, tree)
if np.mean(np.cos(np.angle(np.ndarray.flatten(res_curr))-np.angle(np.ndarray.flatten(res_hist[-1])))) > 0.99:
break
res_hist.append(copy.copy(res_curr))
return i+1, res_hist
def dot_complex(vec1, vec2):
num = np.dot(np.conj(vec1), vec2)
denom = np.linalg.norm(vec1)*np.linalg.norm(vec2)
return np.abs(num)/denom