kot*_*ota 5 python numpy gaussian scipy
我希望实现由 Lindeberg 在他关于尺度空间理论的工作中定义的离散高斯核。
它被定义为 T(n,t) = exp(-t)*I_n(t) 其中 I_n 是第一类修正贝塞尔函数。
我正在尝试使用 Numpy 和 Scipy 在 Python 中实现这一点,但遇到了一些麻烦。
def discrete_gaussian_kernel(t, n):
return math.exp(-t) * scipy.special.iv(n, t)
Run Code Online (Sandbox Code Playgroud)
我尝试绘制:
import math
import numpy as np
import scipy
from matplotlib import pyplot as plt
def kernel(t, n):
return math.exp(-t) * scipy.special.iv(n, t)
ns = np.linspace(-5, 5, 1000)
y0 = discrete_gaussian_kernel(0.5, ns)
y1 = discrete_gaussian_kernel(1, ns)
y2 = discrete_gaussian_kernel(2, ns)
y3 = discrete_gaussian_kernel(4, ns)
plt.plot(ns, y0, ns, y1, ns, y2, ns, y3)
plt.xlim([-4, 4])
plt.ylim([0, 0.7])
Run Code Online (Sandbox Code Playgroud)
输出看起来像:
从维基百科文章,它应该是这样的:
我想我犯了一些非常微不足道的错误。:/ 有什么想法吗?谢谢!
编辑:我写的相当于scipy.special.ive(n, t). 我很确定它应该是第一种修正的 Bessel 函数,而不是第二种,但有人可以确认吗?
如果你想得到维基百科的情节,替换
ns = np.linspace(-5, 5, 1000)
Run Code Online (Sandbox Code Playgroud)
和
ns = np.arange(-5, 5+1)
Run Code Online (Sandbox Code Playgroud)
也就是说,您使用的公式仅对整数点有意义。作为负阶函数的贝塞尔函数是一个振荡函数,http://dlmf.nist.gov/10.27#E2所以这个情节对我来说很好。