-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_gauss.py
60 lines (52 loc) · 1.94 KB
/
multi_gauss.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
# a multi gaussian maker for to use with scipy curve_fit
import math
def multiGauss(x, *par):
"""
Fits single or multiple Gaussian peaks
Algorithm :
1. create single gaussian term as a function
2. here number of unknowns 3 --> p0, p1, p2.
3. say number of peaks to fit --> 3
4. total parameters: p0, p1,...p8 --> (3*3)
5. construct a list --> [0, 1, 2....3*3-1]
6. break it down to chunks of 3 --> [0, 1, 2] etc
7. use the chunks to create terms and use sum func
to sum it up
8. list comprehend the sum over all element in x data
When a piece of code like this function works I just
remember the verse from Bhagavat Gita
aash-charya-vat pasyati kash-chid enam
aash-charyavad vadati tathaiva chaanyaha
aashcharya-vach chainyam anyaha srunoti
shrutvapyenam veda na chaiva kash-chit || 2.29 ||
'One sees this Self as a wonder,
another speaks of it in wonder,
another hears of it as a wonder;
yet having heard none understands this at all'
"""
# number of peaks to fit
# make N global; N = 3 #3
N = int(len(par)/3.0)
# number of unknown parameters in the equation
n = 3 #3
# (i, j, k) for three parameters
def term(X, i, j, k):
return par[i] * math.exp(- (X - par[j])**2 / (2 * par[k]**2))
split = []
# split [0..9] --> [0, 1, 2], [3, 4, 5] etc.
for i in range(0, n * N, 3):
# split_ --> [0, 1, 2] etc.
# range(0, 3), range(3, 6) etc.
split_ = list(range(i, i + 3))
# split --> [[0, 1, 2], ...]
split.append(split_)
# create multi-equation with multiple gaussian
# terms as a sum
def multiterm(X):
"""
sum all the terms in the gaussian
"""
return sum((term(X, i, j, k) for (i, j, k) in \
split))
# sum the terms over the entire x range
return [multiterm(X) for X in x]