在 k=3 格子图中,我使用此代码将我的距离相加并选择最短的
tmpPrevState=[]
for a in range(4):
if tmpDist[0, 2 * a + 0] <= tmpDist[0, 2 * a + 1]:
cumDist[a] = tmpDist[0, 2 * a + 0]
tmpPrevState.append((a % 2) * 2 + 0)
else:
cumDist[a] = tmpDist[0, 2 * a + 1]
tmpPrevState.append((a % 2) * 2 + 1)
prevState.append(tmpPrevState)
我将范围设置在 0~3 之间,因为 K=3 状态图中有 4 个状态,但在 k=4 环境中,图中有 8 个状态,所以我将范围更改为 0~7,但这个解码器不能正常工作
在此之下,是我的整个编解码器代码
import numpy as np
def Encoder(data):
data = np.append(data, [0, 0, 0, 0])#shift Register
dataSize = np.shape(data)[0] #(64,)
shiftReg = [0, 0, 0, 0] #k=4
encoded_bit = np.zeros((2,dataSize)
for i in range(dataSize):
shiftReg[3] = shiftReg[2]
shiftReg[2] = shiftReg[1]
shiftReg[1] = shiftReg[0]
shiftReg[0] = data[i]
encoded_bit[0, i] = np.logical_xor(np.logical_xor(shiftReg[0], shiftReg[1]), np.logical_xor(shiftReg[2], shiftReg[3]))
encoded_bit[1, i] = np.logical_xor(np.logical_xor(shiftReg[0],shiftReg[2]), shiftReg[3])
return encoded_bit
def ViterbiDecoder(encoded_bit):
ref_out = np.zeros((2,16))
ref_out[0, :] = [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1]
ref_out[1, :] = [0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0]
dataSize = np.shape(encoded_bit)[1]
cumDist = [0, 100, 100, 100, 100, 100, 100, 100]
prevState = []
for i in range(dataSize):
tmpData = np.tile(encoded_bit[:, i].reshape(2,1),(1,16))
dist = np.sum(np.abs(tmpData - ref_out), axis=0)
tmpDist = np.tile(cumDist, (1,2)) + dist
tmpPrevState=[]
for a in range(8):
if tmpDist[0, 2 * a + 0] <= tmpDist[0, 2 * a + 1]:
cumDist[a] = tmpDist[0, 2 * a + 0]
tmpPrevState.append((a % 2) * 2 + 0)
else:
cumDist[a] = tmpDist[0, 2 * a + 1]
tmpPrevState.append((a % 2) * 2 + 1)
prevState.append(tmpPrevState)
state_index = np.argmin(cumDist)
decoded_bit=[]
for b in range(dataSize-1,-1,-1):
decoded_bit.append(int(state_index/2))
state_index = prevState[b][state_index]
data_size = np.shape(decoded_bit)[0]
decoded_bit = np.flip(decoded_bit)[0:data_size - 4]
return decoded_bit