-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathCode.py
155 lines (141 loc) · 3.89 KB
/
mathCode.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
from numpy import *
import pdb
import pickle
def binary(code):
for i in range(code.size):
code[0,i]=code[0,i]%2
return code
def binarymatrix(A):
[n,m] = shape(A)
for i in range(n):
A[i,:]=binary(A[i,:])
return A
#Define message and generator matrix
#Generate codeword
def getCodeword(c):
ident=matrix(eye(25))
ldpc=genfromtxt("gmatrix.txt")
G=ldpc
c=binary(c*G)
return c
def getH_T():
ldpc=matrix(transpose(genfromtxt("hmatrix.txt")))
return ldpc
#Create a dictionary with syndromes as keys and error vectors as values
def generateSyndromeTable():
errors=getErrorMatrix()
syndromes=binarymatrix(errors*getH_T())
lookup={}
for i in range(shape(errors)[0]):
strsyn=''
for j in range(shape(syndromes[i])[1]):
strsyn=strsyn+str(int(syndromes[i,j]))
if strsyn in lookup:
print strsyn + ' is a duplicate syndrome - time to revise the code!'
lookup[strsyn]=errors[i,:]
print strsyn, lookup[strsyn]
f=open('syndromes.txt','w')
pickle.dump(lookup, f)
def getSyndromeTable():
f=open('syndromes.txt','r')
syndromes=pickle.load(f)
return syndromes
def generateErrorMatrix():
errors=matrix(concatenate((zeros((1,50)),eye(50)),0))
errors=matrix(zeros((1,50)))
errorlist=[]
allzeros=matrix(zeros((1,50)))
print 'starting'
#errorlist.append(errors)
#loop through 1 error
for i in range(50):
currerr=matrix(zeros((1,50)))
currerr[0,i]=1
errors=matrix(concatenate((errors,currerr),0))
#errorlist.append(errors)
#loop through 2 errors
for i in range(50):
for j in range(50-i-1):
j+=(i+1)
currerr=matrix(zeros((1,50)))
currerr[0,i]=1
currerr[0,j]=1
errors=matrix(concatenate((errors,currerr),0))
#loop through 3 errors
for i in range(50):
print i
for j in range(50-i-1):
j+=(i+1)
for k in range(50-j-1):
k+=j+1
currerr=matrix(zeros((1,50)))
currerr[0,i]=1
currerr[0,j]=1
currerr[0,k]=1
errors=matrix(concatenate((errors,currerr),0))
'''
#loop through 4 errors
for i in range(50):
print i
for j in range(50-i-1):
print j
j+=(i+1)
for k in range(50-j-1):
k+=(j+1)
for l in range(50-k-1):
l+=(k+1)
currerr=matrix(zeros((1,50)))
currerr[0,i]=1
currerr[0,j]=1
currerr[0,k]=1
currerr[0,l]=1
errors=matrix(concatenate((errors,currerr),0))'''
#pdb.set_trace()
print 'finished'
savetxt('errormat.txt',errors)
def getErrorMatrix():
errors=genfromtxt("errormat.txt")
return matrix(errors)
#Compute syndrome for received codeword
def isValidCodeword(c):
valid = c*getH_T()
valid=binary(valid)
if valid.all()==0:
return True
#print "valid codeword"
else:
return False
#print "an error has occured"
#Looks up the error vector
def getError(c):
errsyn=''
for j in range(shape(c*getH_T())[1]):
errsyn=errsyn+str(int((c*getH_T())[0,j])%2)
lookup=getSyndromeTable()
if errsyn in lookup:
errvec=lookup[errsyn]
return errvec
print 'Error vector: ',errvec
else:
print 'not found'
return zeros(shape(c))
'''
m=matrix([0,1,1,0,1,1,1,0,0,1,1,0,1,0,1,1,1,1,0,0,1,0,1,0,0])
c=getCodeword(m)
print 'Codeword:'
print c
c[0,48]+=1
c[0,49]+=1
c=binary(c)
print 'Received:'
print c
pdb.set_trace()
e=getError(c)
print 'Error:'
print e
print "The codeword after error correction is:"
print binary(e+c)
print 'Received message: ',binary(e+c)[0,25:50]
'''
#generateErrorMatrix()
#generateSyndromeTable()