librosax.feature.spectral_centroid¶
- spectral_centroid(*, y: Array | None = None, sr: float = 22050, S: Array | None = None, n_fft: int = 2048, hop_length: int = 512, freq: Array | None = None, win_length: int | None = None, window: str = 'hann', center: bool = True, pad_mode: str = 'constant') Array[source]¶
Compute the spectral centroid.
Each frame of a magnitude spectrogram is normalized and treated as a distribution over frequency bins, from which the mean (centroid) is extracted per frame.
- More precisely, the centroid at frame t is defined as:
centroid[t] = sum_k S[k, t] * freq[k] / (sum_j S[j, t])
where S is a magnitude spectrogram, and freq is the array of frequencies (e.g., FFT frequencies in Hz) of the rows of S.
Users should ensure S is real-valued and non-negative.
- Parameters:
y –
Audio time series. The last axis must be time.
(T,)- single waveform(B, T)- batch of waveforms
sr – Audio sampling rate
S – (optional) Pre-computed spectrogram magnitude with shape
(..., F, N)n_fft – FFT window size
hop_length – Hop length for STFT
freq – Center frequencies for spectrogram bins. If None, FFT bin center frequencies are used.
win_length – Window length
window – Window function
center – If True, pad the signal
pad_mode – Padding mode
- Returns:
Spectral centroid frequencies with shape
(..., 1, N)where N is the number of frames. The frequency axis is reduced to size 1.(T,)→(1, N)(B, T)→(B, 1, N)