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)=a⋅exp(−2σ2(x−b)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)=A⋅exp(−2σx2(x−x0)2−2σy2(y−y0)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()