在卷积响应函数中使用的正确点数是多少?

时间:2018-07-19 19:34:34

标签: python numpy convolution

我有一个输入函数/数据-f(x)-我想用一个响应函数-g(x)对它进行卷积以获得输出函数-h(x)。我已经验证了g(x)中使用的点数会影响h(x)的形状。 图1显示了我的意思比我可以描述的要好: Figure 1: Influence of the number of points used in the response function on the convoluted curves 然后,我的问题是:g(x)中使用的正确点数是多少?

我已经使用python和numpy来完成工作。我正在使用的代码是:

#!/usr/bin/python
# -*- coding: utf8 -*-

import numpy as np
import matplotlib.pyplot as plt
import csv

# Full Width at Half Maximum - also known as Instrumental Broadening
FWHM = 0.678

filename = "Input_Data_File.dat"
DataX,DataY = [],[]
with open(filename) as Data:
  reader = csv.reader(Data,delimiter=' ',skipinitialspace=True)
  for row in reader:
    DataX.append(float(row[0]))
    DataY.append(float(row[1]))

# The f(x) data:
DataX = np.array(DataX)
DataY = np.array(DataY)
lx = len(DataX)

x1 = np.linspace(-FWHM*2.0,FWHM*2.0,num=int(lx*(0.1))) #len(x) = 10% of len(DataX)
x2 = np.linspace(-FWHM*2.0,FWHM*2.0,num=int(lx*(0.02))) #len(x) = 2% of len(DataX)
x3 = np.linspace(-FWHM*2.0,FWHM*2.0,num=int(lx*(0.2))) #len(x) = 20% of len(DataX)

# Response function - Gaussian type
G1 = np.exp(-4*np.log(2)*((x1/FWHM)**2))
G2 = np.exp(-4*np.log(2)*((x2/FWHM)**2))
G3 = np.exp(-4*np.log(2)*((x3/FWHM)**2))

# Performing the convolutions
H1 = np.convolve(G1,DataY,'same')
H2 = np.convolve(G2,DataY,'same')
H3 = np.convolve(G3,DataY,'same')

# Normalizing convoluted functions
H1 = H1/max(H1)
H2 = H2/max(H2)
H3 = H3/max(H3)

# Normalizing DataY as Y
Y = DataY/max(DataY)

plt.plot(DataX,Y,'y--', label="Input function / f(x)")
plt.plot(DataX,H1,'r-', label="#points: {}".format(len(x1)))
plt.plot(DataX,H2,'g-', label="#points: {}".format(len(x2)))
plt.plot(DataX,H3,'b-', label="#points: {}".format(len(x3)))
plt.xlabel("x / DataXt")
plt.ylabel("Normalized intensity")
plt.ylim(-0.05,1.05)
plt.title("Influence of #points of 'Response\nfunction' in convoluted curves")
plt.legend(loc='upper left')
plt.savefig("Convoluted_curves.png")
plt.show()

然后可以在here中找到用作“ Input_Data_File.dat”的数据文件。

1 个答案:

答案 0 :(得分:0)

通过实验验证,我可以得出结论,在响应函数g(x)中使用的正确点数是步距最接近输入函数f(x)步距的点。

因此在问题的python代码中,最佳选择是使用 numpy.arange 正确的步骤,而不是 numpy.linspace