Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 66

Dynamic Range Processing: Compression and Limiting

Recommended resources

Hello! Welcome back to our course on Digital and Analog Signal Processing.

In our last lesson, we explored delay-based effects like echo and reverberation, learning how to manipulate a signal's characteristics in the time domain using FIR and IIR filter structures. Today, we shift our focus from time and frequency to the amplitude domain. We'll investigate one of the most fundamental tools in audio engineering: dynamic range processing.

This lesson is designed to help you analyze and implement dynamic range compression and limiting. We will dissect the inner workings of a compressor, understanding it as a sophisticated, automated volume control. We'll explore the key parameters that define its behavior and see how these concepts are translated into a digital signal processing algorithm.

1. The "Why" of Compression: A Historical Perspective

Before diving into the "how," let's address the "why." The dynamic range of an audio signal is the ratio between its loudest and quietest parts. Live music can have an enormous dynamic range—an orchestra can go from a near-silent passage to a deafening crescendo.

Historically, the technologies used for recording and broadcasting audio (vinyl records, magnetic tape, AM/FM radio) could not handle this full range. This "10 pounds of audio in a 5-pound bag" problem necessitated the invention of the compressor. Its original job was to automatically reduce the dynamic range of an entire program to fit the limitations of the medium.

Dynamics Processors -- Technology & Applications

For a bit more historical context, this document explains the original concept of compression. Your EE background will appreciate the 'hinge point' concept, which was the predecessor to the modern 'threshold' control.

Please read the short 'Appendix -- Compressor & Expander History Note' at the very end of the document. It provides an excellent summary of how the purpose and function of compressors have evolved.

While modern digital audio has a vast dynamic range, compression remains an essential tool, now used more for artistic control: to make a vocal sit perfectly in a mix, to add punch to a drum kit, or to ensure consistent loudness in a video.


2. The Anatomy of a Digital Compressor

A dynamic range compressor is essentially a voltage-controlled amplifier (VCA) where the control signal is derived from the audio signal itself. In the digital domain, it's an amplifier with a time-varying gain. The most common modern implementation is the feed-forward topology, which we will focus on.

The following video provides an excellent overview of the structure and parameters of a typical compressor.

(Sponsored) Audio Compressor Software Implementation (STM32 DSP) - Phil's lab #157

The video '(Sponsored) Audio Compressor Software Implementation' from Phil's Lab introduces the fundamental purpose of a compressor and its primary controls.

Please watch from 03:08 to 07:21. This section covers: The basic concept and purpose of compression. The key parameters: threshold, ratio, attack, and release. A block diagram of a feed-forward compressor, which will serve as our roadmap.

As shown in the video, the compressor can be broken down into a sequence of processing blocks. Let's analyze each one.


3. Deconstructing the Compressor: A Step-by-Step Analysis

Let's follow the signal through the side-chain to understand how the gain is computed. All calculations in the side-chain are typically performed in the decibel (logarithmic) domain, which aligns well with human perception of loudness and is familiar from your work in electrical engineering.

3.1. Block 1: The Envelope Detector

The first step is to measure the instantaneous level or "envelope" of the input signal.

(Sponsored) Audio Compressor Software Implementation (STM32 DSP) - Phil's lab #157

This segment from the same video explains the role of the envelope detector.

Watch from 08:00 to 10:06. Focus on how the detector extracts the signal's amplitude and the choice of detection method (e.g., RMS).

While peak detection is an option, Root Mean Square (RMS) detection is very common as it gives a result more closely related to perceived loudness and signal power. After calculating the envelope level , it's converted to decibels:

The small value is added to prevent taking the logarithm of zero.

3.2. Block 2: The Gain Computer (Static Non-linearity)

This is the heart of the compressor. It's a static, non-linear function that determines the target gain reduction based on the input level relative to the user-set parameters.

(Sponsored) Audio Compressor Software Implementation (STM32 DSP) - Phil's lab #157

This part of the video explains the gain computer and visualizes how the parameters shape its transfer characteristic.

Watch from 10:06 to 15:00. Pay close attention to the input/output graph and how threshold, ratio, and knee are defined.

The key parameters are:

  • Threshold (): The level (in dB) above which compression starts.
  • Ratio (): Determines the amount of gain reduction. A 4:1 ratio means that for every 4 dB the input goes above the threshold, the output will only rise by 1 dB.
  • Knee (): Controls the transition from uncompressed to compressed. A "hard knee" is an abrupt change, while a "soft knee" provides a smoother, more gradual transition.

For a simple hard knee compressor, the static gain reduction in dB is calculated as follows:

This formula computes how much the signal should be turned down. For example, if the input is 4 dB over the threshold with a 4:1 ratio, the output should be 1 dB over the threshold, which means a gain reduction of -3 dB.

3.3. Block 3: Attack and Release (Gain Smoothing)

The static gain calculated above can change very rapidly. Applying it directly would cause audible distortion. The attack and release controls are used to smooth this gain signal, determining how quickly the compressor reacts.

(Sponsored) Audio Compressor Software Implementation (STM32 DSP) - Phil's lab #157

This crucial segment explains how attack and release times are implemented using a concept you are very familiar with: a first-order IIR low-pass filter.

