失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【信号处理】Python实现BPSK QPSK 8PSK 8QAM 16QAM 64QAM的调制和解调

【信号处理】Python实现BPSK QPSK 8PSK 8QAM 16QAM 64QAM的调制和解调

时间:2023-06-30 16:36:33

相关推荐

【信号处理】Python实现BPSK QPSK 8PSK 8QAM 16QAM 64QAM的调制和解调

目录

1 引言2 实现2.1 调制2.2 解调3 完整编码和解码的例子

1 引言

本文不涉及原理讲解,只提供实现方法。需要借助Commpy开源包去实现通信中的各种处理。

源码下载地址Commpy源码下载Commpy官方文档

安装方法

方法一pip install scikit-commpy方法二git clone /veeresht/CommPy.gitcd CommPypython setup.py install

2 实现

2.1 调制

import commpy as cpybits = 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 symbolelif Modulation_type=="QPSK":qpsk = cpy.PSKModem(4)symbol = qpsk.modulate(bits)return symbolelif Modulation_type=="8PSK":psk8 = cpy.PSKModem(8)symbol = psk8.modulate(bits)return symbolelif Modulation_type=="16QAM":qam16 = cpy.QAMModem(16)symbol = qam16.modulate(bits)return symbolelif Modulation_type=="64QAM":qam64 = cpy.QAMModem(64)symbol = qam64.modulate(bits)return symbol

注意8QAM,不能用Commpy实现,实现如下

import numpy as npimport matplotlib.pyplot as pltimport mathdef split_by_len(str, length):return [str[i:i+length] for i in range(0, len(str), length)]def generate_8qam(signal):SAMP = 1fc = 4qam = []t = np.arange(0, SAMP)wave = []for i in range(8):if i % 2 == 0:amp = 0.5else:amp = 1wave.append(amp * np.exp(1j * (fc * np.pi * t/SAMP + math.floor(i/2) * np.pi/2)))s_bits = split_by_len(signal, 3)for s in s_bits:qam.extend(wave[int('0b' + s, 0)])qam = np.array(qam)return qambits = np.random.binomial(n=1,p=0.5,size=(128,))if Modulation_type=="64QAM":signal = ''for i in range(bits.shape[0]):signal = signal+str(bits[i])symbol = generate_8qam(signal)return symbol

2.2 解调

# 和调制一样,需要先定义调制方法的类,再去调用解调的函数。import commpy as cpybits = np.random.binomial(n=1,p=0.5,size=(128))# Modem : QPSKmodem = mod.QAMModem(4)signal = modem.modulate(bits)modem.demodulate(signal, 'hard')

3 完整编码和解码的例子

来源Commpy 例子

# Authors: CommPy contributors# License: BSD 3-Clausefrom __future__ import division, print_function # Python 2 compatibilityimport mathimport matplotlib.pyplot as pltimport numpy as npimport commpy.channelcoding.convcode as ccimport commpy.channels as chanimport commpy.links as lkimport commpy.modulation as modimport commpy.utilities as util# =============================================================================# Convolutional Code 1: G(D) = [1+D^2, 1+D+D^2]# Standard code with rate 1/2# =============================================================================# Number of delay elements in the convolutional encodermemory = np.array(2, ndmin=1)# Generator matrixg_matrix = np.array((0o5, 0o7), ndmin=2)# Create trellis data structuretrellis1 = cc.Trellis(memory, g_matrix)# =============================================================================# Convolutional Code 1: G(D) = [1+D^2, 1+D^2+D^3]# Standard code with rate 1/2# =============================================================================# Number of delay elements in the convolutional encodermemory = np.array(3, ndmin=1)# Generator matrix (1+D^2+D^3 <-> 13 or 0o15)g_matrix = np.array((0o5, 0o15), ndmin=2)# Create trellis data structuretrellis2 = cc.Trellis(memory, g_matrix)# =============================================================================# Convolutional Code 2: G(D) = [[1, 0, 0], [0, 1, 1+D]]; F(D) = [[D, D], [1+D, 1]]# RSC with rate 2/3# =============================================================================# Number of delay elements in the convolutional encodermemory = np.array((1, 1))# Generator matrix & feedback matrixg_matrix = np.array(((1, 0, 0), (0, 1, 3)))feedback = np.array(((2, 2), (3, 1)))# Create trellis data structuretrellis3 = cc.Trellis(memory, g_matrix, feedback, 'rsc')# =============================================================================# Basic example using homemade counting and hard decoding# =============================================================================# Traceback depth of the decodertb_depth = None # Default value is 5 times the number or memoriesfor trellis in (trellis1, trellis2, trellis3):for i in range(10):# Generate random message bits to be encodedmessage_bits = np.random.randint(0, 2, 1000)# Encode message bitscoded_bits = cc.conv_encode(message_bits, trellis)# Introduce bit errors (channel)coded_bits[np.random.randint(0, 1000)] = 0coded_bits[np.random.randint(0, 1000)] = 0coded_bits[np.random.randint(0, 1000)] = 1coded_bits[np.random.randint(0, 1000)] = 1# Decode the received bitsdecoded_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 :)")# ==================================================================================================# Complete example using Commpy features and compare hard and soft demodulation. Example with code 1# ==================================================================================================# Modem : QPSKmodem = mod.QAMModem(4)# AWGN channelchannels = chan.SISOFlatChannel(None, (1 + 0j, 0j))# SNR range to testSNRs = np.arange(0, 6) + 10 * math.log10(modem.num_bits_symbol)# Modulation functiondef modulate(bits):return modem.modulate(cc.conv_encode(bits, trellis1, 'cont'))# Receiver function (no process required as there are no fading)def receiver_hard(y, h, constellation, noise_var):return modem.demodulate(y, 'hard')# Receiver function (no process required as there are no fading)def receiver_soft(y, h, constellation, noise_var):return modem.demodulate(y, 'soft', noise_var)# Decoder functiondef decoder_hard(msg):return cc.viterbi_decode(msg, trellis1)# Decoder functiondef decoder_soft(msg):return cc.viterbi_decode(msg, trellis1, decoding_type='soft')# Build model from parameterscode_rate = trellis1.k / trellis1.nmodel_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)# TestBERs_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()

如果觉得《【信号处理】Python实现BPSK QPSK 8PSK 8QAM 16QAM 64QAM的调制和解调》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。