Skip to content

Commit 1f2fa94

Browse files
we should select the largest column as root column
1 parent 6360a8d commit 1f2fa94

10 files changed

+111
-68
lines changed

__pycache__/binmatrix.cpython-39.pyc

0 Bytes
Binary file not shown.

__pycache__/slow_lib.cpython-39.pyc

174 Bytes
Binary file not shown.

__pycache__/slow_utils.cpython-39.pyc

95 Bytes
Binary file not shown.

__pycache__/utils.cpython-39.pyc

0 Bytes
Binary file not shown.

ach_runner.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@
9595
## LLC
9696
print(" -Phase 1 (decoding) now starts.")
9797
tic = time.time()
98-
rxBits_llc, usedRootsIndex, listSizeOrder = slow_decoder(np.ones((listSize,L),dtype=int), rx_coded_symbols_llc, L, J, parityLen, messageLen, listSize, parityInvolved, whichGMatrix, windowSize)
98+
losses = np.count_nonzero(rx_coded_symbols_llc == -1, axis=0) # losses is a L-long array
99+
chosenRoot = np.argmin(losses)
100+
print("chosenRoot: " + str(chosenRoot))
101+
rx_coded_symbols_llc[:,range(L)] = rx_coded_symbols_llc[:, np.mod(np.arange(chosenRoot, chosenRoot+L),L) ]
102+
whichGMatrix[:,range(L)] = whichGMatrix[:,np.mod(np.arange(chosenRoot, chosenRoot+L),L)]
103+
whichGMatrix[range(L),:] = whichGMatrix[np.mod(np.arange(chosenRoot, chosenRoot+L),L),:]
104+
rxBits_llc, usedRootsIndex, listSizeOrder = slow_decoder(np.ones((listSize,L),dtype=int), rx_coded_symbols_llc, L, J, parityLen, messageLen, listSize, parityInvolved, whichGMatrix, windowSize, chosenRoot)
99105
toc = time.time()
100106
print(" | Time of LLC decode " + str(toc-tic))
101107
if rxBits_llc.shape[0] > K:
@@ -129,19 +135,19 @@
129135
print(f' | In phase 1, LDPC decodes {LDPC_num_matches}/{len(rx_user_codewords)} codewords. ')
130136

131137

138+
print(" -Phase 1 Done.")
132139

133140

134141

135142

136143

137144

138-
print(" -Phase 1 Done.")
139145

140146

141147
# *Corrector. PAINPOINT
142148
print(" -Phase 2 (correction) now starts.")
143149
tic = time.time()
144-
rxBits_corrected_llc= slow_corrector(np.ones((listSize,L),dtype=int),rx_coded_symbols_llc,L,J,messageLen,parityLen,listSize,parityInvolved,usedRootsIndex,whichGMatrix,windowSize,listSizeOrder)
150+
rxBits_corrected_llc= slow_corrector(np.ones((listSize,L),dtype=int),rx_coded_symbols_llc,L,J,messageLen,parityLen,listSize,parityInvolved,usedRootsIndex,whichGMatrix,windowSize,listSizeOrder,chosenRoot)
145151
toc = time.time()
146152
print(" | Time of correct " + str(toc-tic))
147153
# print(" | corrected shape: " + str( rxBits_corrected_llc.shape))

demo.py

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,59 +24,59 @@
2424

2525

2626

27-
import numpy as np
28-
import matplotlib.pyplot as pyplot
27+
# import numpy as np
28+
# import matplotlib.pyplot as pyplot
2929

3030

31-
phase1_llc_true = np.array([20, 36, 68, 100])
32-
phase1_llc_false= np.array([0, 0, 0, 0])
33-
phase2_llc_true = np.array([36, 38, 22, 0])
34-
phase2_llc_false= np.array([0, 2, 0, 0])
35-
phase1_tc_true = np.array([20, 36, 68, 100])
36-
phase1_tc_false= np.array([0, 0, 0, 0])
37-
p_es = np.array(["0.1", "0.05", "0.025", "0"])
38-
x_axis = np.arange(len(p_es))
31+
# phase1_llc_true = np.array([20, 36, 68, 100])
32+
# phase1_llc_false= np.array([0, 0, 0, 0])
33+
# phase2_llc_true = np.array([36, 38, 22, 0])
34+
# phase2_llc_false= np.array([0, 2, 0, 0])
35+
# phase1_tc_true = np.array([20, 36, 68, 100])
36+
# phase1_tc_false= np.array([0, 0, 0, 0])
37+
# p_es = np.array(["0.1", "0.05", "0.025", "0"])
38+
# x_axis = np.arange(len(p_es))
3939

