高斯函数绘制

1D高斯函数 1D Gaussian Function

数学形式
f(x)=a⋅exp⁡(−(x−b)22σ2)f(x)= a\cdot\exp\left(-\frac{(x - b)^2}{2\sigma^2}\right)f(x)=aexp(2σ2(xb)2)
其中:

  • aaa 是函数的幅度
  • bbb 是函数的中心位置
  • σ\sigmaσ 是标准差,控制函数的宽度。

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

# 定义高斯函数
def gauss(x, a, b, sigma):
    return a * np.exp(-0.5 * ((x - b) / sigma) ** 2)
    
# 设置参数
params = [
    (1, 0, 1),  # (a, b, sigma)
    (0.5, 1, 2),
    (2, -1, 0.5)
]  

x = np.linspace(-5, 5, 100) 

# 绘制高斯函数
plt.figure(figsize=(10, 6))
for a, b, sigma in params:
    y = gauss(x, a, b, sigma)
    plt.plot(x, y, label=f'a={a}, b={b}, sigma={sigma}')  

plt.title('1D Gaussian Function with Different Parameters')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()

2D高斯函数 2D Gaussian Function

数学形式
f(x,y)=A⋅exp⁡(−(x−x0)22σx2−(y−y0)22σy2)f(x, y) = A \cdot \exp\left( -\frac{(x - x_0)^2}{2\sigma_x^2} - \frac{(y - y_0)^2}{2\sigma_y^2} \right)f(x,y)=Aexp(2σx2(xx0)22σy2(yy0)2)
其中:

  • f(x,y)f(x,y)f(x,y):二维高斯函数的值,定义在 x 和 y 平面上的某一点。
  • A:幅度(系数),控制高斯函数的整体幅度或峰值。
  • (x0,y0)(x_0, y_0)(x0,y0):高斯函数中心的坐标,表示高斯分布的中心位置。
  • σx\sigma_xσx​:沿 x 方向的标准差,控制 x 方向上的宽度。
  • σy\sigma_yσy​:沿 y 方向的标准差,控制 y 方向上的宽度。
  • exp⁡\expexp:指数函数,表示自然对数的底数 e 的幂。

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt  

# 定义2维高斯函数
def gauss2d(x, y, A, x0, y0, sigma_x, sigma_y):
    return A * np.exp(-0.5 * (((x - x0) / sigma_x) ** 2 + ((y - y0) / sigma_y) ** 2))

# 设置参数
params_2d = [
    (1, 0, 0, 1, 1),  # (A, x0, y0, sigma_x, sigma_y)
    (0.5, 1, 1, 2, 2),
    (2, -1, -1, 0.5, 0.5)
]  

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y) 

# 绘制2维高斯函数
fig = plt.figure(figsize=(16, 6))
for i, (A, x0, y0, sigma_x, sigma_y) in enumerate(params_2d, 1):
    Z = gauss2d(X, Y, A, x0, y0, sigma_x, sigma_y)
    ax = fig.add_subplot(1, 3, i, projection='3d')
    ax.plot_surface(X, Y, Z, cmap='viridis')
    ax.set_title(f'A={A}, x0={x0}, y0={y0},\n sigma_x={sigma_x}, sigma_y={sigma_y}')

    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('f(x, y)')  

plt.suptitle('2D Gaussian Function with Different Parameters')
plt.savefig('2DGaussianFunction.png')
plt.subplots_adjust(left=0.1, right=0.9, top=0.85, bottom=0.15, wspace=0.3)
plt.show()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值