{"id":12555,"date":"2023-09-11T17:15:20","date_gmt":"2023-09-11T17:15:20","guid":{"rendered":"https:\/\/www.digitaldesignjournal.com\/?p=12555"},"modified":"2023-09-15T08:26:46","modified_gmt":"2023-09-15T08:26:46","slug":"python-voigt-profile","status":"publish","type":"post","link":"https:\/\/www.digitaldesignjournal.com\/python-voigt-profile\/","title":{"rendered":"Python voigt Profile [Explained With Examples]"},"content":{"rendered":"\n<p>A Voigt profile, also known as a Voigt function or Voigt distribution, is a convolution of a Gaussian distribution and a Lorentzian distribution.<\/p>\n\n\n\n<p> It is often used in spectroscopy and other fields to model spectral lines that exhibit both Gaussian and Lorentzian broadening. <\/p>\n\n\n\n<p>You can create a Voigt profile in Python using various libraries, but I&#8217;ll show you how to do it using the <code>scipy<\/code> library, which provides a built-in function for generating Voigt profiles.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to create a Voigt profile using <code>scipy<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n<span class=\"hljs-keyword\">from<\/span> scipy.special <span class=\"hljs-keyword\">import<\/span> wofz\n<span class=\"hljs-keyword\">import<\/span> matplotlib.pyplot <span class=\"hljs-keyword\">as<\/span> plt\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">voigt_profile<\/span><span class=\"hljs-params\">(x, sigma, gamma)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\"\n    Calculate the Voigt profile.\n\n    Parameters:\n        x (array-like): The x-values at which to calculate the profile.\n        sigma (float): The Gaussian standard deviation.\n        gamma (float): The Lorentzian full-width at half-maximum.\n\n    Returns:\n        array-like: The Voigt profile values at the specified x-values.\n    \"\"\"<\/span>\n    z = (x + <span class=\"hljs-number\">1j<\/span> * gamma) \/ (sigma * np.sqrt(<span class=\"hljs-number\">2<\/span>))\n    v = wofz(z).real \/ (sigma * np.sqrt(<span class=\"hljs-number\">2<\/span> * np.pi))\n    <span class=\"hljs-keyword\">return<\/span> v\n\n<span class=\"hljs-comment\"># Define parameters<\/span>\nx = np.linspace(<span class=\"hljs-number\">-5<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">1000<\/span>)  <span class=\"hljs-comment\"># X-values<\/span>\nsigma = <span class=\"hljs-number\">1.0<\/span>                   <span class=\"hljs-comment\"># Gaussian standard deviation<\/span>\ngamma = <span class=\"hljs-number\">0.5<\/span>                   <span class=\"hljs-comment\"># Lorentzian full-width at half-maximum<\/span>\n\n<span class=\"hljs-comment\"># Calculate the Voigt profile<\/span>\nprofile = voigt_profile(x, sigma, gamma)\n\n<span class=\"hljs-comment\"># Plot the Voigt profile<\/span>\nplt.plot(x, profile, label=<span class=\"hljs-string\">'Voigt Profile'<\/span>)\nplt.xlabel(<span class=\"hljs-string\">'X'<\/span>)\nplt.ylabel(<span class=\"hljs-string\">'Intensity'<\/span>)\nplt.legend()\nplt.title(<span class=\"hljs-string\">'Voigt Profile'<\/span>)\nplt.grid(<span class=\"hljs-literal\">True<\/span>)\nplt.show()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We import the necessary libraries, including <code>numpy<\/code> for numerical operations, <code>scipy.special.wofz<\/code> for the Faddeeva function (required for the Voigt profile calculation), and <code>matplotlib<\/code> for plotting.<\/li>\n\n\n\n<li>We define a <code>voigt_profile<\/code> function that calculates the Voigt profile at a given set of x-values using the formula involving the Faddeeva function.<\/li>\n\n\n\n<li>We specify the parameters <code>sigma<\/code> (Gaussian standard deviation) and <code>gamma<\/code> (Lorentzian full-width at half-maximum).<\/li>\n\n\n\n<li>We calculate the Voigt profile for the specified x-values.<\/li>\n\n\n\n<li>Finally, we plot the Voigt profile using Matplotlib.<\/li>\n<\/ol>\n\n\n\n<p>You can adjust the <code>sigma<\/code> and <code>gamma<\/code> parameters to see how they affect the shape of the Voigt profile.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fitting the data with a voigt function in python<\/h2>\n\n\n\n<p>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 <code>scipy.optimize.curve_fit<\/code> to find the parameters that best describe your data. Here&#8217;s an example of how to fit data with a Voigt function using <code>scipy<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n<span class=\"hljs-keyword\">from<\/span> scipy.optimize <span class=\"hljs-keyword\">import<\/span> curve_fit\n<span class=\"hljs-keyword\">import<\/span> matplotlib.pyplot <span class=\"hljs-keyword\">as<\/span> plt\n<span class=\"hljs-keyword\">from<\/span> scipy.special <span class=\"hljs-keyword\">import<\/span> wofz\n\n<span class=\"hljs-comment\"># Define the Voigt profile function<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">voigt_profile<\/span><span class=\"hljs-params\">(x, amplitude, center, sigma, gamma)<\/span>:<\/span>\n    z = ((x - center) + <span class=\"hljs-number\">1j<\/span> * gamma) \/ (sigma * np.sqrt(<span class=\"hljs-number\">2<\/span>))\n    v = amplitude * wofz(z).real \/ (sigma * np.sqrt(<span class=\"hljs-number\">2<\/span> * np.pi))\n    <span class=\"hljs-keyword\">return<\/span> v\n\n<span class=\"hljs-comment\"># Generate some synthetic data<\/span>\nx_data = np.linspace(<span class=\"hljs-number\">-5<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">100<\/span>)\ny_data = voigt_profile(x_data, amplitude=<span class=\"hljs-number\">1.0<\/span>, center=<span class=\"hljs-number\">0.0<\/span>, sigma=<span class=\"hljs-number\">1.0<\/span>, gamma=<span class=\"hljs-number\">0.5<\/span>) + np.random.normal(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0.05<\/span>, len(x_data))\n\n<span class=\"hljs-comment\"># Fit the data to the Voigt profile function<\/span>\ninitial_guess = (<span class=\"hljs-number\">1.0<\/span>, <span class=\"hljs-number\">0.0<\/span>, <span class=\"hljs-number\">1.0<\/span>, <span class=\"hljs-number\">0.5<\/span>)  <span class=\"hljs-comment\"># Initial parameter guess<\/span>\nparams, covariance = curve_fit(voigt_profile, x_data, y_data, p0=initial_guess)\n\n<span class=\"hljs-comment\"># Extract the fitted parameters<\/span>\namplitude_fit, center_fit, sigma_fit, gamma_fit = params\n\n<span class=\"hljs-comment\"># Plot the original data and the fitted Voigt profile<\/span>\nplt.figure(figsize=(<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">6<\/span>))\nplt.plot(x_data, y_data, <span class=\"hljs-string\">'b.'<\/span>, label=<span class=\"hljs-string\">'Data'<\/span>)\nplt.plot(x_data, voigt_profile(x_data, *params), <span class=\"hljs-string\">'r-'<\/span>, label=<span class=\"hljs-string\">'Fit'<\/span>)\nplt.xlabel(<span class=\"hljs-string\">'X'<\/span>)\nplt.ylabel(<span class=\"hljs-string\">'Intensity'<\/span>)\nplt.legend()\nplt.title(<span class=\"hljs-string\">'Voigt Function Fit'<\/span>)\nplt.grid(<span class=\"hljs-literal\">True<\/span>)\nplt.show()\n\n<span class=\"hljs-comment\"># Print the fitted parameters<\/span>\nprint(<span class=\"hljs-string\">\"Amplitude:\"<\/span>, amplitude_fit)\nprint(<span class=\"hljs-string\">\"Center:\"<\/span>, center_fit)\nprint(<span class=\"hljs-string\">\"Sigma:\"<\/span>, sigma_fit)\nprint(<span class=\"hljs-string\">\"Gamma:\"<\/span>, gamma_fit)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We define the <code>voigt_profile<\/code> function as before, which calculates the Voigt profile.<\/li>\n\n\n\n<li>We generate synthetic data with some added noise.<\/li>\n\n\n\n<li>We use <code>curve_fit<\/code> from <code>scipy.optimize<\/code> to fit the synthetic data to the Voigt function. We provide an initial guess for the parameters.<\/li>\n\n\n\n<li>We plot both the original data and the fitted Voigt profile.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<p>You can adapt this code to fit your own data by replacing <code>x_data<\/code> and <code>y_data<\/code> with your actual data points. Adjust the initial guess as needed to improve the fit.<\/p>\n\n\n\n<p><strong>Read More;<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-cprofile-alternative\/\">Best Python cProfile Alternative<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-cprofile-filter\/\">Python cProfile Filter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-cprofile-gunicorn\/\">Python cProfile Gunicorn With Example<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-profile-guided-optimization\/\">Python Profile Guided Optimization<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/profiling-in-fastapi-python-applications\/\">Profiling in FastAPI Python Applications<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-cprofile-export-with-example\/\">Python cProfile Export With Example<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-error-attribute-error-enter-solved\/\">Python Error: \u201cAttributeError: __enter__\u201d<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/subprocess-exited-with-error-in-python\/\">subprocess-exited-with-error in Python<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/how-to-use-python-cprofile-async\/\">How to Use Python cProfile async<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-profile-likelihood\/\">Python Profile likelihood<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/python-elevation-profile-with-example\/\">Python Elevation Profile With Example<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.digitaldesignjournal.com\/pythons-pandas-library-vs-pandas-profiling-explained\/\">Python\u2019s Pandas Library vs Pandas Profiling<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A Voigt profile, also known as a Voigt function or Voigt distribution, is a convolution of a Gaussian distribution and &#8230; <a title=\"Python voigt Profile [Explained With Examples]\" class=\"read-more\" href=\"https:\/\/www.digitaldesignjournal.com\/python-voigt-profile\/\" aria-label=\"More on Python voigt Profile [Explained With Examples]\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":12557,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"ppma_author":[148],"class_list":["post-12555","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","author-abdullah-walied-allama"],"authors":[{"term_id":148,"user_id":12,"is_guest":0,"slug":"abdullah-walied-allama","display_name":"Abdullah Walied Allama","avatar_url":{"url":"https:\/\/www.digitaldesignjournal.com\/wp-content\/uploads\/2023\/08\/Abdullah-Walied-Allama.jpg","url2x":"https:\/\/www.digitaldesignjournal.com\/wp-content\/uploads\/2023\/08\/Abdullah-Walied-Allama.jpg"},"0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/posts\/12555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/comments?post=12555"}],"version-history":[{"count":5,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/posts\/12555\/revisions"}],"predecessor-version":[{"id":12671,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/posts\/12555\/revisions\/12671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/media\/12557"}],"wp:attachment":[{"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/media?parent=12555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/categories?post=12555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/tags?post=12555"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.digitaldesignjournal.com\/wp-json\/wp\/v2\/ppma_author?post=12555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}