librosa.onset.onset_backtrack

librosa.onset.onset_backtrack(events, energy)[source]

Backtrack detected onset events to the nearest preceding local minimum of an energy function.

This function can be used to roll back the timing of detected onsets from a detected peak amplitude to the preceding minimum.

This is most useful when using onsets to determine slice points for segmentation.

[1]Jehan, Tristan. “Creating music by listening” Doctoral dissertation Massachusetts Institute of Technology, 2005.
Parameters:
events : np.ndarray, dtype=int

List of onset event frame indices, as computed by onset_detect

energy : np.ndarray, shape=(m,)

An energy function

Returns:
events_backtracked : np.ndarray, shape=events.shape

The input events matched to nearest preceding minima of energy.

Examples

>>> y, sr = librosa.load(librosa.util.example_audio_file(),
...                      offset=30, duration=2.0)
>>> oenv = librosa.onset.onset_strength(y=y, sr=sr)
>>> # Detect events without backtracking
>>> onset_raw = librosa.onset.onset_detect(onset_envelope=oenv,
...                                        backtrack=False)
>>> # Backtrack the events using the onset envelope
>>> onset_bt = librosa.onset.onset_backtrack(onset_raw, oenv)
>>> # Backtrack the events using the RMS energy
>>> rmse = librosa.feature.rmse(S=np.abs(librosa.stft(y=y)))
>>> onset_bt_rmse = librosa.onset.onset_backtrack(onset_raw, rmse[0])
>>> # Plot the results
>>> import matplotlib.pyplot as plt
>>> plt.figure()
>>> plt.subplot(2,1,1)
>>> plt.plot(oenv, label='Onset strength')
>>> plt.vlines(onset_raw, 0, oenv.max(), label='Raw onsets')
>>> plt.vlines(onset_bt, 0, oenv.max(), label='Backtracked', color='r')
>>> plt.legend(frameon=True, framealpha=0.75)
>>> plt.subplot(2,1,2)
>>> plt.plot(rmse[0], label='RMSE')
>>> plt.vlines(onset_bt_rmse, 0, rmse.max(), label='Backtracked (RMSE)', color='r')
>>> plt.legend(frameon=True, framealpha=0.75)

(Source code)

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