Extend a 1D signal using a given boundary mode. This function operates like :func:`numpy.pad` but supports all signal extension modes that can be used by PyWavelets discrete wavelet transforms. Parameters ---------- x : ndarray The array to pad pad_widths : {sequenc
(x, pad_widths, mode)
| 402 | |
| 403 | |
| 404 | def pad(x, pad_widths, mode): |
| 405 | """Extend a 1D signal using a given boundary mode. |
| 406 | |
| 407 | This function operates like :func:`numpy.pad` but supports all signal |
| 408 | extension modes that can be used by PyWavelets discrete wavelet transforms. |
| 409 | |
| 410 | Parameters |
| 411 | ---------- |
| 412 | x : ndarray |
| 413 | The array to pad |
| 414 | pad_widths : {sequence, array_like, int} |
| 415 | Number of values padded to the edges of each axis. |
| 416 | ``((before_1, after_1), … (before_N, after_N))`` unique pad widths for |
| 417 | each axis. ``((before, after),)`` yields same before and after pad for |
| 418 | each axis. ``(pad,)`` or int is a shortcut for |
| 419 | ``before = after = pad width`` for all axes. |
| 420 | mode : str, optional |
| 421 | Signal extension mode, see :ref:`Modes <ref-modes>`. |
| 422 | |
| 423 | Returns |
| 424 | ------- |
| 425 | pad : ndarray |
| 426 | Padded array of rank equal to array with shape increased according to |
| 427 | ``pad_widths``. |
| 428 | |
| 429 | Notes |
| 430 | ----- |
| 431 | The performance of padding in dimensions > 1 may be substantially slower |
| 432 | for modes ``'smooth'`` and ``'antisymmetric'`` as these modes are not |
| 433 | supported efficiently by the underlying :func:`numpy.pad` function. |
| 434 | |
| 435 | Note that the behavior of the ``'constant'`` mode here follows the |
| 436 | PyWavelets convention which is different from NumPy (it is equivalent to |
| 437 | ``mode='edge'`` in :func:`numpy.pad`). |
| 438 | """ |
| 439 | x = np.asanyarray(x) |
| 440 | |
| 441 | # process pad_widths exactly as in numpy.pad |
| 442 | pad_widths = np.array(pad_widths) |
| 443 | pad_widths = np.round(pad_widths).astype(np.intp, copy=False) |
| 444 | if pad_widths.min() < 0: |
| 445 | raise ValueError("pad_widths must be > 0") |
| 446 | pad_widths = np.broadcast_to(pad_widths, (x.ndim, 2)).tolist() |
| 447 | |
| 448 | if mode in ['symmetric', 'reflect']: |
| 449 | xp = np.pad(x, pad_widths, mode=mode) |
| 450 | elif mode in ['periodic', 'periodization']: |
| 451 | if mode == 'periodization': |
| 452 | # Promote odd-sized dimensions to even length by duplicating the |
| 453 | # last value. |
| 454 | edge_pad_widths = [(0, x.shape[ax] % 2) |
| 455 | for ax in range(x.ndim)] |
| 456 | x = np.pad(x, edge_pad_widths, mode='edge') |
| 457 | xp = np.pad(x, pad_widths, mode='wrap') |
| 458 | elif mode == 'zero': |
| 459 | xp = np.pad(x, pad_widths, mode='constant', constant_values=0) |
| 460 | elif mode == 'constant': |
| 461 | xp = np.pad(x, pad_widths, mode='edge') |
no outgoing calls
no test coverage detected