TL;DR
Mu-law companding is a way to store a signal with a fixed number of levels, such as 256, without wasting them on loud sounds. It first bends the signal with a logarithmic curve, so small values are spread out and large values are squeezed together, and then rounds the bent value to the nearest level. The decoder bends it back. The result is good detail for quiet parts, which carry most of the information in speech, at the same number of bits as a plain 8-bit scale.
Builds on: Quantization, Logarithm · Leads to: WaveNet
Where it's used
Telephone calls. The international G.711 standard carries voice in mu-law form at 8 bits per sample and 8,000 samples per second, which is 64,000 bits per second. The 8,000 rate follows from the sampling theorem for the narrow band of a telephone line. The North American and Japanese versions of the standard use this curve. Speech is mostly quiet, with loud peaks, so the curve spends more of its levels on the quiet samples, which are the most common.
Speech models that generate raw audio. WaveNet, a 2016 convolutional model that produces sound one sample at a time, needs each sample to be one of a small set of values so that it can predict a probability for every value. The authors convert 16-bit audio to 256 values with a mu-law curve where mu equals 255. They report that this non-linear quantization gives a significantly better reconstruction than a simple linear quantization with the same 256 values, and that for speech the reconstruction sounded very similar to the original.
Any fixed-bit storage of a wide-range signal. The same idea applies whenever a quantity spans a huge range and its small values matter as much as its large ones: a sensor reading, a sound level, or the brightness of a digital image. The curve is chosen so that equal relative errors cost roughly equal bits at every level.
How it works
Take the smallest case. Suppose we may store a signal value only as one of 9 levels: , which means a step of 0.25. The signal is a number between and . We will compare plain rounding with mu-law, using so the arithmetic stays small.
-
Plain rounding first. Round to the nearest multiple of 0.25. The nearest level is 0, so the stored value is 0. Check: the error is , which is 100 percent of the signal. Plain rounding has erased it.
-
Bend the signal with the mu-law curve. Compute . Check: is between and , and it is larger than , so the small value has been stretched.
-
Round the bent value. The nearest level to 0.189 is 0.25, because is closer to than to . Store the level 0.25. Check: counting the 9 levels from as index 0, the level 0.25 is index 5. The stored value is one of the 9 levels, so it fits in 4 bits.
-
Decode the stored level by bending back. The decoder computes . With , , so the decoded value is . Check: the error is about , which is much less than the 0.1 of plain rounding.
-
Now try a large value, . Plain rounding gives the level 0.75, with error 0.05. Good, plain rounding works here.
-
Run the mu-law path on 0.7. Compute . Round to the nearest level, 0.75. Decode: . Check: the error is about 0.09, which is worse than the 0.05 of plain rounding.
-
Name the trade-off. Mu-law gives small values fine steps and large values coarse steps. Small values gain accuracy, and large values lose some. For speech, where quiet samples are common and carry much of the information, this is a good bargain. The curve is the whole idea: the same number of levels, redistributed.
flowchart LR X[Signal value x] --> F[Bend with mu-law curve] F --> Q[Round to nearest of 256 levels] Q --> C[Store the level index in 8 bits] C --> D[Decoder: look up level] D --> E[Bend back with the inverse curve] E --> Y[Approximate value of x]
The math
Let (in telephony and in WaveNet, ). For a signal value with , the mu-law compander is where is for positive , for negative , and at zero. The expander that undoes it is Compression happens by quantizing on a uniform grid, and the decoder applies to the stored level.
Two facts make the definition work. First, and , so the curve maps the full range onto itself. Second, really is the inverse: if , then , which rearranges to .
Step 2 in symbols: the step size in the signal domain
A uniform quantizer that splits into equal steps has step , and its rounding error is at most . For 8 bits, and .
Without companding, the signal-domain step is everywhere. With companding, a small change near produces , and the derivative is So the signal-domain step is . At this is while at it is about . The ratio is . Near zero, the curve gives steps about 128 times finer than at half scale. That is the quantitative content of "quiet sounds keep detail".
Step 4 in symbols: the decoding error
Take with 8-bit mu-law. Then . Multiply by 128 to find the level position, , and round to level 29, which is . The decoder gives . The error is about , which is 1.5 percent of the signal.
Worked examples
Levels are multiples of 0.25 between and , and . Take .
- Compute .
- Divide by the step 0.25 to find the level position: .
- Round to the nearest integer, , which is the level .
- Decode: .
- Compare with plain rounding: , which rounds to , so plain rounding also gives , with error 0.05. Mu-law gives error in absolute value, which is about 0.09.
- Interpretation: for a large value, the curve makes the grid coarser, so mu-law is worse here. The same grid is much better for small values, as step 4 of the walkthrough showed.
Take , a quiet sample, with 256 levels.
- Compute .
- Find the position: , so store level 29.
- The stored value is .
- Decode: , so .
- Compare with linear 8-bit storage. Round to 1, giving the stored value , with error . That is 22 percent of the signal.
- Interpretation: the mu-law error is about 1.5 percent and the linear error is about 22 percent, for the same 8 bits. The quiet sample is recorded far more faithfully.
A telephone call samples speech 8,000 times per second. Compare two encodings of one second of speech.
- With 8-bit mu-law, each sample uses 8 bits. The rate is bits per second.
- With 16-bit linear encoding, each sample uses 16 bits. The rate is bits per second.
- The mu-law encoding therefore halves the rate with the same sampling rate.
- Check the claim on quiet speech. Most samples in ordinary speech are small compared with the peaks, so the fine mu-law steps near zero are used most often. The error on those samples is small, which is why the halved rate sounds acceptable.
- Interpretation: the choice is about where the bits go. The rate is set by the number of bits per sample times the sampling rate, and mu-law changes how the bits are spent on the amplitude scale.
Try it: A speech model samples at 16,000 samples per second and stores each sample as 8-bit mu-law. What is the bit rate in bits per second? Answer: bits per second, the same as 16-bit linear speech at 8,000 samples per second.
Common mistakes and tradeoffs
It is tempting to think mu-law is lossless compression, like a zip file. In fact it is lossy. The encoder rounds the bent value to a grid, and the decoder cannot recover the exact original. The loss is small for quiet values and larger for loud ones.
It is tempting to think any logarithm will do. In fact the offset matters. A pure logarithm would blow up near zero. The offset keeps the curve linear for very small values and logarithmic for large ones, which gives the fine steps near zero that the walkthrough relied on.
It is tempting to think a larger mu is always better. In fact a larger mu makes small values more accurate, but it makes large values coarser, as the step-size formula shows. A larger mu also raises the signal-to-noise ratio of quiet samples, at the cost of loud ones. The value is a standard choice for 8 bits, not a universal optimum.
It is tempting to think a companded number can be added or multiplied like the original. In fact the samples must be expanded first, because the curve is not linear. Averaging two stored levels does not average the signal.
It is tempting to confuse this with the quantization used to shrink neural networks. The weight quantization of a neural network rounds model weights to low precision. Mu-law rounds a signal, through a fixed curve, to a fixed set of levels. The idea of rounding is shared, but the goal is different.
When to use it: for speech and other signals where small values matter as much as large ones, under a fixed bit budget.
When not to use it: for high-fidelity music or measurement data, where linear 16-bit or higher is the standard, and where the loud parts matter as much as the quiet ones. Mu-law is also a poor match when the signal is always near full scale.
Alternatives: A-law, the companding curve used in much of Europe, has a different shape near zero. A signed logarithm such as the symlog transform is a related curve for data that spans many orders of magnitude. Adaptive quantizers change the step size as the signal changes, and learned codecs fit the grid to data, as vector quantization does. Each trades simplicity against fit to the signal.
History
Pulse-code modulation, which stores a signal as a sequence of rounded samples, was developed in the 1930s. Engineers who wanted voice to fit in fewer bits found that a linear grid spends its levels evenly, although loud sounds are rarer in speech. Applying a logarithmic curve before rounding fixed this, and the technique became known as companding, a blend of compressing and expanding.
The mu-law curve became part of the CCITT recommendation G.711 for pulse-code modulation of voice, a predecessor of the ITU-T. The WaveNet paper cites the 1988 edition of G.711 for the curve.
In 2016 the WaveNet paper used the same curve with mu equal to 255 to reduce 16-bit audio to 256 values. A telephony standard cited in that paper thus became a building block of modern audio machine learning.
Recap
- A uniform grid spends the same step size everywhere, so quiet values are rounded as coarsely as loud ones.
- Mu-law first bends the signal with , then rounds the bent value.
- The bent curve has steps about 128 times finer near zero than at half scale when .
- The decoder applies the inverse curve, and the error on small values drops sharply while large values lose some accuracy.
- The bit rate is the sampling rate times the bits per sample, and mu-law keeps the bits per sample fixed while redistributing its levels.
- Read next: WaveNet uses mu-law values as the output of its prediction model, and Decibel (dB) is another logarithmic scale for signal levels.
Check yourself
QuestionIn one sentence, what does mu-law companding do before rounding a signal?Show answer
It bends the signal with a logarithmic curve, which stretches small values and squeezes large ones, so that the fixed grid of levels gives small values finer steps.
QuestionWith mu equal to 255 and 256 levels, what is the bit rate for 8,000 samples per second of 8-bit mu-law audio?Show answer
Each sample uses 8 bits, so the rate is bits per second.
QuestionWhy is the rounding error near zero much smaller with mu-law than with a linear 8-bit grid, even though both use 256 levels?Show answer
The mu-law curve has slope at zero, so each grid step covers about of a linear step in the signal domain. The linear grid has the same step everywhere, so a small value is rounded with the full step size, which is large in relative terms.
QuestionA stored mu-law value is 0.5, with mu equal to 255. Is it correct to add it to another stored value of 0.5 and read the sum as the signal 1.0?Show answer
No. The stored values are compressed, so the signal values are each. Their sum in the signal domain is about 0.118, not 1.0. The samples must be expanded before any arithmetic.