-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChannelCoding.py
186 lines (134 loc) · 5.62 KB
/
ChannelCoding.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from math import *
import numpy as np
# input: bits array
# output: bits array
# rate: "1/2","3/4","2/3"
def BCCEncoder(input, rate):
output = []
ShiftRegister = [0, 0, 0, 0, 0, 0, 0]
BitStreamLen = len(input)
#if rate == "1/2": (default)
MaskLen = 1
PunctureMaskA = [1]
PunctureMaskB = [1]
if rate == "3/4":
MaskLen = 8
PunctureMaskA = [1, 1, 0, 1, 1, 0, 1, 1, 0]
PunctureMaskB = [1, 0, 1, 1, 0, 1, 1, 0, 1]
if rate == "2/3":
MaskLen = 6
PunctureMaskA = [1, 1, 1, 1, 1, 1]
PunctureMaskB = [1, 0, 1, 0, 1, 0]
MaskCounter = 0
for i in range(BitStreamLen):
for j in range(6, 0, -1):
ShiftRegister[j] = ShiftRegister[j-1]
ShiftRegister[0] = input[i]
a = ShiftRegister[0] ^ ShiftRegister[2] ^ ShiftRegister[3] ^ ShiftRegister[5] ^ ShiftRegister[6]
b = ShiftRegister[0] ^ ShiftRegister[1] ^ ShiftRegister[2] ^ ShiftRegister[3] ^ ShiftRegister[6]
if PunctureMaskA[MaskCounter] == 1:
output.append(a)
if PunctureMaskB[MaskCounter] == 1:
output.append(b)
MaskCounter = (MaskCounter+1) % MaskLen
return output
# input: LLR array
# output: LLR array
# rate: "1/2","3/4","2/3"
def BCCDecoder(input, rate):
ret = 0
alpha = 1 - 1e-10
alpha = 1
inputA = []
inputB = []
#if rate == "1/2": (default)
BitStreamLen = int(len(input)/2)
MaskLen = 1
PunctureMaskA = [1]
PunctureMaskB = [1]
if rate == "3/4":
BitStreamLen = int(len(input)*3/4)
MaskLen = 8
PunctureMaskA = [1, 1, 0, 1, 1, 0, 1, 1, 0]
PunctureMaskB = [1, 0, 1, 1, 0, 1, 1, 0, 1]
if rate == "2/3":
BitStreamLen = int(len(input)*2/3)
MaskLen = 6
PunctureMaskA = [1, 1, 1, 1, 1, 1]
PunctureMaskB = [1, 0, 1, 0, 1, 0]
# interpolate the puncture
MaskCounter = 0
currentIndex = 0
for i in range(BitStreamLen):
if PunctureMaskA[MaskCounter] == 1:
inputA.append(input[currentIndex])
currentIndex = currentIndex + 1
else:
inputA.append(0) # LLR = log2(1)
if PunctureMaskB[MaskCounter] == 1:
inputB.append(input[currentIndex])
currentIndex = currentIndex + 1
else:
inputB.append(0) # LLR = log2(1)
MaskCounter = (MaskCounter+1) % MaskLen
output = np.zeros(BitStreamLen, dtype=float)
output[-6:] = 999 # the last 6 bits have to be tail (six "0"s)
outputForward = np.zeros(BitStreamLen, dtype=float)
outputForward[-6:] = 999
outputBackward = np.zeros(BitStreamLen, dtype=float)
outputBackward[-6:] = 999
for repeat in range(100):
# forward propagation
ShiftRegister = [999, 999, 999, 999, 999, 999, 999]
for i in range(BitStreamLen - 6): # the last 6 bits have to be tail (six "0")
for j in range(6, 0, -1):
ShiftRegister[j] = ShiftRegister[j - 1]
# a = 2*atanh(tanh(ShiftRegister[2]/2)*tanh(ShiftRegister[3]/2)*tanh(
# ShiftRegister[5]/2)*tanh(ShiftRegister[6]/2)*tanh(inputA[i]/2)*alpha)
# b = 2*atanh(tanh(ShiftRegister[1]/2)*tanh(ShiftRegister[2]/2)*tanh(
# ShiftRegister[3]/2)*tanh(ShiftRegister[6]/2)*tanh(inputB[i]/2)*alpha)
a = atanh(tanh(ShiftRegister[2])*tanh(ShiftRegister[3])*tanh(
ShiftRegister[5])*tanh(ShiftRegister[6])*tanh(inputA[i])*alpha)
b = atanh(tanh(ShiftRegister[1])*tanh(ShiftRegister[2])*tanh(
ShiftRegister[3])*tanh(ShiftRegister[6])*tanh(inputB[i])*alpha)
LLR = a + b
ShiftRegister[0] = outputBackward[i] + LLR
outputForward[i] = outputBackward[i] + LLR
# backward progagation
ShiftRegister = [999, 999, 999, 999, 999, 999, 999]
for i in range(BitStreamLen - 7, -1, -1): # the last 6 bits have to be tail (six "0")
for j in range(0, 6, 1):
ShiftRegister[j] = ShiftRegister[j + 1]
# a = 2*atanh(tanh(ShiftRegister[0]/2)*tanh(ShiftRegister[2]/2)*tanh(
# ShiftRegister[3]/2)*tanh(ShiftRegister[5]/2)*tanh(inputA[i + 6]/2)*alpha)
# b = 2*atanh(tanh(ShiftRegister[0]/2)*tanh(ShiftRegister[1]/2)*tanh(
# ShiftRegister[2]/2)*tanh(ShiftRegister[3]/2)*tanh(inputB[i + 6]/2)*alpha)
a = atanh(tanh(ShiftRegister[0])*tanh(ShiftRegister[2])*tanh(
ShiftRegister[3])*tanh(ShiftRegister[5])*tanh(inputA[i + 6])*alpha)
b = atanh(tanh(ShiftRegister[0])*tanh(ShiftRegister[1])*tanh(
ShiftRegister[2])*tanh(ShiftRegister[3])*tanh(inputB[i + 6])*alpha)
LLR = a + b
ShiftRegister[6] = outputForward[i] + LLR
outputBackward[i] = outputForward[i] + LLR
# combine the forward and backward
for i in range(BitStreamLen):
output[i] = outputForward[i] + outputBackward[i]
return output
if __name__ == "__main__":
input = [1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
output = []
BCCEncoder(input, output, "1/2")
input.clear()
for i in range(len(output)):
if output[i] == 0:
input.append(0.1)
else:
input.append(-0.1)
BCCDecoder(input, output, "1/2")
for i in range(len(output)):
if output[i] >= 0:
output[i] = 0
else:
output[i] = 1
print(output)