生成瑞利信道(Python and Matlab)

channel h k h_k hk is modeled as independent Rayleigh fading with average power loss set as 10^−3

Python

import numpy as np

# Set the parameters
average_power_loss = 1e-3  # Average power loss (10^(-3))
num_samples = 1000  # Number of fading samples to generate

# Calculate the Rayleigh scale parameter (sigma)
# The scale parameter is related to the average power loss as follows:
# average_power_loss = 2 * sigma^2
sigma = np.sqrt(average_power_loss / 2)

# Generate independent Rayleigh fading samples
rayleigh_samples = sigma * np.random.randn(num_samples) + 1j * sigma * np.random.randn(num_samples)

# The above code generates complex samples, where the real and imaginary parts
# are both independently Rayleigh distributed.

# Optionally, you can plot a histogram of the fading samples to visualize
# the Rayleigh distribution.
import matplotlib.pyplot as plt

plt.hist(np.abs(rayleigh_samples), bins=50, density=True)
plt.title("Rayleigh Fading Samples")
plt.xlabel("Amplitude")
plt.ylabel("Probability Density")
plt.show()

对比一下:

H = np.random.rayleigh(scale=1, size= N)*1e-3

如果设定的hk是实数,直接取模就行了。

# Take the absolute value to get real Rayleigh fading samples
rayleigh_samples_real = np.abs(rayleigh_samples_complex)

在这里插入图片描述

考虑大尺度衰落:

#其中p为第K个用户的发射功率,g为第n个用户的大尺度信道增益,包括路径损耗和阴影,
#h~CN( 0 , 1)为第K个用户的瑞利衰落系数,N0为噪声功率谱密度。
def large_small(K,d,p,W): #
    N0_dBm = 3.981e-21  # -174dBm==3.981e-21
    def large_small(K):  #
    d_mean = 40
    std_dev = np.sqrt(10)
    distance = np.random.normal(d_mean, std_dev, K)
    distance = np.clip(distance, 10, 70)
    path_loss = 128.1 + 37.6 * np.log10(distance / 1000)  # dB    小区半径为500m
    # the shadowing factor is set as 6 dB.
    shadow_factor = 8  # dB
    h_large = 10 ** (-(path_loss + shadow_factor) / 10)

    sigma = np.sqrt(1 / 2)
    h_small = sigma * np.random.randn(K) + 1j * sigma * np.random.randn(K)

    h = abs(h_small) ** 2
    # snr = h_large * h * p / (N0_dBm * W)
    return h_large * h


考虑蜂窝基站的位置

# time: 2024/3/9 16:02
# author: YanJP
import numpy as np


np.random.seed(1)  # 随机种子
R = 2  # 基站到基站的距离 0.8
PL = lambda d: 128.1 + 37.6 * np.log10(d)  # 路损模型,d--km
U = 7  # 用户个数,每个蜂窝中有一个用户
C = 7  # 基站个数
P = 50  # 最大发射功率43 dBm
sigma2 = -105  # 噪声功率 -100 dBm
shadowing_std = 8  # 阴影衰落的标准差-8 dB

B = 10e6  # 10Mhz
def channel_generate(U, R, PL, shadowing_std):
    # 在正六边形蜂窝小区中撒点
    cell_loc = np.array([[0, 0],
                         [R * np.cos(np.pi / 6), R * np.sin(np.pi / 6)],
                         [0, R],
                         [-R * np.cos(np.pi / 6), R * np.sin(np.pi / 6)],
                         [-R * np.cos(np.pi / 6), -R * np.sin(np.pi / 6)],
                         [0, -R],
                         [R * np.cos(np.pi / 6), -R * np.sin(np.pi / 6)]])

    C = 7  # 蜂窝小区个数

    L = R * np.tan(np.pi / 6)  # 六边形的边长

    # 产生用户位置
    user_loc = np.zeros((U, 2))
    i = 0
    while i < U:
        x = 2 * L * np.random.rand(2) - 1 * L
        if (abs(x[0]) + abs(x[1]) / np.sqrt(3)) <= L and abs(x[1]) <= L * np.sqrt(3) / 2:
            i += 1
            user_loc[i - 1, :] = x + cell_loc[i - 1, :]

    # 计算距离
    dis = np.zeros((U, C))
    for i in range(U):
        for j in range(C):
            dis[i, j] = np.linalg.norm(cell_loc[j, :] - user_loc[i, :])

    # 计算信道增益,考虑服从对数正态分布的阴影衰落
    H_gain = -PL(dis) - shadowing_std * np.random.randn(U, C)

    return H_gain


if __name__ == '__main__':
    H_gain = channel_generate(U, R, PL, shadowing_std)  # 随机产生用户位置,并产生信道增益
    H_gain = 10 ** ((H_gain - sigma2) / 10)  # 为了优化方便,将噪声功率归一化
    print(H_gain)

Matlab:

% Set the average power loss
average_power_loss = 10^(-3);

% Calculate the scale parameter (σ)
sigma = sqrt(average_power_loss / 2);

% Number of samples to generate
num_samples = 1000;

% Generate random samples from the Rayleigh distribution
rayleigh_samples = raylrnd(sigma, 1, num_samples);

% Plot the histogram of the generated samples
histogram(rayleigh_samples, 50); % Adjust the number of bins as needed

% Label the axes
xlabel('Rayleigh Fading');
ylabel('Frequency');

% Title for the plot
title(['Rayleigh Fading with Average Power Loss of 10^(-3), \sigma = ', num2str(sigma)]);

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值