hyperspy._components.voigt module

class hyperspy._components.voigt.Voigt(centre=10.0, area=1.0, gamma=0.2, sigma=0.1, module=['numpy', 'scipy'], **kwargs)

Bases: hyperspy._components.expression.Expression

Voigt component.

Symmetric peak shape based on the convolution of a Lorentzian and Normal (Gaussian) distribution:

f(x) = G(x) \cdot L(x)

where G(x) is the Gaussian function and L(x) is the Lorentzian function. In this case using an approximate formula by David (see Notes). This approximation improves on the pseudo-Voigt function (linear combination instead of convolution of the distributions) and is, to a very good approximation, equivalent to a Voigt function:

z(x) &= \frac{x + i \gamma}{\sqrt{2} \sigma} \\
w(z) &= \frac{e^{-z^2} \text{erfc}(-i z)}{\sqrt{2 \pi} \sigma} \\
f(x) &= A \cdot \Re\left\{ w \left[ z(x - x_0) \right] \right\}











  • centre (float) – Location of the maximum of the peak.

  • area (float) – Intensity below the peak.

  • gamma (float) – \gamma = HWHM of the Lorentzian distribution.

  • sigma (float) – 2 \sigma \sqrt{(2 \log(2))} = FWHM of the Gaussian distribution.

For convenience the gwidth and lwidth attributes can also be used to set and get the FWHM of the Gaussian and Lorentzian parts of the distribution, respectively. For backwards compatability, FWHM is another alias for the Gaussian width.


W.I.F. David, J. Appl. Cryst. (1986). 19, 63-64, doi:10.1107/S0021889886089999

Create a component from a string expression.

It automatically generates the partial derivatives and the class docstring.

  • expression (str) – Component function in SymPy text expression format with substitutions separated by ;. See examples and the SymPy documentation for details. In order to vary the components along the signal dimensions, the variables x and y must be included for 1D or 2D components. Also, if module is “numexpr” the functions are limited to those that numexpr support. See its documentation for details.

  • name (str) – Name of the component.

  • position (str, optional) – The parameter name that defines the position of the component if applicable. It enables interative adjustment of the position of the component in the model. For 2D components, a tuple must be passed with the name of the two parameters e.g. (“x0”, “y0”).

  • module ({"numpy", "numexpr", "scipy"}, default "numpy") – Module used to evaluate the function. numexpr is often faster but it supports fewer functions and requires installing numexpr.

  • add_rotation (bool, default False) – This is only relevant for 2D components. If True it automatically adds rotation_angle parameter.

  • rotation_center ({None, tuple}) – If None, the rotation center is the center i.e. (0, 0) if position is not defined, otherwise the center is the coordinates specified by position. Alternatively a tuple with the (x, y) coordinates of the center can be provided.

  • rename_pars (dictionary) – The desired name of a parameter may sometimes coincide with e.g. the name of a scientific function, what prevents using it in the expression. rename_parameters is a dictionary to map the name of the parameter in the expression` to the desired name of the parameter in the Component. For example: {“_gamma”: “gamma”}.

  • compute_gradients (bool, optional) – If True, compute the gradient automatically using sympy. If sympy does not support the calculation of the partial derivatives, for example in case of expression containing a “where” condition, it can be disabled by using compute_gradients=False.

  • **kwargs – Keyword arguments can be used to initialise the value of the parameters.


As of version 1.4, Sympy’s lambdify function, that the Expression components uses internally, does not support the differentiation of some expressions, for example those containing a “where” condition. In such cases, the gradients can be set manually if required.


The following creates a Gaussian component and set the initial value of the parameters:

>>> hs.model.components1D.Expression(
... expression="height * exp(-(x - x0) ** 2 * 4 * log(2)/ fwhm ** 2)",
... name="Gaussian",
... height=1,
... fwhm=1,
... x0=0,
... position="x0",)

Substitutions for long or complicated expressions are separated by semicolumns:

>>> expr = 'A*B/(A+B) ; A = sin(x)+one; B = cos(y) - two; y = tan(x)'
>>> comp = hs.model.components1D.Expression(
... expression=expr,
... name='my function')
>>> comp.parameters
(<Parameter one of my function component>,
 <Parameter two of my function component>)
estimate_parameters(signal, x1, x2, only_current=False)

Estimate the Voigt function by calculating the momenta of the Gaussian.

  • signal (Signal1D instance) –

  • x1 (float) – Defines the left limit of the spectral range to use for the estimation.

  • x2 (float) – Defines the right limit of the spectral range to use for the estimation.

  • only_current (bool) – If False estimates the parameters for the full dataset.


Exit status required for the remove_background() function.

Return type



Adapted from http://www.scipy.org/Cookbook/FittingData


>>> g = hs.model.components1D.Voigt(legacy=False)
>>> x = np.arange(-10, 10, 0.01)
>>> data = np.zeros((32, 32, 2000))
>>> data[:] = g.function(x).reshape((1, 1, 2000))
>>> s = hs.signals.Signal1D(data)
>>> s.axes_manager[-1].offset = -10
>>> s.axes_manager[-1].scale = 0.01
>>> g.estimate_parameters(s, -10, 10, False)