【发布时间】:2018-07-19 19:34:33
【问题描述】:
我有一个输入函数/数据 - f(x) - 我想将它与一个响应函数 - g(x) 进行卷积 - 以获得一个输出函数 - h(x)。我已经验证了 g(x) 中使用的点数正在影响 h(x) 的形状。 图 1 显示了我的意思比我能描述的更好: 那么,我的问题是:在 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()
而我用作“Input_Data_File.dat”的数据文件可以在here找到。
【问题讨论】:
标签: python numpy convolution