Untitled page

def generate_supervisory_matrix(generators):

k = len(generators[0])

r = len(generators)

N = r * (k - 1)

H = [[0] * N for _ in range(N)]

for i in range(N):

for j in range(N):

if (i // r) == (j // r):

H[i][j] = generators[j % r][(i % r) + 1]

else:

H[i][j] = generators[j % r][i % r]

return H

def convolutional_encoder(message, generators):

N = len(message)

k = len(generators[0])

r = len(generators)

H = generate_supervisory_matrix(generators)

encoded_message = ''

for i in range(N):

encoded_bit = ''

for j in range(r):

out = 0

for l in range(k):

out ^= int(generators[j][l]) & int(message[i - l])

encoded_bit += str(out)

encoded_message += encoded_bit

return encoded_message

def truncated_convolutional_encoder(message, generators):

N = len(message)

k = len(generators[0])

r = len(generators)

H = generate_supervisory_matrix(generators)

encoded_message = ''

state = ['0'] * r

for i in range(N):

encoded_bit = ''

for j in range(r):

out = 0

for l in range(k):

out ^= int(generators[j][l]) & int(message[i - l])

encoded_bit += str(out)

state[j] = str(out)

encoded_message += encoded_bit

return encoded_message

def print_generator_matrix(generators):

k = len(generators[0])

r = len(generators)

for i in range(r):

print("Generator", i+1, ":", generators[i])

print()

def get_user_choice():

print("请选择编码形式:")

print("1. 卷积码编码")

print("2. 卷积码初始截短码编码")

choice = input()

return int(choice)

def get_user_message():

print("请输入信息码序列(0和1的字符串):")

message = input()

return message

def continue_encoding():

print("是否再次进行编码?(y/n)")

choice = input()

return choice.lower() == 'y'

def main():

generators = [

[1, 1, 1],

[1, 0, 1],

]

print_generator_matrix(generators)

while True:

choice = get_user_choice()

message = get_user_message()

if choice == 1:

encoded_message = convolutional_encoder(message, generators)

print("卷积码编码序列:", encoded_message)

elif choice == 2:

encoded_message = truncated_convolutional_encoder(message, generators)

print("卷积码截短码编码序列:", encoded_message)

else:

print("无效的选择")

if not continue_encoding():

break

if __name__ == '__main__':

main()

这是一个实验,使用python,实现(4,3,3)卷积码编码和(4,3,3)卷积码初始截短码编码。要求定义一个基本监督矩阵,从键盘中输入信息码序列,输出卷积码编码序列或卷积码截短码编码序列,编码过程中的生成矩阵也要打印,用户能够选择哪一种编码形式,以及是否再次进行此编码或返回选择编码形式