librosa.mu_compress

librosa.mu_compress(x, *, mu=255, quantize=True)[source]

mu-law compression

Given an input signal -1 <= x <= 1, the mu-law compression is calculated by:

sign(x) * ln(1 + mu * abs(x)) /  ln(1 + mu)
Parameters:
xnp.ndarray with values in [-1, +1]

The input signal to compress

mupositive number

The compression parameter. Values of the form 2**n - 1 (e.g., 15, 31, 63, etc.) are most common.

quantizebool

If True, quantize the compressed values into 1 + mu distinct integer values.

If False, mu-law compression is applied without quantization.

Returns:
x_compressednp.ndarray

The compressed signal.

Raises:
ParameterError

If x has values outside the range [-1, +1] If mu <= 0

See also

mu_expand

Examples

Compression without quantization

>>> x = np.linspace(-1, 1, num=16)
>>> x
array([-1.        , -0.86666667, -0.73333333, -0.6       , -0.46666667,
       -0.33333333, -0.2       , -0.06666667,  0.06666667,  0.2       ,
        0.33333333,  0.46666667,  0.6       ,  0.73333333,  0.86666667,
        1.        ])
>>> y = librosa.mu_compress(x, quantize=False)
>>> y
array([-1.        , -0.97430198, -0.94432361, -0.90834832, -0.86336132,
       -0.80328309, -0.71255496, -0.52124063,  0.52124063,  0.71255496,
        0.80328309,  0.86336132,  0.90834832,  0.94432361,  0.97430198,
        1.        ])

Compression with quantization

>>> y = librosa.mu_compress(x, quantize=True)
>>> y
array([-128, -124, -120, -116, -110, -102,  -91,  -66,   66,   91,  102,
       110,  116,  120,  124,  127])

Compression with quantization and a smaller range

>>> y = librosa.mu_compress(x, mu=15, quantize=True)
>>> y
array([-8, -7, -7, -6, -6, -5, -4, -2,  2,  4,  5,  6,  6,  7,  7,  7])