40-
pyplot.axhline(y = 100, color = 'black', linestyle = 'dashed')
41-
pyplot.bar(x_axis-0.15, phase1_llc_true, 0.2)
42-
pyplot.bar(x_axis-0.15, phase2_llc_true, 0.2, bottom = phase1_llc_true)
43-
pyplot.bar(x_axis-0.15, phase1_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true)
44-
pyplot.bar(x_axis-0.15, phase2_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true + phase1_llc_false)
40+
# pyplot.axhline(y = 100, color = 'black', linestyle = 'dashed')
41+
# pyplot.bar(x_axis-0.15, phase1_llc_true, 0.2)
42+
# pyplot.bar(x_axis-0.15, phase2_llc_true, 0.2, bottom = phase1_llc_true)
43+
# pyplot.bar(x_axis-0.15, phase1_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true)
44+
# pyplot.bar(x_axis-0.15, phase2_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true + phase1_llc_false)
4545

46-
pyplot.bar(x_axis+0.15, phase1_tc_true, 0.2)
47-
pyplot.bar(x_axis+0.15, phase1_tc_false, 0.2, bottom = phase1_tc_true)
46+
# pyplot.bar(x_axis+0.15, phase1_tc_true, 0.2)
47+
# pyplot.bar(x_axis+0.15, phase1_tc_false, 0.2, bottom = phase1_tc_true)
4848

49-
pyplot.xticks(x_axis, p_es)
50-
pyplot.legend([ "K=100","Ph.1 True (LLC)", "Ph.2 True (LLC)","Ph.1 False (LLC)", "Ph.2 False (LLC)", "Ph.1 True (TreeCode)", "Ph.1 False (TreeCode)"])
51-
pyplot.xlabel("p_e")
52-
pyplot.ylim([0,110])
53-
pyplot.title("K= 100 Linked-loop code and Tree code")
54-
pyplot.show()
49+
# pyplot.xticks(x_axis, p_es)
50+
# pyplot.legend([ "K=100","Ph.1 True (LLC)", "Ph.2 True (LLC)","Ph.1 False (LLC)", "Ph.2 False (LLC)", "Ph.1 True (TreeCode)", "Ph.1 False (TreeCode)"])
51+
# pyplot.xlabel("p_e")
52+
# pyplot.ylim([0,110])
53+
# pyplot.title("K= 100 Linked-loop code and Tree code")
54+
# pyplot.show()
5555

5656

5757

58-
phase1_llc_true = np.array([28, 59, 107, 150])
59-
phase1_llc_false= np.array([0, 0, 0, 0])
60-
phase2_llc_true = np.array([37, 56, 30, 0])
61-
phase2_llc_false= np.array([4, 3, 0, 0])
62-
phase1_tc_true = np.array([28, 59, 108, 150])
63-
phase1_tc_false = np.array([1, 0, 0, 0])
58+
# phase1_llc_true = np.array([28, 59, 107, 150])
59+
# phase1_llc_false= np.array([0, 0, 0, 0])
60+
# phase2_llc_true = np.array([37, 56, 30, 0])
61+
# phase2_llc_false= np.array([4, 3, 0, 0])
62+
# phase1_tc_true = np.array([28, 59, 108, 150])
63+
# phase1_tc_false = np.array([1, 0, 0, 0])
6464

65-
pyplot.axhline(y = 150, color = 'black', linestyle = 'dashed')
66-
pyplot.bar(x_axis-0.15, phase1_llc_true, 0.2)
67-
pyplot.bar(x_axis-0.15, phase2_llc_true, 0.2, bottom = phase1_llc_true)
68-
pyplot.bar(x_axis-0.15, phase1_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true)
69-
pyplot.bar(x_axis-0.15, phase2_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true + phase1_llc_false)
65+
# pyplot.axhline(y = 150, color = 'black', linestyle = 'dashed')
66+
# pyplot.bar(x_axis-0.15, phase1_llc_true, 0.2)
67+
# pyplot.bar(x_axis-0.15, phase2_llc_true, 0.2, bottom = phase1_llc_true)
68+
# pyplot.bar(x_axis-0.15, phase1_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true)
69+
# pyplot.bar(x_axis-0.15, phase2_llc_false, 0.2, bottom = phase1_llc_true + phase2_llc_true + phase1_llc_false)
7070

