Skip to content

Models

About implemented models

In principle, every model can be implemented in spectrafit by extending the module spectrafit.models by a new functions. It is important to know that the raise check have to be extend by the new function name in the solver_model and calculated_model.

__implemented_models__ = [
    "gaussian",
    "lorentzian",
    "voigt",
    "pseudovoigt",
    "exponential",
    "power",
    "linear",
    "constant",
    "erf",
    "atan",
    "log",
    "heaviside",
    "my_new_model",
]
...
for model in params:
    model = model.lower()
    if model.split("_")[0] not in __implemented_models__:
        raise KeyError(f"{model} is not supported")
    peak_kwargs[(model.split("_")[-1], model.split("_")[0])][
        model.split("_")[1]
    ] = params[model]

for key, _kwarg in peak_kwargs.items():
    if key[1] == "my_new_model":
        val += my_new_model(x, **_kwarg)
    ...


def my_new_model(
    x: np.array, amplitude: float = 1.0, center: float = 0.0, fwhmg: float = 1.0
) -> np.array:
    r"""Return a 1-dimensional `m`y_new_model` distribution."""


...
Further information about implemented own models in lmfit can be found in this example. So far, the built-in models of lmfit are not supported, yet.

Change in notation for the Full Maximum Half Widht (FWHM)

The notation for the Full Maximum Half Widht (FWHM) is adapted due to changes in the **kwargs-handling in the models; see also API and CHANGELOG. The notation becomes:

Method Old Notation New Notation
Gaussian-FWHM fwhm fwhmg
Lorentzian-FWHM fwhm fwhml
Pseudo-Voigt fwhm_g fwhmg
Pseudo-Voigt fwhm_l fwhml
Voigt fwhm fwhmv

Implemented models

Here is a list of implemented models of spectrafit:

Return a 1-dimensional Gaussian distribution.

