-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.py
144 lines (107 loc) · 3.4 KB
/
tools.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
import numpy as np
import torch
def bi2str(raw_bits):
'''bit sequence 2 string'''
Len = raw_bits.shape[0]
bits_list = [str(raw_bits[i]) for i in range(Len)]
str_bits = ''.join(bits_list)
return str_bits
def str2bi(str_bits):
'''string 2 bit sequence(numpy)'''
Len = len(str_bits)
bit_seq = list(str_bits)
bit_seq = [int(bit_seq[i]) for i in range(Len)]
return np.array(bit_seq)
def ind2sub(id_2d, k_best):
'''Python version of Matlab function: ind2sub'''
Len = id_2d.shape[0]
id_new, id_old = np.zeros(Len,dtype = int), np.zeros(Len,dtype = int)
for idx in range(Len):
id_old[idx] = id_2d[idx] % k_best
id_new[idx] = int(id_2d[idx]/ k_best)
return id_new, id_old
'''
def ind2sub(id_2d, keepnode, k_best):
#Python version of Matlab function: ind2sub
Len = id_2d.shape[0]
id_new, id_old = np.zeros(Len,dtype = int), np.zeros(Len,dtype = int)
for idx in range(Len):
id_new[idx] = id_2d[idx] % k_best
id_old[idx] = int(id_2d[idx]/ k_best)
return id_old, id_new
'''
def de2bi(num,K):
bit_seq = [0 for _ in range(K)]
for i in range(K):
s = num%2
num = num//2
bit_seq[K-i-1] = s
if num == 0:
break
return bit_seq
def bi2de(array):
k = array.shape[0]
num = 0
for j in range(k):
num += array[j]*2**(k-j-1)
return num
def map_fun(I):
de2bin_map = torch.tensor([[0], [1]])
for i in range(I-1):
de2bin_map_top = de2bin_map.clone()
de2bin_map_down = de2bin_map.clone()
de2bin_map_top = torch.cat((torch.zeros(2**(i+1), 1), de2bin_map_top), 1)
de2bin_map_down = torch.cat((torch.ones(2**(i+1), 1), de2bin_map_down), 1)
de2bin_map = torch.cat((de2bin_map_top, de2bin_map_down), 0)
de2bin_map = de2bin_map.numpy()
return de2bin_map
############# CRC Part #############
def XOR(str1, str2):
ans = ''
if str1[0] == '0':
return '0', str1[1:]
else:
for i in range(len(str1)):
if (str1[i] == '0' and str2[i] == '0'):
ans = ans + '0'
elif (str1[i] == '1' and str2[i] == '1'):
ans = ans + '0'
else:
ans = ans + '1'
return '1', ans[1:]
def CRC_Encoding(str1,str2):
lenght = len(str2)
str3 = str1 + '0'*(lenght-1)
ans = ''
yus = str3[0:lenght]
for i in range(len(str1)):
str4,yus = XOR(yus, str2)
ans = ans+str4
if i == len(str1)-1:
break
else:
yus = yus+str3[i+lenght]
ans = str1 + yus
return ans
def CRC_Decoding(str1,str2):
lenght = len(str2)
str3 = str1 + '0'*(lenght-1)
ans = ''
yus = str3[0:lenght]
for i in range(len(str1)):
str4,yus = XOR(yus, str2)
ans = ans+str4
if i == len(str1)-1:
break
else:
yus = yus+str3[i+lenght]
return yus == '0'*len(yus)
if __name__ == "__main__":
raw_bits = "10011000000000000000011"
crc8 = "100000111"
codeword = CRC_Encoding(raw_bits,crc8)
print(codeword)
code_list = list(codeword)
code_list[10] = str((1+int(code_list[10]))%2)
codeword = ''.join(code_list)
print(CRC_Decoding(codeword,crc8))