librosa.onset.onset_strength(y=None, sr=22050, S=None, lag=1, max_size=1, ref=None, 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[f, t - lag])

where ref is S after local max filtering along the frequency axis [1].

By default, if a time series y is provided, S will be the log-power Mel spectrogram.

[1]Böck, Sebastian, and Gerhard Widmer. “Maximum filter vibrato suppression for onset detection.” 16th International Conference on Digital Audio Effects, Maynooth, Ireland. 2013.
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.

ref : None or np.ndarray [shape=(d, m)]

An optional pre-computed reference spectrum, of the same shape as S. If not provided, it will be computed from S. If provided, it will override any local max filtering governed by max_size.

detrend : bool [scalar]

Filter the onset strength to remove the DC component

center : bool [scalar]

Shift the onset function by n_fft / (2 * hop_length) frames

feature : function

Function for computing time-series features, eg, scaled spectrograms. By default, uses librosa.feature.melspectrogram with fmax=11025.0

aggregate : 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.

onset_envelope : np.ndarray [shape=(m,)]

vector containing the onset strength envelope


if neither (y, sr) nor S are provided

or if lag or max_size are not positive integers


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))
>>> times = librosa.times_like(D)
>>> plt.figure()
>>> ax1 = plt.subplot(2, 1, 1)
>>> librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
...                          y_axis='log', x_axis='time')
>>> plt.title('Power spectrogram')

Construct a standard onset function

>>> onset_env = librosa.onset.onset_strength(y=y, sr=sr)
>>> plt.subplot(2, 1, 2, sharex=ax1)
>>> plt.plot(times, 2 + onset_env / onset_env.max(), alpha=0.8,
...          label='Mean (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(times, 1 + onset_env / onset_env.max(), alpha=0.8,
...          label='Median (custom mel)')

Constant-Q spectrogram instead of Mel

>>> C = np.abs(librosa.cqt(y=y, sr=sr))
>>> onset_env = librosa.onset.onset_strength(sr=sr, S=librosa.amplitude_to_db(C, ref=np.max))
>>> plt.plot(times, onset_env / onset_env.max(), alpha=0.8,
...          label='Mean (CQT)')
>>> plt.legend(frameon=True, framealpha=0.75)
>>> plt.ylabel('Normalized strength')
>>> plt.yticks([])
>>> plt.axis('tight')
>>> plt.tight_layout()

(Source code)