True RMS Envelope Follower

Overview

True RMS: square root of the moving mean of the squared signalThe True RMS Envelope Follower computes the root-mean-square level of the signal over a moving window: the square root of the moving average of the squared samples.

The signal path is as follows:

  1. Square the signal.

  2. Apply the moving average filter: the arithmetic mean of the squared samples over the window.

  3. Return the square root of the result.

The radical covers the mean of the squared samples. In the Fast RMS Envelope Follower, it covers the held peak of the squared signal (a max).

The true RMS envelope follower is a measuring follower. Its reading is independent of the waveform’s crest factor: a sine of amplitude A reads A/√2, a square wave reads A, and any two signals of equal power read the same. The fast followers, the Fast RMS Envelope Follower included, read the peak level. Use the true RMS envelope follower wherever envelopes are compared or ratioed as levels: level matching, makeup gain, energy gates, and metering. Use the fast followers where a detector must catch transients, such as dynamics side-chains: a moving average trails by half its window.

For periodic signals, a window spanning a whole number of periods nulls the ripple of the squared signal exactly: the mean over complete cycles is constant. Several periods of the lowest frequency of interest is a good default window.

The memory cost is one float per window sample (the Moving Average buffer).

The true_rms_envelope_follower_db variant works in the dB domain: the square root becomes a division by 2 (the same optimization as the fast_rms_envelope_follower_db). Use it when the consumer already works in dB (compressor, expander, and agc).

The true_rms_envelope_follower_ref variant reads a history the caller owns, with no buffer of its own.

All three are the double instantiations of class templates on T, the type of the moving average’s accumulator. float suits a single-precision FPU; a float accumulator re-sums once per window to bound its drift (see Moving Average).

Response

The plot in Figure 1 below shows the true RMS envelope follower’s response (envelope: green, signal: blue) over the same Low E pluck as the figure on the Fast RMS Envelope Follower page, with a window of 4 periods. The fast follower’s envelope follows the peaks of the signal. The true RMS reading follows its power level, below the peaks by the crest factor, and stays smooth through the beating partials. At the attack, the reading rises gradually, since a moving average trails by half its window.

True RMS envelope follower response
Figure 1. True RMS envelope follower response

True RMS Envelope Follower

Include

#include <q/fx/envelope.hpp>

Declaration

template <typename T>
struct basic_true_rms_envelope_follower
{
    static constexpr auto threshold = lin_float(-240_dB);

                        basic_true_rms_envelope_follower(duration window, float sps);
                        basic_true_rms_envelope_follower(std::size_t window_samples);

    float               operator()(float s);
    float               operator()() const;
    float               mean_square() const;
};

template <typename T>
struct basic_true_rms_envelope_follower_db
 : basic_true_rms_envelope_follower<T>
{
    using base_type = basic_true_rms_envelope_follower<T>;
    using base_type::base_type;

    decibel             operator()(float s);
    decibel             operator()() const;
};

Template Instantiations

true_rms_envelope_follower

A true RMS envelope follower in double.

true_rms_envelope_follower_db

Its dB domain variant, returning decibel.

using true_rms_envelope_follower = basic_true_rms_envelope_follower<double>;
using true_rms_envelope_follower_db =
   basic_true_rms_envelope_follower_db<double>;

Expressions

Notation

env_t

A basic_true_rms_envelope_follower<T> or basic_true_rms_envelope_follower_db<T>.

env, a, b

Objects of type env_t.

window

Object of type duration.

n

Object of type std::size_t: the window in samples.

sps

Floating point value for samples per second.

s

Floating point value for the latest input sample.

Constructors and Assignment

Expression Semantics

env_t(window, sps)

Construct an env_t given window (averaging window) and sps (samples per second).

env_t(n)

Construct an env_t with an averaging window of exactly n samples.

env_t(env)

Copy construct from env.

a = b

Assign b to a.

C++ brace initialization may also be used.

Function Call

Expression Semantics Return Type

env(s)

Process the input sample s and return the RMS level.

float (decibel for the _db variant)

env()

Get the latest RMS level without processing a sample.

float (decibel for the _db variant)

env.mean_square()

Get the raw mean of the squared samples (no square root).

float

