This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample0.py
171 lines (124 loc) · 4.49 KB
/
sample0.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
r'''Implement traditional sum-product decoder and evaluate performance '''
import sum_product as sp
import numpy as np
import torch
from torch.autograd import Variable
import time
cuda = False
seed = 1
# model
codedir = 'data/PEGREG504x1008'
decoding_iteration = 20 # number of iteration for decoding
all_zero_codeword = False
# SNR range for performance evaluation
snr_from = 1
snr_to = 5
snr_step = 0.5
def isnan(x):
return x != x
def dtype(tensor):
return tensor.cuda() if cuda else tensor
class NeuralSumProductModel(torch.nn.Module):
def __init__(self, code, num_of_iteration):
super(NeuralSumProductModel, self).__init__()
self._spa = sp.SumProductAlgorithm(code)
self._code = code
self.num_of_iteration = num_of_iteration
def forward(self, llr):
spa = self._spa
scattered_llr = spa.scatter(llr)
extrinsic_value = Variable(dtype(torch.zeros(scattered_llr.size())))
output = []
for i in range(self.num_of_iteration):
# variable node process
a_priori_value = spa.variable_node_process(extrinsic_value)
# check node process
extrinsic_value = spa.check_node_process(
a_priori_value + scattered_llr)
# Temporary Decision
temporary_output = spa.gather(extrinsic_value) + llr
output.append(temporary_output)
return output
def bin_to_bip(x):
return -2 * x + 1
def bip_to_bin(x):
return -0.5 * (x - 1)
def snr_to_var(snr, rate):
return 0.5 * (1.0 / pow(10.0, snr / 10.0)) / rate
def calc_llr(x, var):
return 2 * x / var
def eval_accuracy(code, model):
decoder = model
minibatch_size = 100
code_length = code.code_length
infoword_length = code.dim
code_rate = code.rate
mean = torch.zeros(minibatch_size, code_length)
print('snr ser bler serr symbols blerr blocks rtime ptime')
for snr in np.arange(snr_from, snr_to, snr_step):
variance = snr_to_var(snr, code_rate)
stddev = variance**0.5
block_num = 0
symbol_num = 0
block_error_num = 0
symbol_error_num = 0
start_rtime = time.time()
start_ptime = time.process_time()
while True:
if all_zero_codeword:
codeword = dtype(torch.zeros([minibatch_size, code_length]))
else:
message = dtype(
torch.Tensor(
minibatch_size,
infoword_length).random_(0, 2))
codeword = code.encode(message)
transmitted_signal = bin_to_bip(codeword)
channel_noise = dtype(torch.normal(mean, stddev))
received_signal = transmitted_signal + channel_noise
llr = calc_llr(received_signal, variance)
soft_output = decoder(Variable(llr))[-1].data
estimated_word = bip_to_bin(torch.sign(soft_output))
error = torch.sum(torch.abs(codeword - estimated_word) > 0.5,
dim=1)
block_error_num += torch.sum(error > 0)
symbol_error_num += torch.sum(error)
block_num += minibatch_size
symbol_num += minibatch_size * code_length
if symbol_error_num > 5000:
break
elapsed_rtime = time.time() - start_rtime
elapsed_ptime = time.process_time() - start_ptime
symbol_error_rate = symbol_error_num / float(symbol_num)
block_error_rate = block_error_num / float(block_num)
s = '{snr} {ser:.2e} {bler:.2e} {serr} {symbols} {blerr} {blocks}'\
'{rtime:9.2e} {ptime:9.2e}'
s = s.format(
snr=snr,
ser=symbol_error_rate, bler=block_error_rate,
serr=symbol_error_num, symbols=symbol_num,
blerr=block_error_num, blocks=block_num,
rtime=elapsed_rtime, ptime=elapsed_ptime
)
print(s)
def main(cuda_=True):
global cuda
cuda = cuda_
torch.manual_seed(seed)
if cuda:
torch.cuda.manual_seed(seed)
print('Prepare matrices')
use_genmat = not all_zero_codeword
code = sp.Code(codedir, use_genmat)
code.save(codedir)
if cuda:
code.cuda()
print(' codelength = {}'.format(code.code_length))
print(' rate = {}'.format(code.rate))
model = NeuralSumProductModel(code, decoding_iteration)
if cuda:
model.cuda()
print('eval decoding performance')
eval_accuracy(code, model)
if __name__ == '__main__':
main(False)