\[ {\displaystyle g(x)={\frac {1}{\sigma {\sqrt {2\pi }}}}\exp ( -{\frac {1}{2}}{\frac {(x-\mu )^{2}}{\sigma ^{2}}} ) } \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the Gaussian distribution. Defaults to 1.0.

1.0
center float

Center of the Gaussian distribution. Defaults to 0.0.

0.0
fwhmg float

Full width at half maximum (FWHM) of the Gaussian distribution. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Gaussian distribution of x given.

Source code in spectrafit/models.py
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
@staticmethod
def gaussian(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    fwhmg: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional Gaussian distribution.

    $$
    {\displaystyle g(x)={\frac {1}{\sigma {\sqrt {2\pi }}}}\exp
    (  -{\frac {1}{2}}{\frac {(x-\mu )^{2}}{\sigma ^{2}}} ) }
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the Gaussian distribution.
             Defaults to 1.0.
        center (float, optional): Center of the Gaussian distribution.
             Defaults to 0.0.
        fwhmg (float, optional): Full width at half maximum (FWHM) of the Gaussian
            distribution. Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Gaussian distribution of `x` given.
    """
    sigma = fwhmg * Constants.fwhmg2sig
    return np.array(amplitude / (Constants.sq2pi * sigma)) * np.exp(
        -((1.0 * x - center) ** 2) / (2 * sigma**2)
    )

Return a 1-dimensional Lorentzian distribution.

\[ f(x;x_{0},\gamma )={\frac {1}{\pi \gamma [ 1+ ( {\frac {x-x_{0}}{\gamma }})^{2} ] }} ={1 \over \pi \gamma } [ {\gamma ^{2} \over (x-x_{0})^{2}+\gamma ^{2}} ] \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the Lorentzian distribution. Defaults to 1.0.

1.0
center float

Center of the Lorentzian distribution. Defaults to 0.0.

0.0
fwhml float

Full width at half maximum (FWHM) of the Lorentzian distribution. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

Union[NDArray[np.float64], float]: Lorentzian distribution of x given.

Source code in spectrafit/models.py
 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
@staticmethod
def lorentzian(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    fwhml: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional Lorentzian distribution.

    $$
    f(x;x_{0},\gamma )={\frac  {1}{\pi \gamma
    [ 1+ ( {\frac  {x-x_{0}}{\gamma }})^{2} ]
    }} ={1 \over \pi \gamma } [ {\gamma ^{2} \over (x-x_{0})^{2}+\gamma ^{2}} ]
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the Lorentzian distribution.
            Defaults to 1.0.
        center (float, optional): Center of the Lorentzian distribution. Defaults to
            0.0.
        fwhml (float, optional): Full width at half maximum (FWHM) of the Lorentzian
            distribution. Defaults to 1.0.

    Returns:
        Union[NDArray[np.float64], float]: Lorentzian distribution of `x` given.
    """
    sigma = fwhml * Constants.fwhml2sig
    return np.array(amplitude / (1 + ((1.0 * x - center) / sigma) ** 2)) / (
        pi * sigma
    )

Return a 1-dimensional Pseudo-Voigt distribution.

See also:

J. Appl. Cryst. (2000). 33, 1311-1316 https://doi.org/10.1107/S0021889800010219

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the Pseudo-Voigt distribution. Defaults to 1.0.

1.0
center float

Center of the Pseudo-Voigt distribution. Defaults to 0.0.

0.0
fwhmg float

Full width half maximum of the Gaussian distribution in the Pseudo-Voigt distribution. Defaults to 1.0.

1.0
fwhml float

Full width half maximum of the Lorentzian distribution in the Pseudo-Voigt distribution. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Pseudo-Voigt distribution of x given.

Source code in spectrafit/models.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
@staticmethod
def pseudovoigt(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    fwhmg: float = 1.0,
    fwhml: float = 1.0,
) -> NDArray[np.float64]:
    """Return a 1-dimensional Pseudo-Voigt distribution.

    !!! note "See also:"

        J. Appl. Cryst. (2000). 33, 1311-1316
        https://doi.org/10.1107/S0021889800010219

    Args:
        x (NDArray[np.float64]):  `x`-values of the data.
        amplitude (float, optional): Amplitude of the Pseudo-Voigt distribution.
            Defaults to 1.0.
        center (float, optional): Center of the Pseudo-Voigt distribution.
            Defaults to 0.0.
        fwhmg (float, optional): Full width half maximum of the Gaussian
            distribution in the Pseudo-Voigt distribution. Defaults to 1.0.
        fwhml (float, optional): Full width half maximum of the Lorentzian
            distribution in the Pseudo-Voigt distribution. Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Pseudo-Voigt distribution of `x` given.
    """
    f = np.power(
        fwhmg**5
        + 2.69269 * fwhmg**4 * fwhml
        + 2.42843 * fwhmg**3 * fwhml**2
        + 4.47163 * fwhmg**2 * fwhml**3
        + 0.07842 * fwhmg * fwhml**4
        + fwhml**5,
        0.2,
    )
    n = (
        1.36603 * (fwhml / f)
        - 0.47719 * (fwhml / f) ** 2
        + 0.11116 * (fwhml / f) ** 3
    )
    return np.array(
        n
        * DistributionModels.lorentzian(
            x=x, amplitude=amplitude, center=center, fwhml=fwhml
        )
        + (1 - n)
        * DistributionModels.gaussian(
            x=x, amplitude=amplitude, center=center, fwhmg=fwhmg
        )
    )

Return a 1-dimensional Voigt distribution.

\[ {\displaystyle V(x;\sigma ,\gamma )\equiv \int_{-\infty }^{\infty }G(x';\sigma ) L(x-x';\gamma )\,dx'} \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
center float

Center of the Voigt distribution. Defaults to 0.0.

0.0
fwhmv float

Full width at half maximum (FWHM) of the Lorentzian distribution. Defaults to 1.0.

1.0
gamma float

Scaling factor of the complex part of the Faddeeva Function. Defaults to None.

None

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Voigt distribution of x given.

Source code in spectrafit/models.py
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
@staticmethod
def voigt(
    x: NDArray[np.float64],
    center: float = 0.0,
    fwhmv: float = 1.0,
    gamma: Optional[float] = None,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional Voigt distribution.

    $$
    {\displaystyle V(x;\sigma ,\gamma )\equiv
    \int_{-\infty }^{\infty }G(x';\sigma )
    L(x-x';\gamma )\,dx'}
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        center (float, optional): Center of the Voigt distribution. Defaults to 0.0.
        fwhmv (float, optional): Full width at half maximum (FWHM) of the Lorentzian
            distribution. Defaults to 1.0.
        gamma (float, optional): Scaling factor of the complex part of the
            [Faddeeva Function](https://en.wikipedia.org/wiki/Faddeeva_function).
            Defaults to None.

    Returns:
        NDArray[np.float64]: Voigt distribution of `x` given.
    """
    sigma = fwhmv * Constants.fwhmv2sig
    if gamma is None:
        gamma = sigma
    z = (x - center + 1j * gamma) / (sigma * Constants.sq2)
    return np.array(wofz(z).real / (sigma * Constants.sq2pi))

Return a 1-dimensional exponential decay.

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the exponential function. Defaults to 1.0.

1.0
decay float

Decay of the exponential function. Defaults to 1.0.

1.0
intercept float

Intercept of the exponential function. Defaults to 0.0.

0.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Exponential decay of x given.

Source code in spectrafit/models.py
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
@staticmethod
def exponential(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    decay: float = 1.0,
    intercept: float = 0.0,
) -> NDArray[np.float64]:
    """Return a 1-dimensional exponential decay.

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the exponential function.
             Defaults to 1.0.
        decay (float, optional): Decay of the exponential function. Defaults to 1.0.
        intercept (float, optional): Intercept of the exponential function.
             Defaults to 0.0.

    Returns:
        NDArray[np.float64]: Exponential decay of `x` given.
    """
    return np.array(amplitude * np.exp(-x / decay) + intercept)

Return a 1-dimensional power function.

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the power function. Defaults to 1.0.

1.0
exponent float

Exponent of the power function. Defaults to 1.0.

1.0
intercept float

Intercept of the power function. Defaults to 0.0.

0.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: power function of x given.

Source code in spectrafit/models.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
@staticmethod
def power(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    exponent: float = 1.0,
    intercept: float = 0.0,
) -> NDArray[np.float64]:
    """Return a 1-dimensional power function.

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the power function. Defaults to
            1.0.
        exponent (float, optional): Exponent of the power function. Defaults to 1.0.
        intercept (float, optional): Intercept of the power function. Defaults to
            0.0.

    Returns:
        NDArray[np.float64]: power function of `x` given.
    """
    return np.array(amplitude * np.power(x, exponent) + intercept)

Return a 1-dimensional linear function.

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
slope float

Slope of the linear function. Defaults to 1.0.

1.0
intercept float

Intercept of the linear function. Defaults to 0.0.

0.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Linear function of x given.

Source code in spectrafit/models.py
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
@staticmethod
def linear(
    x: NDArray[np.float64],
    slope: float = 1.0,
    intercept: float = 0.0,
) -> NDArray[np.float64]:
    """Return a 1-dimensional linear function.

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        slope (float, optional): Slope of the linear function. Defaults to 1.0.
        intercept (float, optional): Intercept of the linear function.
             Defaults to 0.0.

    Returns:
        NDArray[np.float64]: Linear function of `x` given.
    """
    return np.array(slope * x + intercept)

Return a 1-dimensional constant value.

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the constant. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Constant value of x given.

Source code in spectrafit/models.py
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
@staticmethod
def constant(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
) -> NDArray[np.float64]:
    """Return a 1-dimensional constant value.

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the constant. Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Constant value of `x` given.
    """
    return np.array(np.linspace(amplitude, amplitude, len(x)))

Return a 1-dimensional error function.

\[ f(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the error function. Defaults to 1.0.

1.0
center float

Center of the error function. Defaults to 0.0.

0.0
sigma float

Sigma of the error function. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Error function of x given.

Source code in spectrafit/models.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
@staticmethod
def erf(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    sigma: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional error function.

    $$
    f(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the error function.
                Defaults to 1.0.
        center (float, optional): Center of the error function. Defaults to 0.0.
        sigma (float, optional): Sigma of the error function. Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Error function of `x` given.
    """
    return np.array(
        amplitude * 0.5 * (1 + erf(DistributionModels._norm(x, center, sigma)))
    )

Return a 1-dimensional Heaviside step function.

\[ f(x) = \begin{cases} 0 & x < 0 \\ 0.5 & x = 0 \\ 1 & x > 0 \end{cases} \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the Heaviside step function. Defaults to 1.0.

1.0
center float

Center of the Heaviside step function. Defaults to 0.0.

0.0
sigma float

Sigma of the Heaviside step function. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Heaviside step function of x given.

Source code in spectrafit/models.py
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
@staticmethod
def heaviside(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    sigma: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional Heaviside step function.

    $$
    f(x) = \begin{cases}
    0 & x < 0 \\
    0.5 & x = 0 \\
    1 & x > 0
    \end{cases}
    $$
    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the Heaviside step function.
                Defaults to 1.0.
        center (float, optional): Center of the Heaviside step function.
             Defaults to 0.0.
        sigma (float, optional): Sigma of the Heaviside step function.
             Defaults to 1.0.


    Returns:
        NDArray[np.float64]: Heaviside step function of `x` given.
    """
    return np.array(
        amplitude * 0.5 * (1 + np.sign(DistributionModels._norm(x, center, sigma)))
    )

Return a 1-dimensional arctan step function.

\[ f(x) = \frac{1}{\pi} \arctan(\frac{x - c}{s}) \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the arctan step function. Defaults to 1.0.

1.0
center float

Center of the arctan step function. Defaults to 0.0.

0.0
sigma float

Sigma of the arctan step function. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Arctan step function of x given.

Source code in spectrafit/models.py
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
@staticmethod
def atan(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    sigma: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional arctan step function.

    $$
    f(x) = \frac{1}{\pi} \arctan(\frac{x - c}{s})
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the arctan step function.
                Defaults to 1.0.
        center (float, optional): Center of the arctan step function.
             Defaults to 0.0.
        sigma (float, optional): Sigma of the arctan step function.
             Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Arctan step function of `x` given.
    """
    return np.array(
        amplitude
        * 0.5
        * (1 + np.arctan(DistributionModels._norm(x, center, sigma)) / pi)
    )

Return a 1-dimensional logarithmic step function.

\[ f(x) = \frac{1}{1 + e^{-\frac{x - c}{s}}} \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the logarithmic step function. Defaults to 1.0.

1.0
center float

Center of the logarithmic step function. Defaults to 0.0.

0.0
sigma float

Sigma of the logarithmic step function. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Logarithmic step function of x given.

Source code in spectrafit/models.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
@staticmethod
def log(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    sigma: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional logarithmic step function.

    $$
    f(x) = \frac{1}{1 + e^{-\frac{x - c}{s}}}
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the logarithmic step function.
                Defaults to 1.0.
        center (float, optional): Center of the logarithmic step function.
             Defaults to 0.0.
        sigma (float, optional): Sigma of the logarithmic step function.
             Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Logarithmic step function of `x` given.
    """
    return np.array(
        amplitude
        * 0.5
        * (1 + np.log(DistributionModels._norm(x, center, sigma)) / pi)
    )

Return a 1-dimensional cumulative Gaussian function.

\[ f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right] \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the Gaussian function. Defaults to 1.0.

1.0
center float

Center of the Gaussian function. Defaults to 0.0.

0.0
fwhmg float

Full width at half maximum of the Gaussian function. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Cumulative Gaussian function of x given.

Source code in spectrafit/models.py
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
@staticmethod
def cgaussian(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    fwhmg: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional cumulative Gaussian function.

    $$
    f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right]
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the Gaussian function. Defaults to
            1.0.
        center (float, optional): Center of the Gaussian function. Defaults to 0.0.
        fwhmg (float, optional): Full width at half maximum of the Gaussian
             function. Defaults to 1.0.


    Returns:
        NDArray[np.float64]: Cumulative Gaussian function of `x` given.
    """
    sigma = fwhmg * Constants.fwhmg2sig
    return np.array(
        amplitude * 0.5 * (1 + erf((x - center) / (sigma * np.sqrt(2.0))))
    )

Return a 1-dimensional cumulative Lorentzian function.

\[ f(x) = \frac{1}{\pi} \arctan\left(\frac{x - c}{s}\right) + \frac{1}{2} \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the Lorentzian function. Defaults to 1.0.

1.0
center float

Center of the Lorentzian function. Defaults to 0.0.

0.0
fwhml float

Full width at half maximum of the Lorentzian function. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Cumulative Lorentzian function of x given.

Source code in spectrafit/models.py
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
@staticmethod
def clorentzian(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    fwhml: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional cumulative Lorentzian function.

    $$
    f(x) = \frac{1}{\pi} \arctan\left(\frac{x - c}{s}\right) + \frac{1}{2}
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the Lorentzian function.
                Defaults to 1.0.
        center (float, optional): Center of the Lorentzian function.
             Defaults to 0.0.
        fwhml (float, optional): Full width at half maximum of the Lorentzian
            function. Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Cumulative Lorentzian function of `x` given.
    """
    sigma = fwhml * Constants.fwhml2sig
    return np.array(amplitude * (np.arctan((x - center) / sigma) / pi) + 0.5)

Return a 1-dimensional cumulative Voigt function.

\[ f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right] \]

Parameters:

Name Type Description Default
x NDArray[np.float64]

x-values of the data.

required
amplitude float

Amplitude of the Voigt function. Defaults to 1.0.

1.0
center float

Center of the Voigt function. Defaults to 0.0.

0.0
fwhmv float

Full width at half maximum of the Voigt function. Defaults to 1.0.

1.0
gamma float

Gamma of the Voigt function. Defaults to 1.0.

1.0

Returns:

Type Description
NDArray[np.float64]

NDArray[np.float64]: Cumulative Voigt function of x given.

Source code in spectrafit/models.py
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
@staticmethod
def cvoigt(
    x: NDArray[np.float64],
    amplitude: float = 1.0,
    center: float = 0.0,
    fwhmv: float = 1.0,
    gamma: float = 1.0,
) -> NDArray[np.float64]:
    r"""Return a 1-dimensional cumulative Voigt function.

    $$
    f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right]
    $$

    Args:
        x (NDArray[np.float64]): `x`-values of the data.
        amplitude (float, optional): Amplitude of the Voigt function. Defaults to
            1.0.
        center (float, optional): Center of the Voigt function. Defaults to 0.0.
        fwhmv (float, optional): Full width at half maximum of the Voigt function.
            Defaults to 1.0.
        gamma (float, optional): Gamma of the Voigt function. Defaults to 1.0.

    Returns:
        NDArray[np.float64]: Cumulative Voigt function of `x` given.
    """
    sigma = fwhmv * Constants.fwhmv2sig
    return np.array(
        amplitude
        * 0.5
        * (1 + erf((x - center) / (sigma * np.sqrt(2.0))))
        * np.exp(-(((x - center) / gamma) ** 2))
    )

Important constants for the models

For calculating the models a few math constants are needed, which are implemented in the constants module.

Mathematical constants for the curve models.

Constants

  1. Natural logarithm of 2

    \[ ln2 = \log{2} \]
  2. Square root of 2 times pi

    \[ sq2pi = \sqrt{2 \pi} \]
  3. Square root of pi

    \[ sqpi = \sqrt{ \pi} \]
  4. Square root of 2

    \[ sq2 = \sqrt{2} \]
  5. Full width at half maximum to sigma for Gaussian

    \[ fwhmg2sig = \frac{1}{ 2 \sqrt{2\log{2}}} \]
  6. Full width at half maximum to sigma for Lorentzian

    \[ fwhml2sig = \frac{1}{2} \]
  7. Full width at half maximum to sigma for Voigt according to the article by Olivero and Longbothum1, check also XPSLibary website.

    $$ fwhm_{\text{Voigt}} \approx 0.5346 \cdot fwhm_{\text{Gaussian}} + \sqrt{ 0.2166 fwhm_{\text{Lorentzian}}^2 + fwhm_{\text{Gaussian}}^2 }

    $$

    In case of equal FWHM for Gaussian and Lorentzian, the Voigt FWHM can be defined as:

    \[ fwhm_{\text{Voigt}} \approx 1.0692 + 2 \sqrt{0.2166 + 2 \ln{2}} \cdot \sigma \]
    \[ fwhmv2sig = \frac{1}{fwhm_{\text{Voigt}}} \]

  1. J.J. Olivero, R.L. Longbothum, Empirical fits to the Voigt line width: A brief review, Journal of Quantitative Spectroscopy and Radiative Transfer, Volume 17, Issue 2, 1977, Pages 233-236, ISSN 0022-4073, https://doi.org/10.1016/0022-4073(77)90161-3

Source code in spectrafit/models.py
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
@dataclass(frozen=True)
class Constants:
    r"""Mathematical constants for the curve models.

    !!! info "Constants"

        1. Natural logarithm of 2

            $$
            ln2 = \log{2}
            $$

        2. Square root of 2 times pi

            $$
            sq2pi = \sqrt{2 \pi}
            $$

        3. Square root of pi

            $$
            sqpi = \sqrt{ \pi}
            $$

        4. Square root of 2

            $$
            sq2 = \sqrt{2}
            $$

        5. Full width at half maximum to sigma for Gaussian

            $$
            fwhmg2sig = \frac{1}{ 2 \sqrt{2\log{2}}}
            $$

        6. Full width at half maximum to sigma for Lorentzian

            $$
            fwhml2sig = \frac{1}{2}
            $$

        7. Full width at half maximum to sigma for Voigt according to the article by
            Olivero and Longbothum[^1], check also
            [XPSLibary website](https://xpslibrary.com/voigt-peak-shape/).

            $$
            fwhm_{\text{Voigt}} \approx 0.5346 \cdot fwhm_{\text{Gaussian}} +
              \sqrt{ 0.2166 fwhm_{\text{Lorentzian}}^2  + fwhm_{\text{Gaussian}}^2 }

            $$

            In case of equal FWHM for Gaussian and Lorentzian, the Voigt FWHM can be
            defined as:

            $$
            fwhm_{\text{Voigt}} \approx 1.0692 + 2 \sqrt{0.2166 + 2 \ln{2}} \cdot \sigma
            $$

            $$
            fwhmv2sig = \frac{1}{fwhm_{\text{Voigt}}}
            $$

        [^1]:
            J.J. Olivero, R.L. Longbothum,
            _Empirical fits to the Voigt line width: A brief review_,
            **Journal of Quantitative Spectroscopy and Radiative Transfer**,
            Volume 17, Issue 2, 1977, Pages 233-236, ISSN 0022-4073,
            https://doi.org/10.1016/0022-4073(77)90161-3.
    """

    ln2 = log(2.0)
    sq2pi = sqrt(2.0 * pi)
    sqpi = sqrt(pi)
    sq2 = sqrt(2.0)
    fwhmg2sig = 1 / (2.0 * sqrt(2.0 * log(2.0)))
    fwhml2sig = 1 / 2.0
    fwhmv2sig = 1 / (2 * 0.5346 + 2 * sqrt(0.2166 + log(2) * 2))