Readings whose mean square falls below threshold (-240 dB, an RMS level of -120 dB, the same floor as the Fast RMS Envelope Follower) return 0. The _db variant returns -120 dB.
Consumers that ratio two RMS levels, or compare against a squared threshold, can stay in the mean-square domain via mean_square() and skip the square root entirely: the ratio of two RMS levels is the square root of the ratio of their mean squares.

Access

Expression Semantics Return Type

env_t::threshold

Get the floor of the mean square: -240 dB as a linear value.

float

Example

Measure the level of the input samples in, at sps samples per second, over a window of four periods of 110 Hz:

namespace q = cycfi::q;
using namespace q::literals;

auto env = q::true_rms_envelope_follower{4 * (110_Hz).period(), sps};  (1)

float level = 0;
for (auto s : in)
   level = env(s);                                       (2)
1 Construct a true RMS envelope follower with a window of four periods of 110 Hz, which nulls the ripple of a 110 Hz signal.
2 Process the sample (the follower squares it) and return the RMS level.

True RMS Envelope Follower Ref

true_rms_envelope_follower_ref is the true RMS envelope follower over a history the caller owns: the same reading as true_rms_envelope_follower, with no buffer of its own. The history holds the raw signal, and the follower squares the entering and departing samples itself. One history of the signal serves any number of RMS followers and plain moving averages of it, each with its own window.

Include

#include <q/fx/envelope.hpp>

Declaration

template <typename T>
struct basic_true_rms_envelope_follower_ref
{
    static constexpr auto threshold = lin_float(-240_dB);

             basic_true_rms_envelope_follower_ref(duration window, float sps);
             basic_true_rms_envelope_follower_ref(std::size_t window_samples);

    template <concepts::IndexableContainer History>
    float    operator()(float s, History const& hist);
    float    operator()() const;
    float    mean_square() const;
};

Template Instantiations

true_rms_envelope_follower_ref

A true RMS envelope follower ref in double.

using true_rms_envelope_follower_ref =
   basic_true_rms_envelope_follower_ref<double>;

Expressions

Notation

R

A basic_true_rms_envelope_follower_ref<T> type.

env, a, b

Objects of type R.

window

Object of type duration.

n

Object of type std::size_t: the window in samples.

sps

Floating point value for samples per second.

s

Floating point value for the latest input sample.

hist

Object of a type that conforms to IndexableContainer: the history of the raw signal, hist[0] the most recent sample.

Constructors and Assignment

Expression Semantics

R(window, sps)

Construct an R given window (averaging window) and sps (samples per second).

R(n)

Construct an R with an averaging window of exactly n samples.

R(env)

Copy construct from env.

a = b

Assign b to a.

C++ brace initialization may also be used.

Function Call

Expression Semantics Return Type

env(s, hist)

Process the input sample s, reading the departing sample from hist, and return the RMS level.

float

env()

Get the latest RMS level without processing a sample.

float

env.mean_square()

Get the raw mean of the squared samples (no square root).

float

The history holds at least the window’s length of the raw signal, hist[0] the most recent sample. The caller owns it, sizes it to the longest window that reads it, and pushes it exactly once per sample, after every view has read it. See Moving Sum Ref.
Readings whose mean square falls below threshold (-120 dB, an RMS level of -60 dB) return 0, as in true_rms_envelope_follower.

Access

Expression Semantics Return Type

R::threshold

Get the floor of the mean square: -120 dB as a linear value.

float

Example

Measure the RMS level and the mean absolute value of the input samples in over 1024 samples, from one history of the raw signal:

namespace q = cycfi::q;

auto hist = q::ring_buffer<float>{1024};
hist.clear();

auto rms = q::true_rms_envelope_follower_ref{1024};          (1)
auto mean_abs = q::moving_average_ref{1024};

for (auto s : in)
{
   auto departing = hist[1023];
   auto level = rms(s, hist);                                 (2)
   auto ave = mean_abs(std::abs(s), std::abs(departing));
   hist.push(s);                                              (3)
   // Use level and ave
}
1 Construct a true RMS envelope follower with a window of 1024 samples over the history.
2 Process the sample (the follower squares it and the departing sample) and return the RMS level.
3 Push the raw sample once, after both views have read the history.