Watch from 15:00 to 18:35. Note the logic: a different time constant (filter coefficient) is used depending on whether the compressor is engaging (attack) or disengaging (release).

  • Attack Time: How quickly the gain is reduced when the signal crosses the threshold.
  • Release Time: How quickly the gain is restored when the signal drops back below the threshold.

This is implemented as a conditional first-order IIR filter. Let be the smoothed gain at sample .
If (compressor engaging), we use the attack coefficient :

If (compressor releasing), we use the release coefficient :

The filter coefficients are derived from the desired time (in seconds) and the sample rate , often using an approximation based on the exponential response of an RC circuit:

3.4. Final Steps: Makeup Gain and Application

Compression reduces the level of the loudest parts, which can lower the overall perceived volume. Makeup gain is a static gain added after the smoothing stage to compensate for this.

(Sponsored) Audio Compressor Software Implementation (STM32 DSP) - Phil's lab #157

This short clip explains the purpose of makeup gain and the final conversion back to a linear gain value for multiplication.

Watch from 18:35 to 19:59.

  1. Add Makeup Gain: The total gain in dB is .
  2. Convert to Linear: The final gain must be converted from dB back to a linear scale: .
  3. Apply Gain: The final output sample is computed by multiplying the input sample by the linear gain: .
Test your understanding!

A compressor has a threshold of -20 dBFS and a ratio of 2:1. An input signal with a peak level of -14 dBFS arrives.

  1. How many dB is the input signal above the threshold?
  2. With a 2:1 ratio, what should the output level be relative to the threshold?
  3. What is the target output level in dBFS?
  4. What is the required static gain reduction in dB?
Show answer
  1. The input is dB above the threshold.
  2. With a 2:1 ratio, the 6 dB increase above the threshold is reduced to dB. The output should be 3 dB above the threshold.
  3. The target output level is dBFS.
  4. The required gain reduction is dB.

4. Implementation in Code

We've analyzed all the building blocks. Now, let's see how they are put together in practice to process an audio stream, sample by sample.

(Sponsored) Audio Compressor Software Implementation (STM32 DSP) - Phil's lab #157

The following segment walks through the C code that implements the exact block diagram we have just analyzed. This directly addresses the 'implement' part of our learning goal.

Watch the code walkthrough from 22:51 to 26:31. Follow the update function and map each step back to the blocks we discussed: envelope detection, gain computation, attack/release filtering, and final gain application.

The process for each incoming sample x[n] is:

  1. Update the envelope detector with x[n] to get a new x_dB[n].
  2. Use x_dB[n] in the gain computer to calculate the static gain g_sc[n].
  3. Use g_sc[n] to update the attack/release IIR filter, yielding the smoothed gain g_sm[n].
  4. Add makeup gain and convert the total dB gain to a linear value g_lin[n].
  5. Calculate the output y[n] = g_lin[n] * x[n].

5. Limiters: Compression in the Extreme

A limiter is not a separate type of processor, but rather a specific configuration of a compressor.

You're Using Your Limiter WRONG

This short, excellent video gives a concise and accurate definition of a limiter.

Watch from 04:21 to 04:50.

As the video states, a limiter is a compressor with:

  • A very high ratio (10:1 up to :1).
  • A very fast attack time.

Its purpose is not to shape the sound artistically, but to act as a protective "brick wall," preventing the signal from ever exceeding the threshold. This is crucial for preventing digital clipping in recorders or protecting power amplifiers and speakers from damaging peaks.

Look-Ahead Limiting: A Digital Advantage

Analog limiters have a fundamental problem: they can only react to a peak after it has begun. This means a small part of the transient will always get through before the gain is reduced (this is called "overshoot").

Digital Signal Processing offers an elegant solution: look-ahead. By introducing a small delay (e.g., a few milliseconds) in the main signal path, the side-chain can analyze the input signal in advance. It sees the peak coming before it reaches the gain multiplication stage and can start reducing the gain smoothly, resulting in a much more transparent and effective limiting action. This is a powerful capability unique to the digital domain.


Conclusion

In this lesson, we demystified dynamic range compression by breaking it down into a series of well-defined signal processing blocks. We've seen how a few key parameters can be combined to create one of audio's most powerful tools.

Key Takeaways:

  • Dynamic range compression is an automated process for reducing the difference between the loudest and quietest parts of a signal.
  • A standard digital compressor consists of an envelope detector, a non-linear gain computer, and an attack/release smoothing filter.
  • The core parameters—threshold, ratio, knee, attack, and release—are used to calculate a time-varying gain signal, which is then applied to the audio.
  • The calculations for gain are performed in the decibel domain, while the final multiplication is done in the linear domain.
  • A limiter is a special case of a compressor with a very high ratio and fast attack, used for overload protection.
  • Digital implementations enable powerful features like look-ahead, which are impossible in the analog world.

Preview of the Next Lesson:

In this lesson and the last, we've implemented effects that require memory (delay lines), introduce processing delay (look-ahead), and have computational costs. Our next lesson, "Describe the constraints of real-time audio processing," will formalize these ideas. We will discuss the practical challenges of running these algorithms in real-time, including managing buffer sizes, latency, and overall computational load.