lin2mu

Converts linear audio samples to 8-bit mu-law.

Category: Data Import and Analysis > Data Import and Export

Syntax

mu = lin2mu(y)

Encodes samples in the range -1 to 1 as mu-law values from 0 to 255.

Description

Mu-law companding squeezes the dynamic range so that quiet sounds keep more resolution than loud ones, which is why it is used in telephony. Eight bits per sample are enough for speech quality that would otherwise need 13 or more linear bits. Decode with mu2lin; the round trip is lossy.

Input arguments

y
Linear samples, normally between -1 and 1.

Output arguments

mu
Mu-law codes, as integers from 0 to 255.

Example

Encode and decode

y = [-1 -0.5 0 0.5 1];
mu = lin2mu(y)
mu2lin(mu)
Output
mu = 1×5

     0    15   255   143   128
ans = 1×5

   -0.9803   -0.5116         0    0.5116    0.9803

See also

mu2lin audioread audiowrite