-
Notifications
You must be signed in to change notification settings - Fork 144
/
wincnn.py
156 lines (118 loc) · 3.49 KB
/
wincnn.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import print_function
from sympy import symbols, Matrix, Poly, zeros, eye, Indexed, simplify, IndexedBase, init_printing, pprint
from operator import mul
from functools import reduce
def At(a,m,n):
return Matrix(m, n, lambda i,j: a[i]**j)
def A(a,m,n):
return At(a, m-1, n).row_insert(m-1, Matrix(1, n, lambda i,j: 1 if j==n-1 else 0))
def T(a,n):
return Matrix(Matrix.eye(n).col_insert(n, Matrix(n, 1, lambda i,j: -a[i]**n)))
def Lx(a,n):
x=symbols('x')
return Matrix(n, 1, lambda i,j: Poly((reduce(mul, ((x-a[k] if k!=i else 1) for k in range(0,n)), 1)).expand(basic=True), x))
def F(a,n):
return Matrix(n, 1, lambda i,j: reduce(mul, ((a[i]-a[k] if k!=i else 1) for k in range(0,n)), 1))
def Fdiag(a,n):
f=F(a,n)
return Matrix(n, n, lambda i,j: (f[i,0] if i==j else 0))
def FdiagPlus1(a,n):
f = Fdiag(a,n-1)
f = f.col_insert(n-1, zeros(n-1,1))
f = f.row_insert(n-1, Matrix(1,n, lambda i,j: (1 if j==n-1 else 0)))
return f
def L(a,n):
lx = Lx(a,n)
f = F(a, n)
return Matrix(n, n, lambda i,j: lx[i, 0].nth(j)/f[i]).T
def Bt(a,n):
return L(a,n)*T(a,n)
def B(a,n):
return Bt(a,n-1).row_insert(n-1, Matrix(1, n, lambda i,j: 1 if j==n-1 else 0))
FractionsInG=0
FractionsInA=1
FractionsInB=2
FractionsInF=3
def cookToomFilter(a,n,r,fractionsIn=FractionsInG):
alpha = n+r-1
f = FdiagPlus1(a,alpha)
if f[0,0] < 0:
f[0,:] *= -1
if fractionsIn == FractionsInG:
AT = A(a,alpha,n).T
G = (A(a,alpha,r).T*f**(-1)).T
BT = f * B(a,alpha).T
elif fractionsIn == FractionsInA:
BT = f * B(a,alpha).T
G = A(a,alpha,r)
AT = (A(a,alpha,n)).T*f**(-1)
elif fractionsIn == FractionsInB:
AT = A(a,alpha,n).T
G = A(a,alpha,r)
BT = B(a,alpha).T
else:
AT = A(a,alpha,n).T
G = A(a,alpha,r)
BT = f * B(a,alpha).T
return (AT,G,BT,f)
def filterVerify(n, r, AT, G, BT):
alpha = n+r-1
di = IndexedBase('d')
gi = IndexedBase('g')
d = Matrix(alpha, 1, lambda i,j: di[i])
g = Matrix(r, 1, lambda i,j: gi[i])
V = BT*d
U = G*g
M = U.multiply_elementwise(V)
Y = simplify(AT*M)
return Y
def convolutionVerify(n, r, B, G, A):
di = IndexedBase('d')
gi = IndexedBase('g')
d = Matrix(n, 1, lambda i,j: di[i])
g = Matrix(r, 1, lambda i,j: gi[i])
V = A*d
U = G*g
M = U.multiply_elementwise(V)
Y = simplify(B*M)
return Y
def showCookToomFilter(a,n,r,fractionsIn=FractionsInG):
AT,G,BT,f = cookToomFilter(a,n,r,fractionsIn)
print ("AT = ")
pprint(AT)
print ("")
print ("G = ")
pprint(G)
print ("")
print ("BT = ")
pprint(BT)
print ("")
if fractionsIn != FractionsInF:
print ("FIR filter: AT*((G*g)(BT*d)) =")
pprint(filterVerify(n,r,AT,G,BT))
print ("")
if fractionsIn == FractionsInF:
print ("fractions = ")
pprint(f)
print ("")
def showCookToomConvolution(a,n,r,fractionsIn=FractionsInG):
AT,G,BT,f = cookToomFilter(a,n,r,fractionsIn)
B = BT.transpose()
A = AT.transpose()
print ("A = ")
pprint(A)
print ("")
print ("G = ")
pprint(G)
print ("")
print ("B = ")
pprint(B)
print ("")
if fractionsIn != FractionsInF:
print ("Linear Convolution: B*((G*g)(A*d)) =")
pprint(convolutionVerify(n,r,B,G,A))
print ("")
if fractionsIn == FractionsInF:
print ("fractions = ")
pprint(f)
print ("")