# audtorch.utils¶

Utility functions.

## flatten_list¶

audtorch.utils.flatten_list(nested_list)

Flatten an arbitrarily nested list.

Implemented without recursion to avoid stack overflows. Returns a new list, the original list is unchanged.

Parameters: nested_list (list) – nested list flattened list list

Example

>>> flatten_list([1, 2, 3, , [], [[[[[[[[]]]]]]]]])
[1, 2, 3, 4, 5]
>>> flatten_list([[1, 2], 3])
[1, 2, 3]


## to_tuple¶

audtorch.utils.to_tuple(input, *, tuple_len=2)

Convert to tuple of given length.

This utility function is used to convert single-value arguments to tuples of appropriate length, e.g. for multi-dimensional inputs where each dimension requires the same value. If the argument is already an iterable it is returned as a tuple if its length matches the desired tuple length. Otherwise a ValueError is raised.

Parameters: input (non-iterable or iterable) – argument to be converted to tuple tuple_len (int) – required length of argument tuple. Default: 2 tuple of desired length tuple

Example

>>> to_tuple(2)
(2, 2)


## energy¶

audtorch.utils.energy(signal)

Energy of input signal.

$E = \sum_n |x_n|^2$
Parameters: signal (numpy.ndarray) – signal energy of signal float

Example

>>> a = np.array([[2, 2]])
>>> energy(a)
8


## power¶

audtorch.utils.power(signal)

Power of input signal.

$P = {1 \over N} \sum_n |x_n|^2$
Parameters: signal (numpy.ndarray) – signal power of signal float

Example

>>> a = np.array([[2, 2]])
>>> power(a)
4.0


audtorch.utils.run_worker_threads(num_workers, task_fun, params, *, progress_bar=False)

>>> power = lambda x, n: x ** n