与一维高斯卷积
Convolution with a 1D Gaussian
我是卷积新手,我正在使用 Python。我正在尝试将一维数组与一维高斯进行卷积,我的数组是
B = [0.011,0.022,.032,0.027,0.025,0.033,0.045,0.063,0.09,0.13,0.17,0.21]
。
Gaussian 的 FWHM 是 5。所以我计算出的 sigma 是 5/2.385 = ~2.09
现在,我有 2 个选择:
使用高斯标准方程生成高斯核,并使用np.convolve(array, Gaussian)
Gaussian equation I used
使用scipy.ndimage.gaussian_filter1d
由于两者都是卷积任务,理论上两者都应该给出相似的输出。但事实并非如此。为什么会这样?
我附上了一张图片,其中我绘制了阵列与另一个等距阵列的对比图
A = [1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0, 21.0, 23.0]
.
The array (B) plotted against equally spaced array (A)
基本上,我想将 convolved array
和 non-convolved
数组与 A
一起绘制。我该怎么做?
为什么要 numpy.convolve
和 scipy.ndimage.gaussian_filter1d
?
这是因为两个函数对边缘的处理不同;至少默认设置可以。如果你在中心取一个简单的峰,其他地方都为零,结果实际上是一样的(如下图所示)。默认情况下 scipy.ndimage.gaussian_filter1d
反映边缘上的数据,而 numpy.convolve
实际上用零填充数据。因此,如果在 scipy.ndimage.gaussian_filter1d
中选择 mode='constant'
默认值 cval=0
并在 mode=same
中选择 numpy.convolve
来生成类似大小的数组,结果与您一样可以看下面,一样的。
根据您要对数据执行的操作,您必须决定应如何处理边缘。
关于如何绘制这个,我希望我的示例代码解释这个。
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import gaussian_filter1d
def gaussian( x , s):
return 1./np.sqrt( 2. * np.pi * s**2 ) * np.exp( -x**2 / ( 2. * s**2 ) )
myData = np.zeros(25)
myData[ 12 ] = 1
myGaussian = np.fromiter( (gaussian( x , 1 ) for x in range( -3, 4, 1 ) ), np.float )
filterdData = gaussian_filter1d( myData, 1 )
myFilteredData = np.convolve( myData, myGaussian, mode='same' )
fig = plt.figure(1)
ax = fig.add_subplot( 2, 1, 1 )
ax.plot( myData, marker='x', label='peak' )
ax.plot( filterdData, marker='^',label='filter1D smeared peak' )
ax.plot( myGaussian, marker='v',label='test Gaussian' )
ax.plot( myFilteredData, marker='v', linestyle=':' ,label='convolve smeared peak' )
ax.legend( bbox_to_anchor=( 1.05, 1 ), loc=2 )
B = [0.011,0.022,.032,0.027,0.025,0.033,0.045,0.063,0.09,0.13,0.17,0.21]
myGaussian = np.fromiter( ( gaussian( x , 2.09 ) for x in range( -4, 5, 1 ) ), np.float )
bx = fig.add_subplot( 2, 1, 2 )
bx.plot( B, label='data: B' )
bx.plot( gaussian_filter1d( B, 2.09 ), label='filter1d, refl' )
bx.plot( myGaussian, label='test Gaussian' )
bx.plot( np.convolve( B, myGaussian, mode='same' ), label='Gaussian smear' )
bx.plot( gaussian_filter1d( B, 2.09, mode='constant' ), linestyle=':', label='filter1d, constant')
bx.legend( bbox_to_anchor=(1.05, 1), loc=2 )
plt.tight_layout()
plt.show()
提供以下图片:
我是卷积新手,我正在使用 Python。我正在尝试将一维数组与一维高斯进行卷积,我的数组是
B = [0.011,0.022,.032,0.027,0.025,0.033,0.045,0.063,0.09,0.13,0.17,0.21]
。
Gaussian 的 FWHM 是 5。所以我计算出的 sigma 是 5/2.385 = ~2.09
现在,我有 2 个选择:
使用高斯标准方程生成高斯核,并使用np.convolve(array, Gaussian) Gaussian equation I used
使用
scipy.ndimage.gaussian_filter1d
由于两者都是卷积任务,理论上两者都应该给出相似的输出。但事实并非如此。为什么会这样?
我附上了一张图片,其中我绘制了阵列与另一个等距阵列的对比图
A = [1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0, 21.0, 23.0]
.
The array (B) plotted against equally spaced array (A)
基本上,我想将 convolved array
和 non-convolved
数组与 A
一起绘制。我该怎么做?
为什么要 numpy.convolve
和 scipy.ndimage.gaussian_filter1d
?
这是因为两个函数对边缘的处理不同;至少默认设置可以。如果你在中心取一个简单的峰,其他地方都为零,结果实际上是一样的(如下图所示)。默认情况下 scipy.ndimage.gaussian_filter1d
反映边缘上的数据,而 numpy.convolve
实际上用零填充数据。因此,如果在 scipy.ndimage.gaussian_filter1d
中选择 mode='constant'
默认值 cval=0
并在 mode=same
中选择 numpy.convolve
来生成类似大小的数组,结果与您一样可以看下面,一样的。
根据您要对数据执行的操作,您必须决定应如何处理边缘。
关于如何绘制这个,我希望我的示例代码解释这个。
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import gaussian_filter1d
def gaussian( x , s):
return 1./np.sqrt( 2. * np.pi * s**2 ) * np.exp( -x**2 / ( 2. * s**2 ) )
myData = np.zeros(25)
myData[ 12 ] = 1
myGaussian = np.fromiter( (gaussian( x , 1 ) for x in range( -3, 4, 1 ) ), np.float )
filterdData = gaussian_filter1d( myData, 1 )
myFilteredData = np.convolve( myData, myGaussian, mode='same' )
fig = plt.figure(1)
ax = fig.add_subplot( 2, 1, 1 )
ax.plot( myData, marker='x', label='peak' )
ax.plot( filterdData, marker='^',label='filter1D smeared peak' )
ax.plot( myGaussian, marker='v',label='test Gaussian' )
ax.plot( myFilteredData, marker='v', linestyle=':' ,label='convolve smeared peak' )
ax.legend( bbox_to_anchor=( 1.05, 1 ), loc=2 )
B = [0.011,0.022,.032,0.027,0.025,0.033,0.045,0.063,0.09,0.13,0.17,0.21]
myGaussian = np.fromiter( ( gaussian( x , 2.09 ) for x in range( -4, 5, 1 ) ), np.float )
bx = fig.add_subplot( 2, 1, 2 )
bx.plot( B, label='data: B' )
bx.plot( gaussian_filter1d( B, 2.09 ), label='filter1d, refl' )
bx.plot( myGaussian, label='test Gaussian' )
bx.plot( np.convolve( B, myGaussian, mode='same' ), label='Gaussian smear' )
bx.plot( gaussian_filter1d( B, 2.09, mode='constant' ), linestyle=':', label='filter1d, constant')
bx.legend( bbox_to_anchor=(1.05, 1), loc=2 )
plt.tight_layout()
plt.show()
提供以下图片: