RMS Energy vs Amplitude Envelope: An In-Depth Comparison Using Librosa
When analyzing audio signals, two commonly used measures of loudness are the RMS energy and the amplitude envelope. While related, these metrics can paint quite different pictures of an audio waveform. In this article, we‘ll take a deep dive into RMS energy and amplitude envelope – what they represent, how they‘re calculated, and when you might prefer one over the other. We‘ll visualize these features for various music genres and provide code examples using the popular Librosa library for audio analysis in Python. By the end, you‘ll have a solid understanding of these fundamental audio descriptors and be equipped to extract them from your own audio data.
RMS Energy
RMS energy, which stands for root mean square energy, is a way of measuring the average loudness or amplitude of an audio signal over time. Mathematically, it is calculated by:
- Squaring each sample value in the signal
- Taking the mean (average) of the squared values over a window of time
- Taking the square root of this mean
In equation form, the RMS energy of a discrete signal x with N samples is:
RMS = sqrt( sum(x[n]^2) / N )
Intuitively, you can think of RMS as a rectified, smoothed version of the original waveform that gives a sense of the signal‘s average amplitude. By squaring the samples, it weights larger amplitudes more heavily.
The Librosa library makes it very simple to calculate RMS energy using the librosa.feature.rms function. You simply pass in an audio time series and specify the frame length and hop length (to determine the window size and overlap):
import librosaaudio, sr = librosa.load("my_audio.wav") rms = librosa.feature.rms(audio, frame_length=2048, hop_length=512)
This returns a 1D array of RMS values, where each value corresponds to a frame of the signal. The frame_length determines the window size in samples, while hop_length determines the number of samples between successive frames. Smaller hop lengths will result in a finer resolution RMS curve, at the expense of more computation.
Amplitude Envelope
The amplitude envelope of an audio signal refers to the outline or "envelope" that traces the peak amplitudes of the waveform over time. You can think of it like a smooth curve that hugs the top (and bottom) of the oscillating signal.
To calculate the amplitude envelope, a common approach is:
- Rectify the signal (take the absolute value of each sample)
- Low-pass filter the rectified signal to smooth it out
The Librosa library doesn‘t have a dedicated function for amplitude envelope, but we can easily recreate the above steps:
import librosa import numpy as npaudio, sr = librosa.load("my_audio.wav") rectified = np.abs(audio) envelope = librosa.feature.rms(rectified, frame_length=2048, hop_length=512)
Here we rectify the signal with np.abs, then smooth it using the rms function with appropriate frame and hop lengths to serve as a low-pass filter. The resulting envelope will trace the peaks of the rectified waveform.
Comparing RMS Energy and Amplitude Envelope
To get a visual sense of how RMS energy and amplitude envelope differ, let‘s plot them for some example audio clips across different musical genres. We‘ll use 30 second excerpts of classical, rock, hip hop, and electronic tracks.
<< insert code to load audio files and plot waveform, RMS and envelope >>
<< show plots >>
Examining these plots, a few observations stand out:
-
The amplitude envelope (in green) tightly hugs the peaks of the waveform, while the RMS energy (in red) is a smoother, average representation of the signal‘s amplitude.
-
The envelope captures more of the fine-grained amplitude variations, while RMS provides a more high-level view of loudness over time. This is especially apparent in the classical example, where the envelope traces the note-level dynamics while RMS shows the broader phrases.
-
However, the amplitude envelope is more easily thrown off by outliers and brief high amplitude spikes. The RMS curve is more robust to these outliers. Look at the rock example – the envelope has many sharp peaks from drum hits, while the RMS curve emphasizes the average level of the guitars and vocals underneath.
-
The RMS curve sits below the peak amplitiude envelope. This represents the "crest factor", or ratio between peak and average levels. Signals with higher crest factors (e.g. classical) will have a larger gap between RMS and envelope.
So in summary, RMS energy and amplitude envelope are two ways of looking at the loudness or amplitude of a signal over time. The envelope traces the instantaneous peak levels, while RMS gives an average, smoothed view that‘s less sensitive to short-term spikes. Which representation is most useful will depend on the particularities of your audio data and application.
RMS Energy and Perceptual Loudness
While RMS energy is a useful measure of average amplitude, it doesn‘t perfectly match up with human perception of loudness. Our ears respond nonlinearly to loudness, and are more sensitive to certain frequencies than others.
For this reason, perceptually-motivated loudness measures have been developed, such as the EBU R128 loudness standard used in broadcast. These typically involve applying an equal-loudness filter to the audio (to weight different frequencies by their perceptual loudness), then taking an average amplitude measure like RMS.
Librosa provides some functions for perceptual loudness, like librosa.feature.tonnetz, which calculates tonal centroid features. There are also dedicated loudness plugins and meters that can provide calibrated perceptual loudness measurements. While the basic RMS function is a good starting point, it‘s worth exploring these perceptual loudness measures, especially for applications like automatic mixing or loudness matching.
Conclusion
In this article, we took an in-depth look at two fundamental audio features: RMS energy and amplitude envelope. We compared how these metrics represent the loudness or amplitude of an audio signal over time, with RMS providing an average level and envelope capturing finer peak variations. We discussed the tradeoffs between the two, and how RMS is more robust to outliers while envelope is more sensitive to momentary spikes.
Using the Librosa library, we calculated RMS and envelope features with just a few lines of Python code. We visually examined the RMS and envelope curves for various musical genres, and observed how they differed in tracing the dynamics and amplitude characteristics of each excerpt.
Lastly, we touched on perceptual loudness measures that attempt to more closely match human perception, and how these can be more useful than pure RMS in certain applications.
Hopefully this has given you a solid foundation in these important audio concepts and tools. Whether you‘re working on music information retrieval, audio effect plugins, or automated mixing systems, an understanding of RMS energy and amplitude envelope will serve you well. Try experimenting with these features in Librosa and see what insights you can uncover from your own audio datasets.
As always, the field of audio signal processing is deep and ever-evolving, so I encourage you to explore further. Some good next steps would be reading up on psychoacoustics, perceptual audio coding, and loudness algorithms like EBU R128. Feel free to share your own audio analysis experiments in the comments below!