forked from amerand/SPIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpfunc.py
145 lines (126 loc) · 4.03 KB
/
dpfunc.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
collection of functions parametrized by dictionnaries, to allow easy fitting
with dpfit.py
author: amerand@eso.org
"""
import numpy as np
def polyN(x, params):
"""
Polynomial function. e.g. params={'A0':1.0, 'A2':2.0} returns
x->1+2*x**2. The coefficients do not have to start at 0 and do not
need to be continuous. Actually, the way the function is written,
it will accept any '_x' where '_' is any character and x is a float.
"""
res = 0
for k in params.keys():
res += params[k]*np.array(x)**float(k[1:])
return res
def power(x,params):
"""
return params['AMP']*(X - params['X0'])**params['POW'] + params['OFFSET']
only params['POW'] is mandatory
"""
res = x
if params.has_key('X0'):
res -= params['X0']
res = res**params['POW']
if params.has_key('AMP'):
res *= params['AMP']
if params.has_key('OFFSET'):
res += params['OFFSET']
return res
def gaussian(x,params):
"""
1D gaussian function of moment 'MU' and variance 'SIGMA'**2
params: {'MU':, 'SIGMA':, ('OFFSET':,) ('AMP':)} 'AMP' and
'OFFSET' are optional: if AMP is not given, the amplitude is set
to 1/(sigma*sqrt(2*pi)).
"""
res = np.exp(-(x-params['MU'])**2/(2*params['SIGMA']**2))
if params.has_key('AMP'):
res *= params['AMP']
else:
res *= 1./(params['SIGMA']*np.sqrt(2*np.pi))
if params.has_key('OFFSET'):
res += params['OFFSET']
return res
def lorentzian(x,params):
"""
1D lorentzian function of moment 'MU' and parameter 'GAMMA'
params: {'MU':, 'GAMMA':, ('OFFSET':,) ('AMP':)}. 'AMP' and
'OFFSET' are optional: if AMP is not given, the amplitude is set to
1/(sigma*sqrt(2*pi)). """
res = 1/(1+(x-params['MU'])**2/(params['GAMMA']**2))
if params.has_key('AMP'):
res *= params['AMP']
else:
res *= 1./(params['GAMMA']*np.pi)
if params.has_key('OFFSET'):
res += params['OFFSET']
return res
def sin(x, params):
"""
sinusoidal wave
params: {'AMP':amplitude, 'WAV':wavelength, ('OFFSET':offset), ('PHI':phase
in radian)}
returns AMP*sin(2*pi*x/WAV + PHI) + OFFSET
"""
xx = 2*np.pi*x/params['WAV']
if params.has_key('PHI'):
xx += params['PHI']
res = params['AMP']*np.sin(xx)
if params.has_key('OFFSET'):
res += params['OFFSET']
return res
def cos(x, params):
"""
cosinusoidal wave
params: {'AMP':amplitude, 'WAV':wavelength, ('OFFSET':offset), ('PHI':phase
in radian)}
returns AMP*cos(2*pi*x/WAV + PHI) + OFFSET
"""
xx = 2*np.pi*x/params['WAV']
if params.has_key('PHI'):
xx += params['PHI']
res = params['AMP']*np.cos(xx)
if params.has_key('OFFSET'):
res += params['OFFSET']
return res
def fourier(x, params):
"""
fourier serie:
(A0) + sum(Ak*cos(k*2*pi*x/WAV +PHIk))_k=1...
A0 is optional. Parameters should contain at least WAV and one (Ak,PHIk),
for example:
{'WAV':1.0, 'A1':1.0, 'PHI1':0} -> cos(2*pi*x)
{'A0':1.0, 'WAV':2*np.pi, 'A2':1.0, 'PHI2':np.pi/4} -> 1+cos(2*x+np.pi/4)
warning, will crash if dictionnary is ill formed, for example:
{'WAV':1.0, 'A1':1.0, 'PHI2':0}
"""
phi = filter(lambda k: k[:3]=='PHI', params.keys())
res = np.copy(x)
res *=0
for f in phi:
res += params['A'+f[3:]]*np.cos(float(f[3:])*2*np.pi*x/params['WAV']
+ params[f])
if params.has_key('A0'):
res += params['A0']
return res
def gauss2d(xy, params):
"""
xy = [x, y]
"""
if 'SIGMAX' in params.keys():
sigmax = params['SIGMAX']
if 'SIGMAY' in params.keys():
sigmay = params['SIGMAY']
if 'SIGMA' in params.keys():
sigmax = params['SIGMA']
sigmay = params['SIGMA']
res = np.exp(-(xy[0]-params['X0'])**2/(2*sigmax)**2)
res *= np.exp(-(xy[1]-params['Y0'])**2/(2*sigmay)**2)
if 'AMP' in params.keys():
res *= params['AMP']
if 'OFFSET' in params.keys():
res *= params['OFFSET']
return res