python实现QAM调制后的LS信道估计误码率计算
时间: 2023-07-07 07:32:14 浏览: 106
16qam调制解调+各种信道误码率曲线
5星 · 资源好评率100%
以下是一个简单的 Python 代码,用于实现 QAM 调制后的最小二乘(LS)信道估计并计算误码率:
```python
import numpy as np
from scipy.linalg import inv
import matplotlib.pyplot as plt
# 生成随机发送数据
num_bits = 10000
msg = np.random.randint(2, size=num_bits)
# QAM 调制
M = 16
k = int(np.log2(M))
msg = msg[:num_bits - num_bits % k]
msg_grouped = np.reshape(msg, [len(msg)//k, k])
msg_decimal = np.zeros(len(msg)//k, dtype=int)
for i in range(k):
msg_decimal += msg_grouped[:, i]*(2**(k-i-1))
modulated_signal = np.zeros(len(msg)//k, dtype=complex)
for i in range(len(modulated_signal)):
modulated_signal[i] = (2*msg_decimal[i] - (M - 1))*np.exp(1j*np.pi*(2*np.random.randint(2) - 1)/4)/np.sqrt(10)
# 信道系数
h = np.array([1, 0.5, 0.3])
# 添加高斯白噪声
EbN0_dB = 10
EbN0 = 10**(EbN0_dB/10.0)
noise_std = np.sqrt(1.0/(2.0*EbN0))
noise = noise_std*np.random.randn(len(modulated_signal), len(h)) + 1j*noise_std*np.random.randn(len(modulated_signal), len(h))
received_signal = np.zeros((len(modulated_signal), len(h)), dtype=complex)
for i in range(len(h)):
received_signal[:, i] = modulated_signal*h[i] + noise[:, i]
# LS 信道估计
H_LS = inv(modulated_signal.reshape(-1, 1).dot(modulated_signal.reshape(1, -1))).dot(modulated_signal.reshape(-1, 1)).dot(received_signal)
# 解调
constellation = np.array([(-3+3j), (-3+1j), (-3-3j), (-3-1j), (-1+3j), (-1+1j), (-1-3j), (-1-1j), (3+3j), (3+1j), (3-3j), (3-1j), (1+3j), (1+1j), (1-3j), (1-1j)]) / np.sqrt(10)
demodulated_signal = np.zeros(len(received_signal), dtype=int)
for i in range(len(received_signal)):
distance = np.abs(received_signal[i, :] - constellation)
demodulated_signal[i] = np.argmin(distance)
# 误码率计算
num_errors = np.sum(msg_decimal != demodulated_signal)
BER = num_errors/len(msg_decimal)
# 输出误码率
print('误码率:', BER)
# 绘制接收信号和估计信号的幅度
plt.figure()
plt.scatter(received_signal[:100, 0].real, received_signal[:100, 0].imag, label='接收信号')
plt.scatter(constellation.real, constellation.imag, label='星座图')
plt.legend()
plt.xlabel('实部')
plt.ylabel('虚部')
plt.grid(True)
plt.show()
```
这个代码生成随机的发送数据,并对其进行 QAM 调制。然后,它通过一个三个系数的信道,并添加高斯白噪声。接下来,它使用最小二乘(LS)算法计算信道系数。然后,它解调接收信号,并计算误码率。最后,它绘制了接收信号和星座图以进行可视化。
阅读全文