CurveFitModels.jl
A Julia package providing model functions for curve fitting with CurveFit.jl. Originally developed with spectroscopy applications in mind, but useful for any nonlinear curve fitting task.
All model functions follow the CurveFit.jl convention: fn(parameters, x) where parameters come first.
Available Models
Lineshapes: gaussian, lorentzian, pseudo_voigt, power_law, logistic, gaussian2d
Temporal: single_exponential, stretched_exponential, n_exponentials, sine, damped_sine
Oscillator models: lorentz_oscillator, dielectric_real, dielectric_imag
Installation
using Pkg
Pkg.add("CurveFitModels")Example
Fit a Lorentzian to a molecular absorption peak:
using CurveFit
using CurveFitModels
# Simulated IR absorption peak near 2100 cm⁻¹ (e.g., C≡O stretch)
ν = range(2050, 2150, length=100) # wavenumber (cm⁻¹)
true_params = [5.0, 2100.0, 12.0] # [amplitude, center, FWHM]
y = lorentzian(true_params, ν) .+ 0.003 .* randn(length(ν))
# Fit with initial guess
p0 = [4.7, 2098.0, 15.0] # initial guess for [A, ν₀, Γ]
prob = NonlinearCurveFitProblem(lorentzian, p0, ν, y)
sol = solve(prob)
# Extract fitted parameters
A, ν₀, Γ = sol.u
Model Composition
Combine models with polynomial baselines for simultaneous fitting:
# poly(p, x) evaluates c₀ + c₁x + c₂x² + ...
model = combine(lorentzian, 3, poly, 2) # lorentzian + linear baseline
p0 = [A, x0, Γ, c0, c1] # peak params + baseline params
prob = NonlinearCurveFitProblem(model, p0, x, y)
sol = solve(prob)Helper Functions
# Width conversion
fwhm = sigma_to_fwhm(σ) # Gaussian σ → FWHM
σ = fwhm_to_sigma(fwhm) # FWHM → Gaussian σ
# Area calculation
area = gaussian_area(A, σ) # A × σ × √(2π)
area = lorentzian_area(A, Γ) # A × π × Γ / 2