均匀分布python代码
import numpy as np
import matplotlib.pyplot as plt
# 设定参数
a = 2 # (a-b)均匀分布下限
b = 3 # (a-b)均匀分布上限
fs = 1e7 # 采样率,单位:Hz
t = 1e-3 # 随机序列长度,单位:s
n = int(t * fs) # 采样点数
# 生成(0, 1)单位均匀分布的随机数
np.random.seed(0) # 设置随机数生成器的种子
u = np.random.rand(n)
# 由(0,1)分布转换到(a, b)均匀分布
x = (b - a) * u + a
# 绘制信号图
plt.figure(figsize=(10, 6))
plt.subplot(211)
plt.plot(x)
plt.title('均匀分布信号')
plt.xlabel('样本')
plt.ylabel('幅度')
# 绘制直方图
plt.subplot(212)
plt.hist(x, bins=np.arange(a, b+0.01, 0.01), alpha=0.7, edgecolor='black')
plt.title('均匀分布信号直方图')
plt.xlabel('幅度')
plt.ylabel('频率')
plt.tight_layout()
plt.show()
高斯分布(正态分布)
概念:正态分布、标准正态分布(定义、期望、方差、例题)
https://zhuanlan.zhihu.com/p/503557666
正态分布python代码
import numpy as np
import matplotlib.pyplot as plt
# 定义x的范围,从-10到10,步长为0.01
x = np.arange(-10, 10, 0.01)
# 计算正态分布的概率密度函数,均值为0,标准差为1
y = np.exp(-x**2 / 2) / np.sqrt(2 * np.pi)
# 创建图形和坐标轴
plt.figure(figsize=(8, 6))
ax = plt.axes([0.1, 0.1, 0.85, 0.85])
# 绘制正态分布曲线
plt.plot(x, y)
# 设置y轴和x轴的显示范围
plt.ylim([-0.01, 0.43])
plt.xlim([-3, 3])
# 显示图形
plt.show()