/
Encoder.py
47 lines (41 loc) · 1.46 KB
/
Encoder.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
import numpy as np
import functions
import pyldpc as ldpc
class LDPC:
def __init__(self, N, K,matrix_H):
self.N = N
self.K = K
self.H_matrix = matrix_H
_ ,self.G_matrix = ldpc.CodingMatrix_systematic(self.H_matrix)
# 将文件内容转换为矩阵
'''
def init_LDPC_H(self,file_H):
with open(file_H) as f:
lines = f.readlines()
new_lines = []
for line in lines:
new_lines.append(list(map(int, line.split())))
H_matrix = functions.alistToNumpy(new_lines)
return H_matrix
'''
#使用生成矩阵进行编码
def encode(self,x_bits):
# coding
matrix_G = self.G_matrix.transpose()
u_coded_bits = np.mod(np.matmul(x_bits,matrix_G), 2) # G_matrix
return u_coded_bits
class Polar:
def __init__(self,N,K,design_snr_dB):
self.N = N
self.K = K
self.design_snr_dB = design_snr_dB
def encode(self,x_bits):
codesize = x_bits.shape[0]
infos = functions.polar_channel_choose(self.N, self.K, self.design_snr_dB)
# 建立指定大小的编码集合
u = np.zeros((codesize, self.N),dtype = bool)
u_coded_bits = np.zeros((codesize, self.N),dtype = bool)
u[:,infos] = x_bits
for i in range(0,codesize):
u_coded_bits[i] = functions.polar_generate(u[i])
return u_coded_bits