-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolve.py
executable file
·70 lines (58 loc) · 1.51 KB
/
convolve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import sys
import subprocess
import numpy as np
def f_G(x,mu,sigma):
"""Gauss function"""
return 1.0/(np.sqrt(2.0*np.pi)*sigma) * np.exp(-0.5*((x-mu)/sigma)**2)
def convolve(x, y, sigma=1.0, decim=1):
"""Convolve array y(x) with function f_G"""
wd = 3.0*sigma
i = 1
j = 0
while x[i]-x[0] < wd and i < len(x):
i += 1
xc = []
yc = []
while x[-1]-x[i] > wd and i < len(x):
s = 0.0
norm = 0.0
k = j
print("--")
print("i = ", i)
print("j = ", j)
while x[k]-x[i] < wd:
print("k = ", k)
tmp = f_G(x[k]-x[i],0.0,sigma)
s += y[k] * tmp
norm += tmp
k += 1
print("--")
xc.append(x[i])
yc.append(s/norm)
i += decim
while x[i]-x[j] > wd:
j += 1
return xc, yc
def main():
"""Test of a spectrum convolution with an intrumental profile."""
sigma = 1.0
s = 0.0
n = 10
x1 = -5.0*sigma
x2 = -x1
dx = (x2-x1)/n
for i in range(0,n+1):
x = x1+(x2-x1)*i/n
tmp = f_G(x,0.0,sigma)
s += tmp*dx
print("x = ", x, ", f_G = ", tmp)
print("s = ", s)
sys.exit(0)
data = np.loadtxt("shellspectrum", usecols=[0,3]).transpose()
x = data[0]; y = data[1]
xc, yc = convolve(x, y, sigma=5.0, decim=1)
np.savetxt("convolve.out", np.transpose([xc,yc]))
subprocess.check_output("./convolve.plt", shell=True)
if __name__ == "__main__":
main()