71-
pyplot.bar(x_axis+0.15, phase1_tc_true, 0.2)
72-
pyplot.bar(x_axis+0.15, phase1_tc_false, 0.2, bottom = phase1_tc_true)
71+
# pyplot.bar(x_axis+0.15, phase1_tc_true, 0.2)
72+
# pyplot.bar(x_axis+0.15, phase1_tc_false, 0.2, bottom = phase1_tc_true)
7373

74-
pyplot.xticks(x_axis, p_es)
75-
pyplot.legend([ "K=150","Ph.1 True (LLC)", "Ph.2 True (LLC)","Ph.1 False (LLC)", "Ph.2 False (LLC)", "Ph.1 True (TreeCode)", "Ph.1 False (TreeCode)"])
76-
pyplot.xlabel("p_e")
77-
pyplot.ylim([0,160])
78-
pyplot.title("K= 150 Linked-loop code and Tree code")
79-
pyplot.show()
74+
# pyplot.xticks(x_axis, p_es)
75+
# pyplot.legend([ "K=150","Ph.1 True (LLC)", "Ph.2 True (LLC)","Ph.1 False (LLC)", "Ph.2 False (LLC)", "Ph.1 True (TreeCode)", "Ph.1 False (TreeCode)"])
76+
# pyplot.xlabel("p_e")
77+
# pyplot.ylim([0,160])
78+
# pyplot.title("K= 150 Linked-loop code and Tree code")
79+
# pyplot.show()
8080

8181

8282

@@ -92,4 +92,25 @@
9292

9393
# np.random.seed(2)
9494
# applyErrs = np.where(bernoulli.rvs(0.1, size=100))[0]
95-
# print(applyErrs)
95+
# print(applyErrs)
96+
97+
98+
99+
import numpy as np
100+
101+
# create a sample 2D array
102+
arr = np.array([[1, -1, 3],
103+
[1, 0, -1],
104+
[7, -1, 3]])
105+
106+
# count the number of -1 values in each column
107+
counts = np.count_nonzero(arr == -1, axis=0)
108+
109+
# print(counts)
110+
111+
chosenRoot = 5
112+
L = 16
113+
a = np.mod(np.arange(chosenRoot, chosenRoot+L),L)
114+
print(a)
115+
b = a[np.mod(np.arange(L-chosenRoot, 2*L-chosenRoot),L)]
116+
print(b)

