Python voigt Profile [Explained With Examples]

A Voigt profile, also known as a Voigt function or Voigt distribution, is a convolution of a Gaussian distribution and a Lorentzian distribution.

It is often used in spectroscopy and other fields to model spectral lines that exhibit both Gaussian and Lorentzian broadening.

You can create a Voigt profile in Python using various libraries, but I’ll show you how to do it using the scipy library, which provides a built-in function for generating Voigt profiles.

Here’s an example of how to create a Voigt profile using scipy:

import numpy as np
from scipy.special import wofz
import matplotlib.pyplot as plt

def voigt_profile(x, sigma, gamma):
    Calculate the Voigt profile.

        x (array-like): The x-values at which to calculate the profile.
        sigma (float): The Gaussian standard deviation.
        gamma (float): The Lorentzian full-width at half-maximum.

        array-like: The Voigt profile values at the specified x-values.
    z = (x + 1j * gamma) / (sigma * np.sqrt(2))
    v = wofz(z).real / (sigma * np.sqrt(2 * np.pi))
    return v

# Define parameters
x = np.linspace(-5, 5, 1000)  # X-values
sigma = 1.0                   # Gaussian standard deviation
gamma = 0.5                   # Lorentzian full-width at half-maximum

# Calculate the Voigt profile
profile = voigt_profile(x, sigma, gamma)

# Plot the Voigt profile
plt.plot(x, profile, label='Voigt Profile')
plt.title('Voigt Profile')
plt.grid(True) language: Python (python)

In this code:

  1. We import the necessary libraries, including numpy for numerical operations, scipy.special.wofz for the Faddeeva function (required for the Voigt profile calculation), and matplotlib for plotting.
  2. We define a voigt_profile function that calculates the Voigt profile at a given set of x-values using the formula involving the Faddeeva function.
  3. We specify the parameters sigma (Gaussian standard deviation) and gamma (Lorentzian full-width at half-maximum).
  4. We calculate the Voigt profile for the specified x-values.
  5. Finally, we plot the Voigt profile using Matplotlib.

You can adjust the sigma and gamma parameters to see how they affect the shape of the Voigt profile.

Fitting the data with a voigt function in python

Fitting data with a Voigt function in Python involves a different process than simply creating a Voigt profile. To fit data with a Voigt function, you typically use a curve fitting library like scipy.optimize.curve_fit to find the parameters that best describe your data. Here’s an example of how to fit data with a Voigt function using scipy:

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from scipy.special import wofz

# Define the Voigt profile function
def voigt_profile(x, amplitude, center, sigma, gamma):
    z = ((x - center) + 1j * gamma) / (sigma * np.sqrt(2))
    v = amplitude * wofz(z).real / (sigma * np.sqrt(2 * np.pi))
    return v

# Generate some synthetic data
x_data = np.linspace(-5, 5, 100)
y_data = voigt_profile(x_data, amplitude=1.0, center=0.0, sigma=1.0, gamma=0.5) + np.random.normal(0, 0.05, len(x_data))

# Fit the data to the Voigt profile function
initial_guess = (1.0, 0.0, 1.0, 0.5)  # Initial parameter guess
params, covariance = curve_fit(voigt_profile, x_data, y_data, p0=initial_guess)

# Extract the fitted parameters
amplitude_fit, center_fit, sigma_fit, gamma_fit = params

# Plot the original data and the fitted Voigt profile
plt.figure(figsize=(8, 6))
plt.plot(x_data, y_data, 'b.', label='Data')
plt.plot(x_data, voigt_profile(x_data, *params), 'r-', label='Fit')
plt.title('Voigt Function Fit')

# Print the fitted parameters
print("Amplitude:", amplitude_fit)
print("Center:", center_fit)
print("Sigma:", sigma_fit)
print("Gamma:", gamma_fit)Code language: Python (python)

In this code:

  1. We define the voigt_profile function as before, which calculates the Voigt profile.
  2. We generate synthetic data with some added noise.
  3. We use curve_fit from scipy.optimize to fit the synthetic data to the Voigt function. We provide an initial guess for the parameters.
  4. We plot both the original data and the fitted Voigt profile.
  5. We print the fitted parameters, which represent the amplitude, center, sigma (Gaussian standard deviation), and gamma (Lorentzian full-width at half-maximum) of the Voigt function.

You can adapt this code to fit your own data by replacing x_data and y_data with your actual data points. Adjust the initial guess as needed to improve the fit.

Read More;

  • Abdullah Walied Allama

    Abdullah Walied Allama is a driven programmer who earned his Bachelor's degree in Computer Science from Alexandria University's Faculty of Computer and Data Science. He is passionate about constructing problem-solving models and excels in various technical skills, including Python, data science, data analysis, Java, SQL, HTML, CSS, and JavaScript.

Leave a Comment