Peak fitting
Rampy will soon offer a dedicated function for peak fitting: rampy.fit_peaks
will use curve_fit from scipy.optimize to fit your spectrum with a model that is a sum of peaks.
Users can also use dedicated interfaces such as lmfit. We provide an example of peak-fitting with the lmfit for instance here: Example notebooks .
Peak shapes
Rampy offers functions for various peak shapes, including:
gaussian peaks >
rampy.gaussian
lorentzian peaks >
rampy.lorentzian
pseudo-voigt peaks >
rampy.pseudovoigt
pearson7 peaks >
rampy.pearson7
- rampy.gaussian(x: ndarray, amp, freq, HWHM) ndarray
Computes a Gaussian peak.
- Parameters:
x (np.ndarray) – x axis
amp (float or np.ndarray) – Peak amplitude
freq (float or np.ndarray) – Peak position
HWHM (float or np.ndarray) – Peak half-width at half-maximum.
- Returns:
calculated peak.
- Return type:
np.ndarray
Notes
Formula used: ( ext{amp} cdot exp(-log(2) cdot ((x - ext{freq}) / ext{HWHM})^2) ).
Examples
You can create a single peak like:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.gaussian(x, 10., 3., 1.0)
You can also create an array with several peaks in columns using arrays for the peak parameters:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.gaussian(x.reshape(-1, 1), np.array([[10, 10.]]), np.array([[3, 7.]]), np.array([[1., 1.]]))
In this case, y will be an array of shape (len(x), 2) with one peak per columns. Your peaks can even share parameters, here the HWHM:
>>> y = rampy.gaussian(x.reshape(-1, 1), np.array([[10, 10.]]), np.array([[3, 7.]]), 2.0)
The composite signal is simply given by
>>> y_sum = np.sum(y, axis=1)
- rampy.lorentzian(x: ndarray, amp, freq, HWHM) ndarray
Computes a Lorentzian peak.
- Parameters:
x (np.ndarray) – x axis
amp (float or np.ndarray) – Peak amplitude
freq (float or np.ndarray) – Peak position
HWHM (float or np.ndarray) – Peak half-width at half-maximum.
- Returns:
calculated peak.
- Return type:
np.ndarray
Notes
Formula used: ( ext{amp} / (1 + ((x - ext{freq}) / ext{HWHM})^2) ).
Examples
You can create a single peak like:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.lorentzian(x, 10., 3., 1.0)
You can also create an array with several peaks in columns using arrays for the peak parameters:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.lorentzian(x.reshape(-1, 1), np.array([[10, 10.]]), np.array([[3, 7.]]), np.array([[1., 1.]]))
In this case, y will be an array of shape (len(x), 2) with one peak per columns. Your peaks can even share parameters, here the HWHM:
>>> y = rampy.lorentzian(x.reshape(-1, 1), np.array([[10, 10.]]), np.array([[3, 7.]]), 2.0)
The composite signal is simply given by
>>> y_sum = np.sum(y, axis=1)
- rampy.pseudovoigt(x: ndarray, amp, freq, HWHM, L_ratio) ndarray
Computes a pseudo-Voigt peak.
- Parameters:
x (np.ndarray) – x axis
amp (float or np.ndarray) – Peak amplitude
freq (float or np.ndarray) – Peak position
HWHM (float or np.ndarray) – Peak half-width at half-maximum.
L_ratio (float) – Ratio of the Lorentzian component, must be between 0 and 1 (inclusive).
- Returns:
The computed pseudo-Voigt signal.
- Return type:
np.ndarray
- Raises:
ValueError – If L_ratio is not between 0 and 1.
Notes
- Formula used: ( (1 - L_{ ext{ratio}}) cdot ext{gaussian}(x, ext{amp}, ext{freq}, ext{HWHM}) +
L_{ ext{ratio}} cdot ext{lorentzian}(x, ext{amp}, ext{freq}, ext{HWHM}) ).
Examples
You can create a single peak like:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.pseudovoigt(x, 10., 3., 1.0, 0.5)
You can also create an array with several peaks in columns using arrays for the peak parameters:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.pseudovoigt(x.reshape(-1, 1), np.array([[10, 10]]), np.array([[3, 7]]), np.array([[1., 1.]]), np.array([[0.5, 0.5]]))
In this case, y will be an array of shape (len(x), 2) with one peak per columns. Your peaks can even share parameters, here the L_ratio:
>>> y = rampy.pseudovoigt(x.reshape(-1, 1), np.array([[10, 10]]), np.array([[3, 7]]), np.array([[1., 1.]]), 0.5)
The composite signal is simply given by
>>> y_sum = np.sum(y, axis=1)
- rampy.pearson7(x, a0, a1, a2, a3)
Computes a Pearson VII peak.
- Parameters:
x (np.ndarray) – Positions at which the signal should be sampled.
x – x axis
a0 (float or np.ndarray) – Peak amplitude
a1 (float or np.ndarray) – Peak position
a2 (float or np.ndarray) – Peak width
a3 (float or np.ndarray) – Peak shape parameter.
- Returns:
The computed Pearson VII signal.
- Return type:
np.ndarray
Notes
Formula used: ( a_0 / ((1 + ((x - a_1) / a_2)^2 cdot (2^{(1/a_3)} - 1))^{a_3}) ).
Examples
You can create a single peak like:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.pearson7(x, 10., 3., 1.0, 0.5)
You can also create an array with several peaks in columns using arrays for the peak parameters:
>>> x = np.linspace(0, 10, 1000) >>> y = rampy.pearson7(x.reshape(-1, 1), np.array([[10, 10]]), np.array([[3, 7]]), np.array([[1., 1.]]), np.array([[0.5, 0.5]]))
In this case, y will be an array of shape (len(x), 2) with one peak per columns. Your peaks can even share parameters, here the a3:
>>> y = rampy.pearson7(x.reshape(-1, 1), np.array([[10, 10]]), np.array([[3, 7]]), np.array([[1., 1.]]), 0.5)
The composite signal is simply given by
>>> y_sum = np.sum(y, axis=1)
Peak areas
Peak area can be calculated using the rampy.area_peak
function, using analytical functions.
Note that the old function rampy.peakarea
is still available but it will be removed in a near future. It uses numerical integration and is thus less precise than the rampy.area_peak
function.
- rampy.area_peak(peak_type: Literal['gaussian', 'lorentzian', 'pseudovoigt', 'pearson7'], amplitude: float, hwhm: float, *, lorentzian_fraction: float | None = None, exponent: float | None = None) float
Calculates the analytical area under a peak based on its type and parameters.
- Parameters:
peak_type – Type of peak (gaussian, lorentzian, pseudovoigt, pearson7)
amplitude – Amplitude of the peak (maximum height)
hwhm – Half-width at half-maximum of the peak
lorentzian_fraction – Lorentzian fraction for Pseudo-Voigt [0, 1]
exponent – Shape parameter for Pearson VII
- Returns:
Area under the specified peak
- Raises:
ValueError – For invalid arguments or missing required parameters
Examples
>>> area_peaks("gaussian", 2.0, 0.5) 2.3548200450309493
>>> area_peaks("pseudovoigt", 2.0, 0.5, lorentzian_fraction=0.5) 3.141592653589793
Propagate uncertainties
A good way to propagate the uncertainties of your model is to directly use the uncertainties
package, see the docs here.