pwtools.signal.pad_zeros

pwtools.signal.pad_zeros(arr, axis=0, where='end', nadd=None, upto=None, tonext=None, tonext_min=None)[source]

Pad an nd-array with zeros. Default is to append an array of zeros of the same shape as arr to arr’s end along axis.

Parameters:
  • arr (nd array)

  • axis (the axis along which to pad)

  • where (string {'end', 'start'}, pad at the end ("append to array") or) – start (“prepend to array”) of axis

  • nadd (Use only one of) – of an 1d array)

  • upto (pad until arr.shape[axis] == upto)

  • tonext (bool, pad up to the next power of two (pad so that the padded) – array has a length of power of two)

  • tonext_min (int, when using tonext, pad the array to the next possible) – power of two for which the resulting array length along axis is at least tonext_min; the default is tonext_min = arr.shape[axis]

  • nadd

  • upto

  • tonext.

Return type:

padded array

Examples

>>> # 1d
>>> pad_zeros(a)
array([1, 2, 3, 0, 0, 0])
>>> pad_zeros(a, nadd=3)
array([1, 2, 3, 0, 0, 0])
>>> pad_zeros(a, upto=6)
array([1, 2, 3, 0, 0, 0])
>>> pad_zeros(a, nadd=1)
array([1, 2, 3, 0])
>>> pad_zeros(a, nadd=1, where='start')
array([0, 1, 2, 3])
>>> # 2d
>>> a=arange(9).reshape(3,3)
>>> pad_zeros(a, nadd=1, axis=0)
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [0, 0, 0]])
>>> pad_zeros(a, nadd=1, axis=1)
array([[0, 1, 2, 0],
       [3, 4, 5, 0],
       [6, 7, 8, 0]])
>>> # up to next power of two
>>> 2**arange(10)
array([  1,   2,   4,   8,  16,  32,  64, 128, 256, 512])
>>> pydos.pad_zeros(arange(9), tonext=True).shape
(16,)