-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_threaded_CCSDS_Uncoded.py
More file actions
126 lines (95 loc) · 3.79 KB
/
Copy pathtest_threaded_CCSDS_Uncoded.py
File metadata and controls
126 lines (95 loc) · 3.79 KB
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
import LDPC_generator_CCSDS_256 as LDPC
import LDPC_decoder as LDPCdecoder
import scipy.io as scio
import numpy as np
import multiprocessing
def init_worker(ErrorUncoded_shared,ErrorMSA_shared,MC_shared,ArraySize_shared):
global ErrorUncoded
global ErrorMSA
global MC
global ArraySize
MC = MC_shared
ArraySize = ArraySize_shared
ErrorUncoded = np.frombuffer(ErrorUncoded_shared.get_obj(), dtype=np.int32).reshape((ArraySize, MC))
ErrorMSA = np.frombuffer(ErrorMSA_shared.get_obj(), dtype=np.int32).reshape((ArraySize, MC))
def MonteCarloRun(args):
(p, k, EbN, maxDecodeIter) = args
debugString = ""
u = np.random.randint(0,2,LDPC.LDPC_getK())
x = LDPC.LDPC_Encode(u)
debugString+=("##########################################\n")
debugString+=("### EbN0 = %.2f Iteration = %4d ###\n" % (EbN, k))
debugString+=("##########################################\n")
SNR_lin = 10**(EbN/10)
No = 1.0/SNR_lin
sigma = np.sqrt(No/2)
r = 2*x - 1
r = r + sigma*np.random.randn(r.size)
decoder = LDPCdecoder.decoder(LDPC.LDPC_getH())
decoder.setInputMSA(r,sigma)
# get Hard-Bits
w0 = r
w0[w0 >= 0] = 1
w0[w0 < 0] = 0
w0 = np.array(w0, dtype = int)
ErrorUncoded[p,k] = np.sum(w0 != x)
debugString+=("##### Bit Errors in Test: %d \n" % ErrorUncoded[p,k])
##Hard Decoders, Based on Bit-Flipping:
## (Galager's) Bit Flipping
debugString+=("##### Bit Flipping\n")
#print("Threshold: %d" % T)
#print("Maximum Iteration: %d" % lmax)
# v = np.copy(w0)
# for _ in range(1,maxDecodeIter+1):
# decoded, v = decoder.iterateMinimumSumAlgorithm()
# if(decoded):
# break
# if(decoded):
# ErrorMSA[p,k] = np.sum( v != x )
# #IterBF[p,k] = l
# debugString+=("SUCCESS | ERROR: %3d | ITER: |\n" % ( ErrorMSA[p,k]))
# else:
# ErrorMSA[p,k] = np.sum( v != x )
# #IterBF[p,k] = l
# debugString+=("FAILURE | ERROR: %3d | ITER: |\n" % ( ErrorMSA[p,k]))
#print(chr(27) + "[2J")
#print(debugString)
print("THREAD %2.2f | Initial Errors: %2d | Iteration: %3.2f" % (EbN, ErrorUncoded[p,k], k))
#print("THREAD %2.2f | PROGRESS: %3.2f == DONE!" % (EbN, 100*k/MC))
if __name__ == "__main__":
job_list = []
MC = 10**4
step = 0.5
start = -2
end = 11
EbNs = np.arange(start,(end+step),step)
ArraySize = EbNs.size
Error_shape = (ArraySize, MC)
ErrorUncoded_shared = multiprocessing.Array('i', Error_shape[0] * Error_shape[1],lock= True)
ErrorUncoded = np.frombuffer(ErrorUncoded_shared.get_obj(), dtype=np.int32).reshape(Error_shape)
np.copyto(ErrorUncoded, np.zeros(Error_shape, dtype = int))
ErrorMSA_shared = multiprocessing.Array('i', Error_shape[0] * Error_shape[1],lock= True)
ErrorMSA = np.frombuffer(ErrorMSA_shared.get_obj(), dtype=np.int32).reshape(Error_shape)
np.copyto(ErrorMSA, np.zeros(Error_shape, dtype = int))
IterBF = np.zeros(Error_shape, dtype = int)
IterSBF = np.zeros(Error_shape, dtype = int)
maxDecodeIter = 20
for p, EbN in enumerate(EbNs):
for k in range(0,MC):
job_args = (p, k, EbN, maxDecodeIter)
arg = list(job_args)
job_list.append(job_args)
pool = multiprocessing.Pool(processes=8,initializer = init_worker, initargs = (ErrorUncoded_shared,ErrorMSA_shared,MC,ArraySize))
pool.imap_unordered(MonteCarloRun, job_list)
pool.close()
pool.join()
print("######## Threads finished! ########")
K = LDPC.LDPC_getK()
N = LDPC.LDPC_getN()
scio.savemat("LDPC_out_uncoded.mat" ,{
'EbN0s': EbNs,
'ErrorUncoded': ErrorUncoded,
'MC' : MC,
'K' : K,
'N' : N
})