slow_lib.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def slow_encode(tx_message, K, L, J, Pa, w, messageLen, parityLen, parityDistrib
4242

4343
return encoded_tx_message
4444

45-
def slow_decoder(sigValues, sigPos, L, J, parityLen, messageLen, listSize, parityInvolved, whichGMatrix, windowSize):
45+
def slow_decoder(sigValues, sigPos, L, J, parityLen, messageLen, listSize, parityInvolved, whichGMatrix, windowSize, chosenRoot):
4646
"""
4747
Phase 1 decoder (no erasure correction)
4848
@@ -71,53 +71,60 @@ def slow_decoder(sigValues, sigPos, L, J, parityLen, messageLen, listSize, parit
7171
a = np.binary_repr(sigPos[id_row, id_col], width=J) # print("a = " + str(a))
7272
b = np.array([int(n) for n in a] ).reshape(1,-1) # print("b = " + str(b))
7373
cs_decoded_tx_message[id_row, id_col*J:(id_col+1)*J] = b[0,:]
74-
elif id_col == 0 :
74+
elif id_col == 0:
7575
if id_row not in bad_roots:
7676
bad_roots.append(id_row)
7777

78-
bad_roots = np.array(bad_roots, dtype=int)
79-
# print("bad_roots" + str(bad_roots))
80-
81-
# Step 2: find parity consistent paths
8278
listSizeOrder = np.argsort(sigValues[:, 0])[::-1]
83-
# print("listSizeOrder: " + str(listSizeOrder))
79+
80+
81+
# Step 2: find parity consistent paths
8482
results = Parallel(n_jobs=-1)(delayed(slow_decode_deal_with_root_i)(idx, L, cs_decoded_tx_message, J, parityInvolved, whichGMatrix, messageLen, listSize, parityLen, windowSize) for idx in listSizeOrder)
8583

86-
used_index = [a for a in range(len(results)) if sum(np.sum(results[a],axis=1)) >=0 ]
84+
used_index = [a for a in range(len(results)) if sum(np.sum(results[a],axis=1)) >=0]
8785
tree_decoded_tx_message = np.empty((0,0), dtype=int)
8886
for gd_idx in used_index:
8987
tree_decoded_tx_message = np.vstack((tree_decoded_tx_message,results[gd_idx])) if tree_decoded_tx_message.size else results[gd_idx]
9088

91-
return tree_decoded_tx_message, np.concatenate((listSizeOrder[used_index],bad_roots),axis=None), listSizeOrder
89+
tree_decoded_tx_message[:,range(messageLen*L)] = tree_decoded_tx_message[:, np.mod( np.arange(messageLen*L)+(L-chosenRoot)*messageLen , messageLen*L) ]
90+
return tree_decoded_tx_message, np.concatenate((listSizeOrder[used_index], np.array(bad_roots, dtype=int)),axis=None), listSizeOrder
9291

9392

94-
def slow_corrector(sigValues, sigPos, L, J, messageLen, parityLen, listSize, parityInvolved, usedRootsIndex, whichGMatrix, windowSize, listSizeOrder):
93+
def slow_corrector(sigValues, sigPos, L, J, messageLen, parityLen, listSize, parityInvolved, usedRootsIndex, whichGMatrix, windowSize, listSizeOrder, chosenRoot):
9594
cs_decoded_tx_message = np.zeros( (listSize, L*J) )
9695
for id_row in range(sigPos.shape[0]):
9796
for id_col in range(sigPos.shape[1]):
9897
a = np.binary_repr(sigPos[id_row][id_col], width=J)
9998
b = np.array([int(n) for n in a] ).reshape(1,-1)
10099
cs_decoded_tx_message[ id_row, id_col*J: (id_col+1)*J ] = b[0, 0:J]
101-
100+
102101
listSizeOrder_remained = [x for x in listSizeOrder if x not in usedRootsIndex] # exclude used roots.
103102
tree_decoded_tx_message = np.empty(shape=(0,0))
104103
targetingSections = np.mod(np.arange(1,L+1),L)
105104

106-
for i, idx in zip(listSizeOrder_remained, tqdm(range(len(listSizeOrder_remained)))):
107-
# if cs_decoded_tx_message[i,0] == -1: print("我是-1 !!!!")
105+
for i, idx in zip(listSizeOrder_remained,
106+
# tqdm(
107+
range(len(listSizeOrder_remained))
108+
# )
109+
):
110+
assert cs_decoded_tx_message[i,0] != -1
108111
Paths = np.array([[i]])
109112
for l in targetingSections:
110113
# print( "Target section: " + str(l) + " | No. of paths: " + str(Paths.shape[0]) + " | How many contains -1: " + str(sum([1 for Path in Paths if np.any(Path<0)])) )
111114
if Paths.shape[0] == 0:
112-
print("-------"); break
115+
break
113116
newAll=np.empty( shape=(0,0))
114117

115-
if l!=0 : # We still need to enlarge lenth of Paths.
116-
survivePaths = Parallel(n_jobs=-1)(delayed(slow_correct_each_section_and_path)(l, j, Paths, cs_decoded_tx_message, J, parityInvolved, whichGMatrix, listSize, messageLen, parityLen, L, windowSize) for j in range(Paths.shape[0]))
118+
if l != 0 : # We still need to enlarge lenth of Paths.
119+
survivePaths = Parallel(n_jobs=-1)(delayed(slow_correct_each_section_and_path)(l, j, Paths, cs_decoded_tx_message, J,
120+
parityInvolved, whichGMatrix, listSize,
121+
messageLen, parityLen, L, windowSize)
122+
for j in range(Paths.shape[0]))
117123
for survivePath in survivePaths:
118124
if survivePath.size:
119125
newAll = np.vstack((newAll, survivePath)) if newAll.size else survivePath
120126
Paths = newAll
127+
121128
else: # We dont enlarge length of Paths anymore
122129
PathsUpdated = np.empty( shape=(0,0))
123130
for j in range(Paths.shape[0]):
@@ -155,6 +162,7 @@ def slow_corrector(sigValues, sigPos, L, J, messageLen, parityLen, listSize, par
155162
if recovered_message != np.array([], dtype= int).reshape(1,-1):
156163
tree_decoded_tx_message = np.vstack((tree_decoded_tx_message, recovered_message)) if tree_decoded_tx_message.size else recovered_message
157164

165+
tree_decoded_tx_message[:,range(messageLen*L)] = tree_decoded_tx_message[:, np.mod( np.arange(messageLen*L)+(L-chosenRoot)*messageLen , messageLen*L) ]
158166
return tree_decoded_tx_message
159167

160168

slow_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
assert windowSize > 0
2525
Pa = L*parityLen # Total number of parity check bits, in this case Pa=w=128
2626
N = int((30000/2**J)*M) # number of channel uses (real d.o.f)
27-
numAMPIter = 10 # number of AMP iterations desired
27+
numAMPIter = 2 # number of AMP iterations desired
2828
listSize = K + int(np.ceil(K/20)) # list size retained per section after AMP converges
2929
sigma_n = 1 # AWGN noise standard deviation, hence set to 1. "n" stands for "normal"
3030
EbNo = 10**(SNR/10) # Eb/No

slow_utils.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import numpy as np
22
from utils import *
3+
from binmatrix import *
34

45
def slow_compute_permissible_parity(Path,cs_decoded_tx_message,J, parityInvolved, toCheck, whichGMatrix, parityLen, messageLen):
56
# If path length = 2, then we wanna have parity for section 2. toCheck = 2
67
parityDist = parityInvolved[:,toCheck].reshape(1,-1)[0]
7-
# print("----------parityDist: " + str(parityDist))
8+
# print("----------parityDist: " + str(parityDist), "toCheck: " + str(toCheck))
89
deciders = np.nonzero(parityDist)[0] # w(decider), where decider \in deciders, decide p(toCheck) collectively
910
focusPath = Path[0]
1011

1112
Parity_computed = np.zeros(parityLen, dtype=int)
1213
for decider in deciders: # l labels the sections we gonna check to fix toCheck's parities
1314
if focusPath[decider] == -1:
1415
return -1 * np.ones((1,parityLen),dtype=int)
16+
assert whichGMatrix[decider][toCheck] != -1
1517
gen_mat = matrix_repo(dim=messageLen)[ whichGMatrix[decider][toCheck] ]
1618
Parity_computed = Parity_computed + np.matmul( cs_decoded_tx_message[focusPath[decider], decider*J : decider*J+messageLen], gen_mat)
1719
Parity_computed = np.mod(Parity_computed, 2)
@@ -82,8 +84,12 @@ def slow_parity_check(Parity_computed,Path,k,cs_decoded_tx_message,J,messageLen,
8284
theLostPart = np.mod(np.matmul(np.mod(minuend - subtrahend,2),gen_binmat_inv),2)
8385
solutions = np.vstack((solutions,theLostPart)) if solutions.size else theLostPart
8486

85-
if np.all(solutions == solutions[0]): return True
86-
else: return False
87+
if np.all(solutions == solutions[0]):
88+
# print("對的")
89+
return True
90+
else:
91+
# print("不對")
92+
return False
8793

8894
else: # Path is already full
8995
lostSection = losts[0] if len(losts)>0 else -1
@@ -169,7 +175,7 @@ def slow_decode_deal_with_root_i(i,L,cs_decoded_tx_message, J,parityInvolved, wh
169175
# Every i is a root.
170176
# If section ZERO contains -1, then this root is defective
171177
if cs_decoded_tx_message[i,0] == -1:
172-
# print("i= " + str(i)+" 是-1")
178+
print("i= " + str(i)+" 是-1")
173179
return -1*np.ones((1,messageLen * L), dtype=int)
174180

175181
# This root is not defective.
@@ -248,3 +254,4 @@ def slow_correct_each_section_and_path(l, j, Paths, cs_decoded_tx_message, J, pa
248254
return new
249255

250256

257+

utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,5 @@ def check_phase_1(txBits, rxBits, name):
448448
txBits_remained_llc = np.vstack( (txBits_remained_llc, txBits[i,:]) ) if txBits_remained_llc.size else txBits[i,:]
449449
print(" | In phase 1, " + str(name) + " decodes " + str(thisIter) + " true message out of " +str(rxBits.shape[0]))
450450
# print(" - " + str(name) + " Phase 1 is done.")
451-
return txBits_remained_llc
451+
return txBits_remained_llc
452+

0 commit comments

Comments
 (0)