librosa.util.stack

librosa.util.stack(arrays, *, axis=0)[source]

Stack one or more arrays along a target axis.

This function is similar to np.stack, except that memory contiguity is retained when stacking along the first dimension.

This is useful when combining multiple monophonic audio signals into a multi-channel signal, or when stacking multiple feature representations to form a multi-dimensional array.

Parameters
arrayslist

one or more np.ndarray

axisinteger

The target axis along which to stack. axis=0 creates a new first axis, and axis=-1 creates a new last axis.

Returns
arr_stacknp.ndarray [shape=(len(arrays), array_shape) or shape=(array_shape, len(arrays))]

The input arrays, stacked along the target dimension.

If axis=0, then arr_stack will be F-contiguous. Otherwise, arr_stack will be C-contiguous by default, as computed by np.stack.

Raises
ParameterError
  • If arrays do not all have the same shape

  • If no arrays are given

Examples

Combine two buffers into a contiguous arrays

>>> y_left = np.ones(5)
>>> y_right = -np.ones(5)
>>> y_stereo = librosa.util.stack([y_left, y_right], axis=0)
>>> y_stereo
array([[ 1.,  1.,  1.,  1.,  1.],
       [-1., -1., -1., -1., -1.]])
>>> y_stereo.flags
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

Or along the trailing axis

>>> y_stereo = librosa.util.stack([y_left, y_right], axis=-1)
>>> y_stereo
array([[ 1., -1.],
       [ 1., -1.],
       [ 1., -1.],
       [ 1., -1.],
       [ 1., -1.]])
>>> y_stereo.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False