Peak Fitting

Unified multi-peak fitting that works on any spectrum type. Uses CurveFit.jl for the solver and CurveFitModels.jl for peak model functions.

Fitting

OpticalSpectroscopy.fit_peaksFunction
fit_peaks(x, y; kwargs...) -> MultiPeakFitResult
fit_peaks(spec::AbstractSpectroscopyData, region; kwargs...) -> MultiPeakFitResult
fit_peaks(spec::AbstractSpectroscopyData; kwargs...) -> MultiPeakFitResult

Fit one or more peaks in spectroscopy data.

Automatic initial guesses come from initial_peak_guesses; pass p0 (a full parameter vector in the same layout) to override them.

source
OpticalSpectroscopy.initial_peak_guessesFunction
initial_peak_guesses(x, y; model=lorentzian, n_peaks=nothing, peaks=nothing,
                     baseline_order=1, min_prominence=0.05)
    -> (p0, n_peaks, peak_params, baseline_order)

Compute the automatic initial parameter vector that fit_peaks would use for the same keywords, without running the fit.

Returns a named tuple:

  • p0::Vector{Float64}: the full initial parameter vector, laid out as n_peaks blocks of per-peak parameters followed by baseline_order + 1 polynomial baseline coefficients (constant first).
  • n_peaks::Int: the resolved peak count (after auto-detection when the n_peaks keyword is nothing).
  • peak_params::Vector{Symbol}: the per-peak parameter names, in the order they appear within each block.

The result can be edited and passed back to fit_peaks via its p0 keyword, which is how a GUI or script lets a user adjust individual initial guesses while keeping automatic values for the rest.

model must be one of the built-in lineshapes (lorentzian, gaussian, pseudo_voigt, voigt, fano); a custom function throws an ArgumentError, because the parameter layout automatic guesses depend on cannot be inferred from it. fit_peaks does accept a custom model when given an explicit p0.

source

Result Types

Prediction and Decomposition

The predict and residuals functions are re-exported from CurveFit.jl and extended for MultiPeakFitResult:

  • predict(result) — Composite fit curve (all peaks + baseline) evaluated on the fit region
  • predict(result, x) — Composite fit curve on a custom x-axis
  • residuals(result) — Data minus fit (y_data - y_fit)
OpticalSpectroscopy.predict_peakFunction
predict_peak(result::MultiPeakFitResult, i::Int) -> Vector
predict_peak(result::MultiPeakFitResult, i::Int, x::AbstractVector) -> Vector

Evaluate the i-th peak of a multi-peak fit, excluding the baseline. Without x, the peak is evaluated on the original fit region. Pass a custom x to evaluate elsewhere (e.g., for plotting on a wider range).

source
OpticalSpectroscopy.predict_baselineFunction
predict_baseline(result::MultiPeakFitResult) -> Vector
predict_baseline(result::MultiPeakFitResult, x::AbstractVector) -> Vector

Evaluate the polynomial baseline component of a multi-peak fit. Without x, the baseline is evaluated on the original fit region. Pass a custom x to evaluate on a different range.

source

Reporting