pyrasa.utils.aperiodic_utils.compute_aperiodic_model#
- pyrasa.utils.aperiodic_utils.compute_aperiodic_model(aperiodic_spectrum: ndarray, freqs: ndarray, fit_func: str | type[AbstractFitFun] = 'fixed', ch_names: Iterable | None = None, scale: bool = False, fit_bounds: tuple[float, float] | None = None) AperiodicFit [source]#
Computes aperiodic parameters from the aperiodic spectrum using scipy’s curve fitting function.
This function can be used to model the aperiodic (1/f-like) component of the power spectrum. Per default, users can choose between a fixed or knee model fit or specify their own fit method see examples custom_fit_functions.ipynb for an example. The function returns the fitted parameters for each channel along with some goodness of fit metrics.
- Parameters:
aperiodic_spectrum (np.ndarray) – A 1 or 2D array of power values for the aperiodic spectrum where the shape is expected to be either (Samples,) or (Channels, Samples).
freqs (np.ndarray) – A 1D array of frequency values corresponding to the aperiodic spectrum.
fit_func (str or type[AbstractFitFun], optional) – The fitting function to use. Can be “fixed” for a linear fit or “knee” for a fit that includes a knee (bend) in the spectrum or a class that is inherited from AbstractFitFun. The default is ‘fixed’.
ch_names (Iterable or None, optional) – Channel names corresponding to the aperiodic spectrum. If None, channels will be named numerically in ascending order. Default is None.
scale (bool, optional) – Whether to scale the data to improve fitting accuracy. This is useful in cases where power values are very small (e.g., 1e-28), which may lead to numerical precision issues during fitting. After fitting, the parameters are rescaled to match the original data scale. Default is False.
fit_bounds (tuple[float, float] or None, optional) – Tuple specifying the lower and upper frequency bounds for the fit function. If None, the entire frequency range is used. Otherwise, the spectrum is cropped to the specified bounds. Default is None.
- Returns:
- An object containing three pandas DataFrames:
- aperiodic_paramspd.DataFrame
A DataFrame containing the fitted aperiodic parameters for each channel.
- gofpd.DataFrame
A DataFrame containing the goodness of fit metrics for each channel.
- modelpd.DataFrame
A DataFrame containing the predicted aperiodic model for each channel.
- Return type:
AperiodicFit
Notes
This function fits the aperiodic component of the power spectrum using scipy’s curve fitting function. The fitting can be performed using either a simple linear model (‘fixed’) or a more complex model that includes a “knee” point, where the spectrum bends. The resulting parameters can help in understanding the underlying characteristics of the aperiodic component in the data.
If the fit_bounds parameter is used, it ensures that only the specified frequency range is considered for fitting, which can be important to avoid fitting artifacts outside the region of interest.
The scale parameter can be crucial when dealing with data that have extremely small values, as it helps to mitigate issues related to machine precision during the fitting process.
The function asserts that the input data are of the correct type and shape, and raises warnings if the first frequency value is zero, as this can cause issues during model fitting.