在 pycharm 上,我使用 ConvCodec4 类作为编码器和解码器以及模拟类来实验 Ber 与 data_size
我原以为它可以工作,但它发出错误消息说:range expected at most 3 arguments, got 4" 但我确定我在代码顶部给出了 4 个数据空白
ConvCodeck4 类 '''
将 numpy 导入为 np
def 编码器(数据):
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(shiftReg[0], np.logical_xor(shiftReg[2], shiftReg[3]))
return encoded_bit
def ViterbiDecoder(encoded_bit):
ref_out = np.zeros((2,16))
ref_out[0, :] = [0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0]
ref_out[1, :] = [0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1]
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): # 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,-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
伯=[]
对于范围内的 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
'''