librosa.feature.chroma_stft

librosa.feature.chroma_stft(y=None, sr=22050, S=None, norm=inf, n_fft=2048, hop_length=512, tuning=None, **kwargs)[source]

Compute a chromagram from a waveform or power spectrogram.

This implementation is derived from chromagram_E [R35]

[R35]Ellis, Daniel P.W. “Chroma feature analysis and synthesis” 2007/04/21 http://labrosa.ee.columbia.edu/matlab/chroma-ansyn/
Parameters:

y : np.ndarray [shape=(n,)] or None

audio time series

sr : number > 0 [scalar]

sampling rate of y

S : np.ndarray [shape=(d, t)] or None

power spectrogram

norm : float or None

Column-wise normalization. See librosa.util.normalize for details.

If None, no normalization is performed.

n_fft : int > 0 [scalar]

FFT window size if provided y, sr instead of S

hop_length : int > 0 [scalar]

hop length if provided y, sr instead of S

tuning : float in [-0.5, 0.5) [scalar] or None.

Deviation from A440 tuning in fractional bins (cents). If None, it is automatically estimated.

kwargs : additional keyword arguments

Arguments to parameterize chroma filters. See librosa.filters.chroma for details.

Returns:

chromagram : np.ndarray [shape=(n_chroma, t)]

Normalized energy for each chroma bin at each frame.

See also

librosa.filters.chroma
Chroma filter bank construction
librosa.util.normalize
Vector normalization

Examples

>>> y, sr = librosa.load(librosa.util.example_audio_file())
>>> librosa.feature.chroma_stft(y=y, sr=sr)
array([[ 0.974,  0.881, ...,  0.925,  1.   ],
       [ 1.   ,  0.841, ...,  0.882,  0.878],
       ...,
       [ 0.658,  0.985, ...,  0.878,  0.764],
       [ 0.969,  0.92 , ...,  0.974,  0.915]])

Use an energy (magnitude) spectrum instead of power spectrogram

>>> S = np.abs(librosa.stft(y))
>>> chroma = librosa.feature.chroma_stft(S=S, sr=sr)
>>> chroma
array([[ 0.884,  0.91 , ...,  0.861,  0.858],
       [ 0.963,  0.785, ...,  0.968,  0.896],
       ...,
       [ 0.871,  1.   , ...,  0.928,  0.829],
       [ 1.   ,  0.982, ...,  0.93 ,  0.878]])

Use a pre-computed power spectrogram with a larger frame

>>> S = np.abs(librosa.stft(y, n_fft=4096))**2
>>> chroma = librosa.feature.chroma_stft(S=S, sr=sr)
>>> chroma
array([[ 0.685,  0.477, ...,  0.961,  0.986],
       [ 0.674,  0.452, ...,  0.952,  0.926],
       ...,
       [ 0.844,  0.575, ...,  0.934,  0.869],
       [ 0.793,  0.663, ...,  0.964,  0.972]])
>>> import matplotlib.pyplot as plt
>>> plt.figure(figsize=(10, 4))
>>> librosa.display.specshow(chroma, y_axis='chroma', x_axis='time')
>>> plt.colorbar()
>>> plt.title('Chromagram')
>>> plt.tight_layout()

(Source code)

../_images/librosa-feature-chroma_stft-1.png