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 [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.
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]

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.

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

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 = librosa.stft(y)
>>> times = librosa.frames_to_time(np.arange(D.shape[1]))
>>> 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

>>> onset_env = librosa.onset.onset_strength(y=y, sr=sr,
...                                          feature=librosa.cqt)
>>> 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)

../_images/librosa-onset-onset_strength-1.png