sample.beatsdrop.regression module¶
Regression models for beats
- class sample.beatsdrop.regression.BeatRegression(fs: float = 100, lpf: float = 50, linear_regressor=None, linear_regressor_k: str = 'coef_', linear_regressor_q: str = 'intercept_', params_init: Optional[Callable[[ndarray, ndarray, ndarray, BeatRegression], Tuple[float, float, float, float, float, float, float, float]]] = None, bounds: Optional[Callable[[ndarray, ndarray, ndarray, Tuple[float, float, float, float, float, float, float, float], BeatRegression], Tuple[Tuple[float, float, float, float, float, float, float, float], Tuple[float, float, float, float, float, float, float, float]]]] = None)¶
Bases:
RegressorMixin
,BaseEstimator
Regressor for fitting to a beat pattern
- Parameters:
fs (float) – Sampling frequency for autocorrelogram computation
lpf (float) – Corner frequency for LPF of spectrum in autocorrelation computation
linear_regressor (sklearn.base.BaseEstimator) – Linear regression model instance. Must be sklearn-compatible
linear_regressor_k (str) – Attribute name for the estimated slope coefficient of the linear regression
linear_regressor_q (str) – Attribute name for the estimated intercept coefficient of the linear regression
params_init (callable) – Initializer for beat model parameters. Signature should be
f(t, a, f, res_fn, model) -> a0, a1, f0, f1, d0, d1, p0, p1
. It should return initial parameters for nonlinear least squares using input datat
,a
, andf
, the residual functionres_fn
, and theBeatRegression
instancemodel
. IfNone
, use defaultbounds (callable) – Callable for computing beat model coefficient boundaries. Signature should be
bounds(t, a, f, p, model) -> (bounds_min, bounds_max)
. It should return lower and upper boundaries for all eight parameters using input datat
,a
, andf
, initial parameter estimatesp
, and theBeatRegression
instancemodel
. IfNone
, use default
- property bounds¶
Default fget function
- fit(t: ndarray, a: ndarray, f: ndarray, tr_solver: Optional[str] = 'lsmr', method: str = 'dogbox', loss: str = 'cauchy', **kwargs)¶
Fit beat pattern
- Parameters:
t (array) – Time. Must be a column vector (shape like (-1, 1))
a (array) – Amplitude at time
t
(in dB)f (array) – Frequency at time
t
**kwargs – Keyword arguments for
scipy.optimize.least_squares()
- Returns:
self
- Return type:
- property k_: float¶
Linear regression slope
- property params_init¶
Default fget function
- predict(t: ndarray, *args) List[ndarray] ¶
Predict beat pattern
- Parameters:
t (array) – Time
args – The names of the outputs to predict. If none is specified, then the audio signal (
"x"
) is predicted. For a list of available output names, seeBeatRegression.beat_.variables
- Returns:
One array per output
- Return type:
list of arrays
- property q_: float¶
Linear regression intercept
- class sample.beatsdrop.regression.DualBeatRegression(fs: float = 100, lpf: float = 50, linear_regressor=None, linear_regressor_k: str = 'coef_', linear_regressor_q: str = 'intercept_', params_init: Optional[Callable[[ndarray, ndarray, ndarray, BeatRegression], Tuple[float, float, float, float, float, float, float, float]]] = None, bounds: Optional[Callable[[ndarray, ndarray, ndarray, Tuple[float, float, float, float, float, float, float, float], BeatRegression], Tuple[Tuple[float, float, float, float, float, float, float, float], Tuple[float, float, float, float, float, float, float, float]]]] = None, freq_loss: Optional[Callable[[ndarray, ndarray], ndarray]] = None, amp_loss: Optional[Callable[[ndarray, ndarray], ndarray]] = None, freq_w: float = 0.0002631578947368421, freq_clip: Optional[float] = 2.5, disambiguate: bool = True)¶
Bases:
BeatRegression
Regressor for fitting to a beat pattern, using information from both the amplitude modulation and the frequency modulation
- Parameters:
fs (float) – Sampling frequency for autocorrelogram computation
lpf (float) – Corner frequency for LPF of spectrum in autocorrelation computation
linear_regressor (sklearn.base.BaseEstimator) – Linear regression model instance. Must be sklearn-compatible
linear_regressor_k (str) – Attribute name for the estimated slope coefficient of the linear regression
linear_regressor_q (str) – Attribute name for the estimated intercept coefficient of the linear regression
params_init (callable) – Initializer for beat model parameters. Signature should be
f(t, a, f, res_fn, model) -> a0, a1, f0, f1, d0, d1, p0, p1
. It should return initial parameters for nonlinear least squares using input datat
,a
, andf
, the residual functionres_fn
, and theDualBeatRegression
instancemodel
. IfNone
, use defaultbounds (callable) – Callable for computing beat model coefficient boundaries. Signature should be
bounds(t, a, f, p, model) -> (bounds_min, bounds_max)
. It should return lower and upper boundaries for all eight parameters using input datat
,a
, andf
, initial parameter estimatesp
, and theDualBeatRegression
instancemodel
. IfNone
, use defaultfreq_loss (callable) – Loss function for frequencies. Signature should be
freq_loss(f_true, f_est) -> f_loss
. IfNone
, use defaultamp_loss (callable) – Loss function for amplitudes. Signature should be
amp_loss(a_true, a_est) -> a_loss
. IfNone
, use defaultfreq_w (float) – Weighting coefficient for the frequency loss function
freq_clip (float) – Maximum estimatable frequency deviation (as a multiple of the iterquartile range)
disambiguate (bool) – If
True
(default), then disambiguate association between frequencies and amplitudes
- property amp_loss¶
Default fget function
- fit(t: ndarray, a: ndarray, f: ndarray, **kwargs)¶
Fit beat pattern
- Parameters:
t (array) – Time. Must be a column vector (shape like (-1, 1))
a (array) – Amplitude at time
t
(in dB)f (array) – Frequency at time
t
**kwargs – Keyword arguments for
scipy.optimize.least_squares()
- Returns:
self
- Return type:
- property freq_loss¶
Default fget function
- sample.beatsdrop.regression.sort_params(p: ~typing.Tuple[float, float, float, float, float, float, float, float], key: ~typing.Callable[[~typing.Tuple[float, float, float, float, float, float, float, float]], ~typing.Tuple[float, float]] = <function _param_energies>, descending: bool = False) Tuple[float, float, float, float, float, float, float, float] ¶
Sort beat model parameters
- Parameters:
p (tuple) – Beat model parameters
key (callable) – Function with respect to which to sort. Defaults to modal energy
descending (bool) – If
True
, then sort in descending order
- Returns:
Sorted beat model parameters (maintaining association)
- Return type:
tuple