Logarithms and Decibels

This page explains logarithms and the decibel, which is used to express ratios of volume.

<Note>
This page uses MathJax to display formulas. If your browser settings block script execution, the formulas on this page will not display correctly.

Exponents and logarithms

Multiplying a number \(\ a\ \) by itself \(\ n\ \) times is written \(\ a^n\ \) and called \(\ a\ \) to the power of \(\ n\ \). The logarithm is the inverse of this operation.

Definition (logarithm)
When \(\ a>0,\ a\neq1,\ x>0\ \), the \(\ y\ \) satisfying \(\ a^y=x\ \) is called the logarithm of \(\ x\ \) to the base \(\ a\ \), and is written \(\ y=\log_a x\ \).

In other words, \(\ \log_a x\ \) expresses “to what power must \(\ a\ \) be raised to give \(\ x\ \)”. For example, \(\ 2^8=256\ \) so \(\ \log_2 256=8\ \), and \(\ 10^3=1000\ \) so \(\ \log_{10}1000=3\ \).

Logarithms have the following properties.

Theorem (properties of logarithms)

  1. \(\ \log_a xy = \log_a x + \log_a y\ \)
  2. \(\ \log_a \dfrac{x}{y} = \log_a x - \log_a y\ \)
  3. \(\ \log_a x^n = n\log_a x\ \)
  4. \(\ \log_a x = \dfrac{\log_b x}{\log_b a}\ \) (change of base)

The most important is the first property: taking the logarithm turns multiplication into addition. Calculations that multiply ratios together many times (so many times, then so many times, …) become simple additions once you take the logarithm.

Base-2 logarithms and bit counts

What comes up often in hacking is the base-2 logarithm. Since \(\ n\ \) bits can represent \(\ 2^n\ \) different values, the number of bits needed to represent \(\ N\ \) different values is the smallest integer greater than or equal to \(\ \log_2 N\ \).

\[ \text{Required number of bits} = \lceil \log_2 N \rceil \]

For example, to represent 100 different values, \(\ \log_2 100 \fallingdotseq 6.64\ \), so 7 bits are needed; for 256 values, exactly 8 bits are needed.

Decibels

For quantities like volume that range from very small to very large values, it is convenient to express them as the logarithm of a ratio. The human ear is also said to perceive ratios of loudness roughly logarithmically.

Definition (decibel)
The ratio of two amplitudes \(\ A,\ A_0\ \) expressed as the following value is called the decibel (dB).

\[ L = 20\log_{10}\frac{A}{A_0}\ \mathrm{[dB]} \]

The amplitude here means the value of the sound waveform (sample values or volume multipliers). Since the intensity of a sound (power, energy) is proportional to the square of the amplitude, \(\ 10\log_{10}\ \) is used when expressing a ratio of powers. Whichever you calculate with, the same ratio of sounds gives the same dB value.

Since dB is a unit that expresses a ratio, what you are comparing against matters. In these documents, unless otherwise noted, it is used to mean “compared to the original volume”.

Useful values to remember

Amplitude ratiodBNotes
2xapprox. +6dB\(\ 20\log_{10}2 \fallingdotseq 6.02\ \)
1x0dBNo change
1/2xapprox. -6dB
1/4xapprox. -12dB
10x+20dB
1/10x-20dB
1/1000x-60dBRule of thumb for when reverberation is considered to have “died out”

From the properties of logarithms, multiplying ratios becomes adding dB values. For example, applying 1/2x (-6dB) twice gives 1/4x (-12dB). If you remember that the level drops by 6dB each time the volume is halved, you can do most calculations in your head.

Example
Changing the DSP volume from $7F to $40 gives \(\ 20\log_{10}(64/127) \fallingdotseq -5.95\ \), i.e. about 6dB quieter.
A volume of $09 gives \(\ 20\log_{10}(9/128) \fallingdotseq -23\ \), i.e. about 23dB quieter than 1x.

Ranges representable with 8 and 16 bits

The ratio between the maximum value representable with an \(\ n\ \)-bit value and its smallest unit (1) is about \(\ 2^n\ \), so the range of volumes it can represent (dynamic range) is about \(\ 6n\ \)dB.

Number of bitsDynamic range
4 bits (one BRR sample)approx. 24dB
8 bitsapprox. 48dB
16 bits (DSP output)approx. 96dB

BRR stores each sample in 4 bits, but by changing the shift (granularity) for each block, it represents a range close to 16 bits overall.

Attenuation through repetition

If the volume is multiplied by \(\ r\ \) each time a signal passes through some process once, then after passing through it \(\ n\ \) times the volume is multiplied by \(\ r^n\ \). In dB, this is \(\ n\ \) times the per-pass change \(\ 20\log_{10}r\ \).

Example (echo reverberation time)
If the echo gets 2.5dB quieter with each repetition, it drops to -60dB at the \(\ 60/2.5=24\ \)th repetition.
If the delay is 96 milliseconds, the reverberation dies out after \(\ 24\times96 \fallingdotseq 2300\ \) milliseconds, i.e. about 2.3 seconds.

For processes where the same ratio is applied many times, such as echo repetitions or an envelope that gets quieter by a fixed proportion each time, thinking in dB makes things much clearer.

Exponential decay and linear decay

There are broadly two ways volume can decrease.

  • Linear decay: The volume decreases by a fixed amount at fixed time intervals. Viewed in dB, it drops more steeply as it approaches the end.
  • Exponential decay: The volume decreases by a fixed proportion (ratio) at fixed time intervals. Viewed in dB, it drops at a constant rate.

Many natural sounds (the ringing of strings or bells, room reverberation, etc.) decay exponentially. In the DSP envelope, the decay and sustain lower the volume exponentially, and the release lowers it linearly.