A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.
1 of 2

How to speed up Voigt profile fits?

Voigt profile fits in Mathematica seem terribly slow. For an example data set with 81 points a corresponding fitting procedures for Voigt fits is >1000 times slower than for Gaussian or Lorentzian profile. How can Voigt profile fits be speed up?

Here is what I do. First we define the Voigt and Gaussian profile. As Mathematica often complains about [Delta] and [Sigma] being <0, I use Abs insted of constraining the model as this proved faster. In addition I use the compile command to make the function even faster.

voigtprofile = Compile[{{\[Delta], _Real, 0}, {\[Sigma], _Real, 0}, {A, _Real,0}, {\[Nu]0,_Real, 0}, {\[Nu], _Real, 0}}, A PDF[VoigtDistribution[Abs@\[Delta], Abs@\[Sigma]], \[Nu] - \[Nu]0]];
gaussian[\[Sigma]_, A_, \[Nu]0_, \[Nu]_] := Return[A PDF[NormalDistribution[\[Nu]0, \[Sigma]], \[Nu]]];

Then I create noisy example data sets with a bit of noise:

noisyDataV = {#, 
 voigtprofile[0.15, 0.1, 1, 0, #] + RandomReal[{-0.1, 0.1}]} & /@ Range[-2, 2, 0.05];
noisyDataG = {#, gaussian[0.1, 1, 0, #] + RandomReal[{-0.1, 0.1}]} & /@
Range[-2, 2, 0.05];

And we use NonlinearModelFit to fit the data with excellent start parameters:

tv = AbsoluteTiming[vfit = NonlinearModelFit[noisyDataV, 
 voigtprofile[\[Delta], \[Sigma], 
  A, \[Nu]0, \[Nu]], {{\[Delta], 0.15}, {\[Sigma], 0.1}, {A, 
   1}, {\[Nu]0, 0}}, \[Nu]];]
tg = AbsoluteTiming[gfit = NonlinearModelFit[noisyDataG, 
 gaussian[\[Sigma], 
  A, \[Nu]0, \[Nu]], {{\[Sigma], 0.1}, {A, 1}, {\[Nu]0, 
   0}}, \[Nu]];]

And if we compare the required time for fit:

tv[[1]]/tg[[1]]

I get values between 1000 and 6000, which is terrible. In addition, selecting a fit Method e.g. NMinimize or other does at best yield the same result.

As this minimal example is just a very simple example, times scale up to unbearable long times for more realistic scenarios with real data. I'm glad for any hint on how to speed this simple example up.