Skip to content

Peaks and spectral fitting

findpeaks and maskpeaks wrap Peaks.jl for time series and spectra; they handle multivariate input and return peaks with heights, prominences, and widths.

MAPPLE (Adaptive Peaks and Power-Law Exponents) is a parametric spectral model combining multiple piecewise power-law components with Gaussian peaks. Fitting is in two stages: an initial peak-finding + linear-regression pass (fit(MAPPLE, S)), then a refinement with Optim (fit!(m, S)), provided by OptimExt (loaded with using Optim).

julia
using TimeseriesTools, Optim
m  = fit(MAPPLE, S)        # rough fit on a power spectrum
fit!(m, S)                 # refine
ŝ  = predict(m, freqs(S))

Reference

Missing docstring.

Missing docstring for findpeaks. Check Documenter's build log for details.

Missing docstring.

Missing docstring for maskpeaks. Check Documenter's build log for details.

TimeseriesTools.MAPPLE Type
julia
MAPPLE(params::ComponentArray)

An MAPPLE (Adaptive Peaks and Power-Law Exponents) model for fitting power spectra. params consists of:

  • log_A: Base log-10 amplitude of the spectrum.

  • components: An array of components, each with:

    • log_f_stop: Log-10 frequency where the component transitions to the next.

    • β: Power-law exponent for the component.

  • peaks: An array of Gaussian peaks, each with:

    • log_f: Log-10 center frequency of the peak.

    • log_σ: Width of the peak in log-frequency space.

    • log_A: Log-10 amplitude of the peak.

  • transition_width: Width of the transition between components in log-frequency space.

source

Missing docstring.

Missing docstring for mapple. Check Documenter's build log for details.

TimeseriesTools.fit_mapple Function
julia
fit_mapple(log_f, log_s; components, peaks = :auto, peak_threshold = 5.0, max_n_peaks = 8, peak_width_limits, w, kwargs...)

Rough MAPPLE initialisation from log-10 frequencies/spectral density: fit components broken-power-law segments by regression, then detect peaks on the detrended residual.

Peak count:

  • peaks = :auto (default) keeps every detection whose prominence clears peak_threshold robust standard deviations of the residual, up to max_n_peaks strongest. The default threshold is deliberately conservative — noise produces local-maxima prominences several times the point-noise scale, so a low threshold over-detects. Detection is best-effort; pass an explicit peaks::Integer when the count is known.

  • peaks::Integer takes the strongest that many detections (no threshold, no padding); fewer are returned, with a warning, if detection finds fewer.

peak_width_limits = (wmin, wmax) (log-frequency units) rejects implausibly narrow (sub-resolution) or wide detections before counting. w is the peak-finder smoothing window; pass an explicit minprom to override the :auto threshold.

source
julia
fit_mapple(log_f, log_s, initial_params; multistart = 0, algorithm = LBFGS(), kwargs...)

Refine MAPPLE initial_params against log-10 frequencies log_f and log-10 spectral density log_s with Optim. Each fit is refined twice — once with peaks boxed near their detected centre/width and once with peaks free over the band — and the lower-loss result is kept; the boxed refine prevents peaks running away on dense fields while the free refine wins where the box would trap the optimizer. The supplied (clamped) initialisation is also a candidate, so the result never worsens it. With multistart > 0, additionally fit from that many randomly perturbed restarts.

w weights the squared log-residuals: nothing (the default) fits unweighted, true computes logweights from log_f, and a vector supplies per-sample weights directly. Weighting makes the objective minimise ∫ r² d(log f) rather than Σ r², which matters whenever the fitted axis is not uniform in log space. Only the scale-free ratio of the weights affects the minimiser, so normalisation is irrelevant; every refine candidate is scored with the same w, leaving the multistart comparison valid.

Extra kwargs go to Optim.Options.

source
StatsAPI.fit! Method
julia
fit!(m::MAPPLE, spectrum; kwargs...)

Refine the parameters of a MAPPLE model m in place to fit the provided spectrum (linear frequency lookup, linear spectral density). kwargs are forwarded to fit_mapple; in particular w = true weights the fit by logweights of the spectrum's own axis, so a grid that is not uniform in log space cannot bias the fit toward its log-denser end.

source
TimeseriesTools.fit_mapple Method
julia
fit_mapple(log_f, log_s, initial_params; multistart = 0, algorithm = LBFGS(), kwargs...)

Refine MAPPLE initial_params against log-10 frequencies log_f and log-10 spectral density log_s with Optim. Each fit is refined twice — once with peaks boxed near their detected centre/width and once with peaks free over the band — and the lower-loss result is kept; the boxed refine prevents peaks running away on dense fields while the free refine wins where the box would trap the optimizer. The supplied (clamped) initialisation is also a candidate, so the result never worsens it. With multistart > 0, additionally fit from that many randomly perturbed restarts.

w weights the squared log-residuals: nothing (the default) fits unweighted, true computes logweights from log_f, and a vector supplies per-sample weights directly. Weighting makes the objective minimise ∫ r² d(log f) rather than Σ r², which matters whenever the fitted axis is not uniform in log space. Only the scale-free ratio of the weights affects the minimiser, so normalisation is irrelevant; every refine candidate is scored with the same w, leaving the multistart comparison valid.

Extra kwargs go to Optim.Options.

source