librosa.onset.onset_strength¶
-
librosa.onset.
onset_strength
(y=None, sr=22050, S=None, lag=1, max_size=1, detrend=False, center=True, feature=None, aggregate=None, centering=None, **kwargs)[source]¶ Compute a spectral flux onset strength envelope.
Onset strength at time t is determined by:
mean_f max(0, S[f, t] - ref_S[f, t - lag])
where ref_S is S after local max filtering along the frequency axis [R21].
By default, if a time series y is provided, S will be the log-power Mel spectrogram.
[R21] Böck, Sebastian, and Gerhard Widmer. “Maximum filter vibrato suppression for onset detection.” 16th International Conference on Digital Audio Effects, Maynooth, Ireland. 2013. Parameters: y : np.ndarray [shape=(n,)]
audio time-series
sr : number > 0 [scalar]
sampling rate of y
S : np.ndarray [shape=(d, m)]
pre-computed (log-power) spectrogram
lag : int > 0
time lag for computing differences
max_size : int > 0
size (in frequency bins) of the local max filter. set to 1 to disable filtering.
detrend : bool [scalar]
Filter the onset strength to remove the DC component
center : bool [scalar]
centering : bool [scalar] (deprecated)
Shift the onset function by n_fft / (2 * hop_length) frames
Note
The centering parameter is deprecated as of 0.4.1,
and has been replaced by the center parameter.
feature : function
Function for computing time-series features, eg, scaled spectrograms. By default, uses
librosa.feature.melspectrogram
with fmax=11025.0aggregate : function
Aggregation function to use when combining onsets at different frequency bins.
Default:
np.mean
kwargs : additional keyword arguments
Additional parameters to feature(), if S is not provided.
Returns: onset_envelope : np.ndarray [shape=(m,)]
vector containing the onset strength envelope
Raises: ParameterError
if neither (y, sr) nor S are provided
or if lag or max_size are not positive integers
See also
Examples
First, load some audio and plot the spectrogram
>>> import matplotlib.pyplot as plt >>> y, sr = librosa.load(librosa.util.example_audio_file(), ... duration=10.0) >>> D = np.abs(librosa.stft(y))**2 >>> plt.figure() >>> plt.subplot(2, 1, 1) >>> librosa.display.specshow(librosa.logamplitude(D, ref_power=np.max), ... y_axis='log') >>> plt.title('Power spectrogram')
Construct a standard onset function
>>> onset_env = librosa.onset.onset_strength(y=y, sr=sr) >>> plt.subplot(2, 1, 2) >>> plt.plot(2 + onset_env / onset_env.max(), alpha=0.8, ... label='Mean aggregation (mel)')
Median aggregation, and custom mel options
>>> onset_env = librosa.onset.onset_strength(y=y, sr=sr, ... aggregate=np.median, ... fmax=8000, n_mels=256) >>> plt.plot(1 + onset_env / onset_env.max(), alpha=0.8, ... label='Median aggregation (custom mel)')
Constant-Q spectrogram instead of Mel
>>> onset_env = librosa.onset.onset_strength(y=y, sr=sr, ... feature=librosa.cqt) >>> plt.plot(onset_env / onset_env.max(), alpha=0.8, ... label='Mean aggregation (CQT)')
>>> plt.legend(frameon=True, framealpha=0.75) >>> librosa.display.time_ticks(librosa.frames_to_time(np.arange(len(onset_env)))) >>> plt.ylabel('Normalized strength') >>> plt.yticks([]) >>> plt.axis('tight') >>> plt.tight_layout()