# 【Python】Python 实现破零（ZF）和最小均方误差（MMSE）信道均衡

## 1 理论

ZF算法使用一个加权矩阵W消除信道的干扰，根据估计的信道响应H初始化矩阵W
$$W_{ZF} = (H^HH)^{-1}H^H$$

$$\hat{y} = h \cdot x$$
$$\hat{z} = W_{ZF} \cdot \hat{y}$$

MMSE 均衡原理

## 2 ZF均衡实现

import numpy as np
from numpy.linalg import inv

# 此函数基于输入数组生成矩阵，偏移量offset基于输入数组
def generate_square_matrix(arr_data,size,data_offset,datatype):
aMatrix = np.mat(np.zeros(shape=(size,size))).astype(datatype)
for i1 in range(size):
for i2 in range(size):
try:
arr_index = i2+data_offset-i1
if arr_index < 0:
continue
aMatrix[i2,i1]=arr_data[arr_index]
except:
break
return aMatrix
# 它取一个矩阵并将矩阵的中间列提取到一个数组中
def get_W_vector(inv_matrix, size,data_type):
mid_col=size>>1
C_vector=np.zeros(shape=(size)).astype(data_type)
for i in range(size):
C_vector[i]= inv_matrix[i,mid_col]
return C_vector
def ZF_equalizer(y,h,size,size_of_input,data_type):
ZF_h = generate_square_matrix(h,size,h.argmax(),data_type)
ZF_h_hat = inv(ZF_h)
ZF_W = get_W_vector(ZF_h_hat,size,data_type)
z=np.convolve(y,ZF_W)
leftside=size>>1
# print("\nReconstructed signal: \n",z[leftside:leftside+size_of_input])
return z[leftside:leftside+size_of_input]

# 发送的信号
x = np.array([3-0.6j, -0.3+0.9j,
0.1 - 1.2j, -0.7+0.2j,
0+0.7j, -0.5+0.5j,
0.1-0.6j, -0.4+0.6j])
# 信道冲击响应
h = np.array([1, 0, 0.3+0.3j])
H_exact = np.fft.fft(h)
# 仿真经过信号经过信道
y = np.convolve(x, h)

# 滤波器长度
filter_length = 13
ZF_result = ZF_equalizer(y, h, filter_length, x.size, complex)
print("原始signal：", x)
print("恢复signal:", ZF_result)
plt.plot(abs(x))
plt.plot(abs(ZF_result))
plt.ylabel('$|Z|$')
plt.grid(True)
plt.savefig('signal.png')


## 3 MMSE均衡

import numpy as np
from numpy.linalg import inv

# 此函数基于输入数组生成矩阵，偏移量offset基于输入数组
def generate_square_matrix(arr_data,size,data_offset,datatype):
aMatrix = np.mat(np.zeros(shape=(size,size))).astype(datatype)
for i1 in range(size):
for i2 in range(size):
try:
arr_index = i2+data_offset-i1
if arr_index < 0:
continue
aMatrix[i2,i1]=arr_data[arr_index]
except:
break
return aMatrix
import matplotlib.pyplot as plt
from pyphysim.util.conversion import dB2Linear
from pyphysim.util.misc import pretty_time, randn_c
def MMSE_equalizer(x, y, size, data_type):
# 计算自相关性
ryy = np.correlate(y, y, "full")
# 计算互相关性
rxy = np.correlate(x, y, "full")

#从 ryy 和向量 Rxy 形式 rxy 生成矩阵 Ryy
Ryy = generate_square_matrix(ryy, size, ryy.argmax(), data_type)
Rxy = np.mat(np.zeros(shape=(size, 1))).astype(data_type)
# 计算偏移量
offset = rxy.argmax() - (size >> 1)
for i in range(size):Rxy[i, 0] = rxy[i+offset]
MMSE_C_Vec = np.asarray(inv(Ryy)*Rxy).flatten()
result = np.convolve(y, MMSE_C_Vec)
leftside = size >> 1
print("发送的signal: ", x)
print("接收的signal:", y)
print("恢复的signal:", result[leftside:leftside+x.size])
return result[leftside:leftside+x.size]

x = np.array([0.73+0.59j, 0.43+1.01j, 0.41+0.3j, 1.24+1.1j, 0.55+0.83j])
SNR_dB = 30
snr_linear = dB2Linear(SNR_dB)
noise_power = 1 / snr_linear
# 噪声
n = np.math.sqrt(noise_power) * randn_c(x.size)
# 信道响应
h = randn_c(x.size)
#
y_z = h * x + n
# MMSE均衡
y_z /= h
print(y_z)
# 滤波器长度
filter_length = 1
z = MMSE_equalizer(x, y_z, filter_length, complex)

plt.plot(abs(x))
plt.plot(abs(z))
plt.grid(True)
plt.savefig('signal2.png')


|
1月前
|

【Python】Python 仿真OFDM发射机、信道和接收机-实现多种调制方式

56 0
|
4月前
|

Python用KNN（K-近邻）回归、分类、异常值检测预测房价、最优K值选取、误差评估可视化
Python用KNN（K-近邻）回归、分类、异常值检测预测房价、最优K值选取、误差评估可视化
65 1
|
11月前
|

【100天精通Python】Day64：Python可视化_Matplotlib绘制误差线图、填充图、堆叠面积图，示例+代码
【100天精通Python】Day64：Python可视化_Matplotlib绘制误差线图、填充图、堆叠面积图，示例+代码
216 0
|
4月前
|

【Python机器学习】神经网络中误差反向传播（BP）算法详解及代码示例（图文解释 附源码）
【Python机器学习】神经网络中误差反向传播（BP）算法详解及代码示例（图文解释 附源码）
88 0
|

python移动窗口求股票预测误差均值
python移动窗口求股票预测误差均值
94 0
|

python实现BP神经网络进行预测和误差分析（附源代码）
python实现BP神经网络进行预测和误差分析（附源代码）
1085 0
|
Python
Python编程：方差、标准差、均方差、均方根值、均方误差、均方根误差
Python编程：方差、标准差、均方差、均方根值、均方误差、均方根误差
296 0
|
Python
Python编程：方差、标准差、均方差、均方根值、均方误差、均方根误差
Python编程：方差、标准差、均方差、均方根值、均方误差、均方根误差
721 0