[英]Convolution with a 1D Gaussian
I am a noob in convolution and I am using Python. 我是卷积菜鸟,我在使用Python。 I am trying to convolve a 1D array with a 1D Gaussian and my array is
我正在尝试将1D数组与1D高斯进行卷积,而我的数组是
B = [0.011,0.022,.032,0.027,0.025,0.033,0.045,0.063,0.09,0.13,0.17,0.21]
. B = [0.011,0.022,.032,0.027,0.025,0.033,0.045,0.063,0.09,0.13,0.17,0.21]
。
The FWHM of the Gaussian is 5. So I calculated the sigma to be 5/2.385 = ~2.09
Now, I have 2 options: 高斯的FWHM为5。因此,我计算出的sigma为
5/2.385 = ~2.09
现在,我有2个选择:
Generate a Gaussian Kernal using standard equation for Gaussian and use np.convolve(array, Gaussian) Gaussian equation I used 使用高斯的标准方程式生成高斯核,并使用np.convolve(array,Gaussian) 我使用的高斯方程
Use scipy.ndimage.gaussian_filter1d
Since both are convolution tasks, theoretically both are supposed to give similar outputs. 使用
scipy.ndimage.gaussian_filter1d
由于两者都是卷积任务,因此理论上都应该提供相似的输出。 But it is not so. 但事实并非如此。 Why is it so?
为什么会这样呢?
I have attached an image where I have plotted the array vs another equally spaced array 我已在图像上绘制了阵列与另一个等距阵列的关系
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]
. 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) Basically, I want to plot the convolved array
and the non-convolved
array together vs A
. 相对于等距数组(A)绘制的数组(B)基本上,我想将
convolved array
和non-convolved
数组与A
一起绘制。 How do I do it? 我该怎么做?
Why do numpy.convolve
and scipy.ndimage.gaussian_filter1d
? 为什么
numpy.convolve
和scipy.ndimage.gaussian_filter1d
?
It is because the two functions handle the edge differently; 这是因为这两个函数对边缘的处理方式不同; at least the default settings do.
至少是默认设置。 If you take a simple peak in the centre with zeros everywhere else, the result is actually the same (as you can see below).
如果您在中心以一个简单的峰值在其他任何地方都为零,那么结果实际上是相同的(如下所示)。 By default
scipy.ndimage.gaussian_filter1d
reflects the data on the edges while numpy.convolve
virtually puts zeros to fill the data. 默认情况下,
scipy.ndimage.gaussian_filter1d
在边缘反射数据,而numpy.convolve
实际上将零填充以填充数据。 So if in scipy.ndimage.gaussian_filter1d
you chose the mode='constant'
with the default value cval=0
and numpy.convolve
in mode=same
to produce a similar size array, the results are, as you can see below, the same. 因此,如果在
scipy.ndimage.gaussian_filter1d
选择默认值为cval=0
的mode='constant'
,并且在mode=same
选择numpy.convolve
以产生相似的大小数组,则如下所示。
Depending on what you want to do with your data, you have to decide how the edges should be treated. 根据您要对数据进行的处理,必须决定如何处理边缘。
Concerning on how to plot this, I hope that my example code explains this. 关于如何绘制此图形,我希望我的示例代码对此进行解释。
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()
Providing the following image: 提供以下图像:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.