-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
108 lines (98 loc) · 2.97 KB
/
generate.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
from numpy import *
import pdb
from mathCode import *
def generate_ldpc(rows,columns):
#create a matrix of all zeros
zeros_array = zeros((rows,columns))
ldpc = matrix(zeros_array)
#place 5 randoms 1s in each column
for i in range(columns):
for j in range(3):
randrow=int(random.random()*rows)
while ldpc[randrow,i]==1:
randrow=int(random.random()*rows)
ldpc[randrow,i]=1
numones=0.0
zeros_array = zeros((rows,1))
rowones = matrix(zeros_array)
for i in range(rows):
for j in range(columns):
if ldpc[i,j]==1:
numones+=1
rowones[i,0]+=1
print ldpc
print rowones
avgones=numones/rows
if not int(avgones)==avgones:
print 'Avg number of ones: ' + str(avgones)
print 'This is invalid.'
pdb.set_trace()
for a in range(10):
templist=[]
for i in range(rows):
if rowones[i,0]>avgones:
randcol=int(random.random()*columns)
while ldpc[i,randcol]==0:
randcol=int(random.random()*columns)
for j in range(rows):
if rowones[j,0]<avgones and (not ldpc[j,randcol]==1) and (not j==i):
templist.append(j)
if not len(templist)==0:
randrow=int(random.random()*len(templist))
ldpc[i,randcol]=0
rowones[i,0]-=1
ldpc[templist[randrow],randcol]=1
rowones[templist[randrow],0]+=1
templist=[]
savetxt('pmatrix.txt',ldpc)
print ldpc
print rowones
return ldpc
def stdForm(H):
print shape(H)
[n,q]=shape(H)
#need n*n matrix to be identity
#make sure that the diagonal has all ones
#i iterates columns, j iterates rows
rowops=[] #contains tuple of the form: (row operated on, row added to it)
for i in range(n):
if H[i,i]==0:
for j in range(n):
if H[j,i]==1 and j>i:
H[i,:]=binary(H[i,:]+H[j,:])
rowops.append((i,j))
#pdb.set_trace()
break
for j in range(n):
if H[j,i]==1 and (not j==i):
H[j,:]=binary(H[i,:]+H[j,:])
rowops.append((j,i))
return [H,rowops]
def unStdForm(G,rowops):
for op in rowops:
G[op[0],:]=binary(G[op[0],:]+G[op[1],:])
return G
'''
count=0
H=matrix([0,1])
while not(equal(H[:,0:shape(H)[1]/2],eye(shape(H)[1]/2)).all()==True):
H=generate_ldpc(25,50)
[stdH,rowops]=stdForm(H)
H=stdH
count+=1
print count
savetxt('testmatrix.txt',stdH)
pdb.set_trace()
P=stdH[:,25:50]
print P
ident=matrix(eye(25))
stdG=concatenate((transpose(P),ident),1)
print 'Std form G:'
print stdG
savetxt('gmatrix.txt',stdG)
G=unStdForm(stdG,rowops)
print 'Un-std form G:'
print G
print stdH
print binarymatrix(H*transpose(G))
savetxt('hmatrix.txt',H)'''