librosa.segment.agglomerative

librosa.segment.agglomerative(data, k, clusterer=None, axis=-1)[source]

Bottom-up temporal segmentation.

Use a temporally-constrained agglomerative clustering routine to partition data into k contiguous segments.

Parameters:

data : np.ndarray

data to cluster

k : int > 0 [scalar]

number of segments to produce

clusterer : sklearn.cluster.AgglomerativeClustering, optional

An optional AgglomerativeClustering object. If None, a constrained Ward object is instantiated.

axis : int

axis along which to cluster. By default, the last axis (-1) is chosen.

Returns:

boundaries : np.ndarray [shape=(k,)]

left-boundaries (frame numbers) of detected segments. This will always include 0 as the first left-boundary.

See also

sklearn.cluster.AgglomerativeClustering

Examples

Cluster by chroma similarity, break into 20 segments

>>> y, sr = librosa.load(librosa.util.example_audio_file(), duration=15)
>>> chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
>>> boundary_frames = librosa.segment.agglomerative(chroma, 20)
>>> librosa.frames_to_time(boundary_frames, sr=sr)
array([  0.   ,   1.672,   2.322,   2.624,   3.251,   3.506,
         4.18 ,   5.387,   6.014,   6.293,   6.943,   7.198,
         7.848,   9.033,   9.706,   9.961,  10.635,  10.89 ,
        11.54 ,  12.539])

Plot the segmentation against the spectrogram

>>> import matplotlib.pyplot as plt
>>> plt.figure()
>>> S = np.abs(librosa.stft(y))**2
>>> librosa.display.specshow(librosa.logamplitude(S, ref_power=np.max),
...                          y_axis='log', x_axis='time')
>>> plt.vlines(boundary_frames, 0, S.shape[0], color='r', alpha=0.9,
...            label='Segment boundaries')
>>> plt.legend(frameon=True, shadow=True)
>>> plt.title('Power spectrogram')
>>> plt.tight_layout()

(Source code)