1 引言
本文不涉及原理讲解,只提供实现方法。需要借助Commpy开源包去实现通信中的各种处理。
安装方法
方法一
pip install scikit-commpy
方法二
git clone https://github.com/veeresht/CommPy.git
cd CommPy
python setup.py install
2 实现
2.1 调制
import commpy as cpy
bits = np.random.binomial(n=1,p=0.5,size=(128))
Modulation_type ="BPSK"
if Modulation_type=="BPSK":
bpsk = cpy.PSKModem(2)
symbol = bpsk.modulate(bits)
return symbol
elif Modulation_type=="QPSK":
qpsk = cpy.PSKModem(4)
symbol = qpsk.modulate(bits)
return symbol
elif Modulation_type=="8PSK":
psk8 = cpy.PSKModem(8)
symbol = psk8.modulate(bits)
return symbol
elif Modulation_type=="8QAM":
qam8 = cpy.QAMModem(8)
symbol = qam8.modulate(bits)
return symbol
elif Modulation_type=="16QAM":
qam16 = cpy.QAMModem(16)
symbol = qam16.modulate(bits)
return symbol
elif Modulation_type=="64QAM":
qam64 = cpy.QAMModem(64)
symbol = qam64.modulate(bits)
return symbol
2.2 解调
import commpy as cpy
bits = np.random.binomial(n=1,p=0.5,size=(128))
modem = mod.QAMModem(4)
signal = modem.modulate(bits)
modem.demodulate(signal, 'hard')
3 完整编码和解码的例子
来源Commpy 例子
from __future__ import division, print_function
import math
import matplotlib.pyplot as plt
import numpy as np
import commpy.channelcoding.convcode as cc
import commpy.channels as chan
import commpy.links as lk
import commpy.modulation as mod
import commpy.utilities as util
memory = np.array(2, ndmin=1)
g_matrix = np.array((0o5, 0o7), ndmin=2)
trellis1 = cc.Trellis(memory, g_matrix)
memory = np.array(3, ndmin=1)
g_matrix = np.array((0o5, 0o15), ndmin=2)
trellis2 = cc.Trellis(memory, g_matrix)
memory = np.array((1, 1))
g_matrix = np.array(((1, 0, 0), (0, 1, 3)))
feedback = np.array(((2, 2), (3, 1)))
trellis3 = cc.Trellis(memory, g_matrix, feedback, 'rsc')
tb_depth = None
for trellis in (trellis1, trellis2, trellis3):
for i in range(10):
message_bits = np.random.randint(0, 2, 1000)
coded_bits = cc.conv_encode(message_bits, trellis)
coded_bits[np.random.randint(0, 1000)] = 0
coded_bits[np.random.randint(0, 1000)] = 0
coded_bits[np.random.randint(0, 1000)] = 1
coded_bits[np.random.randint(0, 1000)] = 1
decoded_bits = cc.viterbi_decode(coded_bits.astype(float), trellis, tb_depth)
num_bit_errors = util.hamming_dist(message_bits, decoded_bits[:len(message_bits)])
if num_bit_errors != 0:
print(num_bit_errors, "Bit Errors found!")
elif i == 9:
print("No Bit Errors :)")
modem = mod.QAMModem(4)
channels = chan.SISOFlatChannel(None, (1 + 0j, 0j))
SNRs = np.arange(0, 6) + 10 * math.log10(modem.num_bits_symbol)
def modulate(bits):
return modem.modulate(cc.conv_encode(bits, trellis1, 'cont'))
def receiver_hard(y, h, constellation, noise_var):
return modem.demodulate(y, 'hard')
def receiver_soft(y, h, constellation, noise_var):
return modem.demodulate(y, 'soft', noise_var)
def decoder_hard(msg):
return cc.viterbi_decode(msg, trellis1)
def decoder_soft(msg):
return cc.viterbi_decode(msg, trellis1, decoding_type='soft')
code_rate = trellis1.k / trellis1.n
model_hard = lk.LinkModel(modulate, channels, receiver_hard,
modem.num_bits_symbol, modem.constellation, modem.Es,
decoder_hard, code_rate)
model_soft = lk.LinkModel(modulate, channels, receiver_soft,
modem.num_bits_symbol, modem.constellation, modem.Es,
decoder_soft, code_rate)
BERs_hard = model_hard.link_performance(SNRs, 10000, 600, 5000, code_rate)
BERs_soft = model_soft.link_performance(SNRs, 10000, 600, 5000, code_rate)
plt.semilogy(SNRs, BERs_hard, 'o-', SNRs, BERs_soft, 'o-')
plt.grid()
plt.xlabel('Signal to Noise Ration (dB)')
plt.ylabel('Bit Error Rate')
plt.legend(('Hard demodulation', 'Soft demodulation'))
plt.show()
|