微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在编码维特比解码器时遇到问题

如何解决我在编码维特比解码器时遇到问题

在 pycharm 上 我正在使用 ConvCodec4 类作为编码器和解码器 和模拟类来实验 Ber 与 data_size 相比

我原以为它会起作用,但它没有用 错误消息说:范围预计最多 3 个参数,得到 4” 但是我确定我在代码顶部给了4个数据空白

ConvCodeck4 类 '''

将 numpy 导入为 np

def 编码器(数据):

data = np.append(data,[0,0])#shift Register 

dataSize = np.shape(data)[0] #(64,)

shiftReg = [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(shiftReg[0],shiftReg[3]))
return encoded_bit

def ViterbiDecoder(encoded_bit):

ref_out = np.zeros((2,16))

ref_out[0,:] = [0,1,0]
ref_out[1,1]
dataSize = np.shape(encoded_bit)[1] 
cumdist = [0,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,2)) + dist
    tmpPrevstate=[]
    for a in range(8):  # state 
        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

'''

模拟类

将 numpy 导入为 np

将 matplotlib.pyplot 导入为 plt

将 ConvCodeck4 导入为 cc

数据大小=100

max_snr=10# 최대 SNR

min_snr=9

ber=[]

对于范围内的 snr_db(min_snr,max_snr): data=np.random.randint(0,2,data_size)

encoded_bit=cc.Encoder(data) 
real_signal = encoded_bit[0,:] 
imag_signal = encoded_bit[1,:] 

qpsk_sym = (real_signal + 1j * imag_signal) / np.sqrt(2)
ofdm_sym = np.fft.ifft(qpsk_sym) * np.sqrt(data_size) 

noise_std = 10 ** (-snr_db / 20) 
noise = np.random.randn(data_size+4)*noise_std/np.sqrt(2)+1j*np.random.randn(data_size+4)*noise_std/np.sqrt(2)
rcv_signal = np.fft.fft(ofdm_sym) / np.sqrt(data_size) + noise

real_detected_signal = np.array(((rcv_signal.real > 0) + 0)).reshape(1,data_size+4)
imag_detected_signal = np.array(((rcv_signal.imag > 0) + 0)).reshape(1,data_size+4)

snr = np.arange(min_snr,max_snr)
#(2,1024+4)
dec_input = np.vstack([real_detected_signal,imag_detected_signal])
np.shape(dec_input),np.shape(real_detected_signal)

decoded_bit = cc.ViterbiDecoder(dec_input)

打印(dec_input)

打印(real_detected_signal)

print(np.sum(np.abs(dec_input - encoded_bit))) 
print(np.sum(np.abs(data-decoded_bit))) 



num_error = np.sum(np.abs(real_signal - real_detected_signal)) / 2 + np.sum(
    np.abs(imag_signal - imag_detected_signal)) / 2
ber.append(num_error / (data_size * 2))  # BER

'''

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。