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_peaks — Function
fit_peaks(x, y; kwargs...) -> MultiPeakFitResult
fit_peaks(spec::AbstractSpectroscopyData, region; kwargs...) -> MultiPeakFitResult
fit_peaks(spec::AbstractSpectroscopyData; kwargs...) -> MultiPeakFitResultFit 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.
OpticalSpectroscopy.initial_peak_guesses — Function
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 asn_peaksblocks of per-peak parameters followed bybaseline_order + 1polynomial baseline coefficients (constant first).n_peaks::Int: the resolved peak count (after auto-detection when then_peakskeyword isnothing).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.
Result Types
OpticalSpectroscopy.PeakFitResult — Type
PeakFitResultResult of peak fitting with any lineshape model.
Access parameters by name: result[:center].value, result[:fwhm].err
OpticalSpectroscopy.MultiPeakFitResult — Type
MultiPeakFitResultResult of multi-peak fitting (1 to N peaks with polynomial baseline).
Supports indexing by peak number: result[1] returns first peak's PeakFitResult.
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 regionpredict(result, x)— Composite fit curve on a custom x-axisresiduals(result)— Data minus fit (y_data - y_fit)
OpticalSpectroscopy.predict_peak — Function
predict_peak(result::MultiPeakFitResult, i::Int) -> Vector
predict_peak(result::MultiPeakFitResult, i::Int, x::AbstractVector) -> VectorEvaluate 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).
OpticalSpectroscopy.predict_baseline — Function
predict_baseline(result::MultiPeakFitResult) -> Vector
predict_baseline(result::MultiPeakFitResult, x::AbstractVector) -> VectorEvaluate 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.
Reporting
OpticalSpectroscopy.report — Function
report(result)Print a formatted summary of a fit result to the terminal.