Skip to content

Instantly share code, notes, and snippets.

@AEljarrat
Created September 16, 2019 07:08
Show Gist options
  • Save AEljarrat/ec5c927e41e6ddd9c6b0c6699a5d697e to your computer and use it in GitHub Desktop.
Save AEljarrat/ec5c927e41e6ddd9c6b0c6699a5d697e to your computer and use it in GitHub Desktop.
Voigt Expression
import numpy as np
from hyperspy._components.expression import Expression
from distutils.version import LooseVersion
import sympy
class Voigt(Expression):
r"""
Voigt component
---------------
Symmetric peak shape based on the convolution of a Lorentzian and Normal
distribution. In this case using an approximate formula by David (see Notes)
.. math::
z(x) &= \frac{x + i \gamma}{\sqrt{2} \sigma} \\
w(z) &= \frac{e^{-z^2} \text{erf}(-i z)}{\sqrt{2 \pi} \sigma} \\
f(x) &= A \Re\left\{ w \left[ z(x-x_0) \right] \right\}
============== =============
Variable Parameter
============== =============
:math:`x_0` centre
:math:`A` area
:math:`\gamma` gamma
:math:`\sigma` fwhm
============== =============
Parameters
-----------
centre : float
Location of the maximum of the peak.
area : float
Intensity below the peak.
lwidth : float
HWHM of the Lorentzian distribution
gwidth: float
FWHM of the Gaussian distribution
Notes
-----
W.I.F. David, J. Appl. Cryst. (1986). 19, 63-64
"""
def __init__(self, centre=0., area=1., lwidth=0.2, gwidth=0.05,
module="scipy", **kwargs):
if LooseVersion(sympy.__version__) < LooseVersion("1.3"):
raise ImportError("The `Voigt` component requires "
"SymPy >= 1.3")
super(Voigt, self).__init__(
expression="area * real(V); \
V = wofz(z) / (sqrt(2.0 * 3.14159265359) * sigma); \
z = (x - centre + 1j * lwidth) / (sigma * sqrt(2.0)); \
sigma = gwidth / 2.3548200450309493",
name="Voigt",
centre=centre,
area=area,
lwidth=lwidth,
gwidth=gwidth,
module=module,
autodoc=False,
**kwargs,
)
# Boundaries
self.area.bmin = 0.
self.lwidth.bmin = 0.
self.gwidth.bmin = 0.
self.isbackground = False
self.convolved = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment