Caution

You're reading an old version of this documentation. If you want up-to-date information, please have a look at 0.10.2.

librosa.mu_expand

librosa.mu_expand(x, *, mu=255.0, quantize=True)[source]

mu-law expansion

This function is the inverse of mu_compress. Given a mu-law compressed signal -1 <= x <= 1, the mu-law expansion is calculated by:

sign(x) * (1 / mu) * ((1 + mu)**abs(x) - 1)
Parameters:
xnp.ndarray

The compressed signal. If quantize=True, values must be in the range [-1, +1].

mupositive number

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

quantizeboolean

If True, the input is assumed to be quantized to 1 + mu distinct integer values.

Returns:
x_expandednp.ndarray with values in the range [-1, +1]

The mu-law expanded signal.

Raises:
ParameterError

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

See also

mu_compress

Examples

Compress and expand 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.        ])
>>> z = librosa.mu_expand(y, quantize=False)
>>> z
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.        ])

Compress and expand with quantization. Note that this necessarily incurs quantization error, particularly for values near +-1.

>>> 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])
>>> z = librosa.mu_expand(y, quantize=True)
array([-1.        , -0.84027248, -0.70595818, -0.59301377, -0.4563785 ,
       -0.32155973, -0.19817918, -0.06450245,  0.06450245,  0.19817918,
        0.32155973,  0.4563785 ,  0.59301377,  0.70595818,  0.84027248,
        0.95743702])