You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python生成5000采样点高斯随机信号并计算标准差、绘制直方图

Python实现高斯信号生成、标准差计算与直方图绘制

依赖安装

需要用到numpy做数值计算、matplotlib做可视化,提前执行以下命令安装即可:
pip install numpy matplotlib

完整可运行代码

import numpy as np
import matplotlib.pyplot as plt

# 1. 生成5000个采样点的随机高斯信号
# 可自行调整mu(均值)、sigma(理论标准差)参数修改信号特性
mu = 0
sigma = 1
sample_num = 5000
gaussian_signal = np.random.normal(loc=mu, scale=sigma, size=sample_num)

# 2. 计算信号实际标准差
# 需要无偏估计的话可添加参数ddof=1:np.std(gaussian_signal, ddof=1)
actual_std = np.std(gaussian_signal)
print(f"生成的高斯信号实际标准差为:{actual_std:.4f}")

# 3. 绘制分布直方图
plt.figure(figsize=(10, 6))
# bins='auto'自动适配最优分箱数,edgecolor加边框提升辨识度
# 不需要概率密度显示、要显示实际采样数的话去掉density=True参数即可
n, bins, patches = plt.hist(gaussian_signal, bins='auto', edgecolor='black', density=True, alpha=0.6, label='采样信号分布')
# 可选:叠加理论高斯分布曲线做对照,不需要可以直接删除以下3行代码
theoratical_curve = (1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(-0.5 * np.power((bins - mu)/sigma, 2))
plt.plot(bins, theoratical_curve, '--', linewidth=2, color='red', label='理论高斯分布')

plt.title('5000采样点高斯信号分布直方图', fontsize=14)
plt.xlabel('信号值', fontsize=12)
plt.ylabel('概率密度', fontsize=12)
plt.grid(axis='y', alpha=0.3)
plt.legend()
plt.show()

效果说明

  • 生成的信号标准差和理论值的偏差会随采样量提升缩小,5000采样点下偏差通常在1%以内
  • 直方图默认适配了最优分箱数,也可以手动将bins参数改为整数值自定义分箱数量

内容的提问来源于stack exchange,提问作者daemushin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 08:06:04