librosa.feature.rms(y=None, S=None, frame_length=2048, hop_length=512, center=True, pad_mode='reflect')[source]

Compute root-mean-square (RMS) value for each frame, either from the audio samples y or from a spectrogram S.

Computing the RMS value from audio samples is faster as it doesn’t require a STFT calculation. However, using a spectrogram will give a more accurate representation of energy over time because its frames can be windowed, thus prefer using S if it’s already available.

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

(optional) audio time series. Required if S is not input.

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

(optional) spectrogram magnitude. Required if y is not input.

frame_length : int > 0 [scalar]

length of analysis frame (in samples) for energy calculation

hop_length : int > 0 [scalar]

hop length for STFT. See librosa.core.stft for details.

center : bool

If True and operating on time-domain input (y), pad the signal by frame_length//2 on either side.

If operating on spectrogram input, this has no effect.

pad_mode : str

Padding mode for centered analysis. See np.pad for valid values.

rms : np.ndarray [shape=(1, t)]

RMS value for each frame


>>> y, sr = librosa.load(librosa.util.example_audio_file())
>>> librosa.feature.rms(y=y)
array([[ 0.   ,  0.056, ...,  0.   ,  0.   ]], dtype=float32)

Or from spectrogram input

>>> S, phase = librosa.magphase(librosa.stft(y))
>>> rms = librosa.feature.rms(S=S)
>>> import matplotlib.pyplot as plt
>>> plt.figure()
>>> plt.subplot(2, 1, 1)
>>> plt.semilogy(rms.T, label='RMS Energy')
>>> plt.xticks([])
>>> plt.xlim([0, rms.shape[-1]])
>>> plt.legend()
>>> plt.subplot(2, 1, 2)
>>> librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max),
...                          y_axis='log', x_axis='time')
>>> plt.title('log Power spectrogram')
>>> plt.tight_layout()

Use a STFT window of constant ones and no frame centering to get consistent results with the RMS computed from the audio samples y

>>> S = librosa.magphase(librosa.stft(y, window=np.ones, center=False))[0]
>>> librosa.feature.rms(S=S)

(Source code)