Tip

This page can also be run or downloaded as a Jupyter notebook.

  • Run in a new tab using JupyterLite[1]:

  • Download:

Signal Extension Modes#

Import pywt first:

import pywt

and construct a helper function that can format arrays in a consistent manner across different systems. Please note that this function is just for the purpose of this example and is not part of the PyWavelets library, and it is not necessary or required to use it in your own code:

def format_array(a):
    """Consistent array representation across different systems"""
    import numpy
    a = numpy.where(numpy.abs(a) < 1e-5, 0, a)
    return numpy.array2string(a, precision=5, separator=' ', suppress_small=True)

A list of available signal extension modes:

pywt.Modes.modes
['zero',
 'constant',
 'symmetric',
 'periodic',
 'smooth',
 'periodization',
 'reflect',
 'antisymmetric',
 'antireflect']

An invalid mode name should raise a ValueError:

pywt.dwt([1,2,3,4], 'db2', 'invalid')
Traceback (most recent call last):
...
ValueError: Unknown mode name 'invalid'.

You can also refer to modes via the attributes of the Modes class:

x = [1, 2, 1, 5, -1, 8, 4, 6]
for mode_name in ['zero', 'constant', 'symmetric', 'reflect', 'periodic', 'smooth', 'periodization']:
    mode = getattr(pywt.Modes, mode_name)
    cA, cD = pywt.dwt(x, 'db2', mode)
    print("Mode: %d (%s)" % (mode, mode_name))
Mode: 0 (zero)
Mode: 2 (constant)
Mode: 1 (symmetric)
Mode: 6 (reflect)
Mode: 4 (periodic)
Mode: 3 (smooth)
Mode: 5 (periodization)

The default mode is symmetric:

cA, cD = pywt.dwt(x, 'db2')
cA
array([1.76776695, 1.73309178, 3.40612438, 6.32928585, 7.77817459])
cD
array([-0.61237244, -2.15599552, -5.95034847, -1.21545369,  1.22474487])
pywt.idwt(cA, cD, 'db2')
array([ 1.,  2.,  1.,  5., -1.,  8.,  4.,  6.])

Specify the mode using a keyword argument:

cA, cD = pywt.dwt(x, 'db2', mode='symmetric')
cA
array([1.76776695, 1.73309178, 3.40612438, 6.32928585, 7.77817459])
cD
array([-0.61237244, -2.15599552, -5.95034847, -1.21545369,  1.22474487])
pywt.idwt(cA, cD, 'db2')
array([ 1.,  2.,  1.,  5., -1.,  8.,  4.,  6.])