2024年4月24日16:46:31更新:
(这个比较正确)
重点是这个对数阴影衰落
import numpy as np
# Define distance-dependent path-loss function
def path_loss(d):
return 128.1 + 37.6 * np.log10(d)
# Set the number of channels
num_channels = 10
# Simulate distances (assuming random distances for this example)
distances = np.random.rand(num_channels) * 10 # Distances in kilometers
# Calculate path loss for each channel
path_loss_dB = path_loss(distances)
# Apply log-normal shadowing (10 dB standard deviation)
# 阴
shadowing = np.random.normal(scale=10, size=num_channels)
# Convert path loss from dB to linear scale
path_loss_linear = np.power(10, -path_loss_dB / 10)
# Generate Rayleigh fading coefficients (complex numbers)
fading = np.random.rayleigh(size=num_channels) * np.exp(2j * np.pi * np.random.rand(num_channels))
# Combine path loss, shadowing, and fading log(10^(-shadowing/10)=e^(-shadowing / 10) 正好就是对数正态分布
channel_coefficients = fading * np.sqrt(path_loss_linear) * 10 ** (-shadowing / 10)
# Print the channel coefficients
print(channel_coefficients)
注意几个点:
- 对数正态分布:如果x服从对数正态分布,则lnx服从正态分布;如果x服从正态分布,则e^x服从对数正态分布。
- 影衰落服从对数正态分布;则以dB表示的阴影衰落服从正态分布。以dB表示的阴影衰落通常取均值为0,方差为5-12dB的正态分布。所以上述代码有点问题,不需要exp.
ChatGPT答案:
import numpy as np
def generate_rayleigh_channel(num_samples):
# 生成实部和虚部,均为高斯分布 N(0, 1/2)
real_part = np.random.normal(0, np.sqrt(0.5), num_samples)
imag_part = np.random.normal(0, np.sqrt(0.5), num_samples)
# 合成复数形式的瑞利衰落系数
h = real_part + 1j * imag_part
return h
# 生成样本数量
num_samples = 1000
# 生成瑞利信道系数
h = generate_rayleigh_channel(num_samples)
# 打印前10个系数
print("First 10 Rayleigh channel coefficients:", h[:10])
瑞利衰落信道模型通常用于描述多径效应下的信号衰落,其中衰落系数 ( h ) 是复数,并且其幅度服从瑞利分布,相位服从均匀分布。在复高斯分布 C N ( 0 , 1 ) \mathcal{CN}(0,1) CN(0,1)的情况下,实际上是指 ( h ) 的实部和虚部分别独立地服从高斯分布 ( \mathcal{N}(0,1/2) ),因为复高斯分布的功率归一化到1。
下面是一个生成瑞利信道衰落系数 ( h ) 的 Python 代码示例,其中 ( h ) 的实部和虚部均服从标准高斯分布(均值为0,方差为1):
def generate_rayleigh_channels(num_channels):
# 生成实部和虚部
real_parts = np.random.normal(0, 1/np.sqrt(2), num_channels)
imag_parts = np.random.normal(0, 1/np.sqrt(2), num_channels)
# 合成复数衰落系数数组
h = real_parts + 1j * imag_parts
return h
# 生成多个瑞利信道衰落系数
num_channels = 10
h_array = generate_rayleigh_channels(num_channels)
print("Generated Rayleigh channel fading coefficients:", h_array)
注意,由于我们希望 ( h ) 的功率归一化为1,即 ( ∣ h ∣ 2 |h|^2 ∣h∣2 ) 的期望值为1,因此我们使用标准差为 ( 1 / 2 1/\sqrt{2} 1/2 ) 的高斯分布来生成实部和虚部。这是因为复数 ( h ) 的功率是实部和虚部平方和的一半,即 ( ∣ h ∣ 2 = ( r e a l _ p a r t 2 + i m a g _ p a r t 2 ) / 2 |h|^2 = (real\_part^2 + imag\_part^2)/2 ∣h∣2=(real_part2+imag_part2)/2 ),所以需要将方差设置为 ( 1/2 ),而标准差为 ( 1 / 2 1/\sqrt{2} 1/2 )。