Discrete Wavelet Transform (DWT)

Wavelet transform has recently become a very popular when it comes to analysis, de-noising and compression of signals and images. This section describes functions used to perform single- and multilevel Discrete Wavelet Transforms.

Single level dwt

pywt.dwt(data, wavelet, mode='symmetric', axis=-1)

Single level Discrete Wavelet Transform.

Parameters:

data : array_like

Input signal

wavelet : Wavelet object or name

Wavelet to use

mode : str, optional

Signal extension mode, see Modes

axis: int, optional

Axis over which to compute the DWT. If not given, the last axis is used.

Returns:

(cA, cD) : tuple

Approximation and detail coefficients.

Notes

Length of coefficients arrays depends on the selected mode. For all modes except periodization:

len(cA) == len(cD) == floor((len(data) + wavelet.dec_len - 1) / 2)

For periodization mode (“per”):

len(cA) == len(cD) == ceil(len(data) / 2)

Examples

>>> import pywt
>>> (cA, cD) = pywt.dwt([1, 2, 3, 4, 5, 6], 'db1')
>>> cA
array([ 2.12132034,  4.94974747,  7.77817459])
>>> cD
array([-0.70710678, -0.70710678, -0.70710678])

See the signal extension modes section for the list of available options and the dwt_coeff_len() function for information on getting the expected result length.

The transform can be performed over one axis of multi-dimensional data. By default this is the last axis. For multi-dimensional transforms see the 2D transforms section.

Multilevel decomposition using wavedec

pywt.wavedec(data, wavelet, mode='symmetric', level=None, axis=-1)

Multilevel 1D Discrete Wavelet Transform of data.

Parameters:

data: array_like

Input data

wavelet : Wavelet object or name string

Wavelet to use

mode : str, optional

Signal extension mode, see Modes (default: ‘symmetric’)

level : int, optional

Decomposition level (must be >= 0). If level is None (default) then it will be calculated using the dwt_max_level function.

axis: int, optional

Axis over which to compute the DWT. If not given, the last axis is used.

Returns:

[cA_n, cD_n, cD_n-1, ..., cD2, cD1] : list

Ordered list of coefficients arrays where n denotes the level of decomposition. The first element (cA_n) of the result is approximation coefficients array and the following elements (cD_n - cD_1) are details coefficients arrays.

Examples

>>> from pywt import wavedec
>>> coeffs = wavedec([1,2,3,4,5,6,7,8], 'db1', level=2)
>>> cA2, cD2, cD1 = coeffs
>>> cD1
array([-0.70710678, -0.70710678, -0.70710678, -0.70710678])
>>> cD2
array([-2., -2.])
>>> cA2
array([  5.,  13.])

Partial Discrete Wavelet Transform data decomposition downcoef

pywt.downcoef(part, data, wavelet, mode='symmetric', level=1)

Partial Discrete Wavelet Transform data decomposition.

Similar to pywt.dwt, but computes only one set of coefficients. Useful when you need only approximation or only details at the given level.

Parameters:

part : str

Coefficients type:

  • ‘a’ - approximations reconstruction is performed
  • ‘d’ - details reconstruction is performed

data : array_like

Input signal.

wavelet : Wavelet object or name

Wavelet to use

mode : str, optional

Signal extension mode, see Modes. Default is ‘symmetric’.

level : int, optional

Decomposition level. Default is 1.

Returns:

coeffs : ndarray

1-D array of coefficients.

See also

upcoef

Maximum decomposition level - dwt_max_level

pywt.dwt_max_level(data_len, filter_len)

Compute the maximum useful level of decomposition.

Parameters:

data_len : int

Input data length.

filter_len : int

Wavelet filter length.

Returns:

max_level : int

Maximum level.

Examples

>>> import pywt
>>> w = pywt.Wavelet('sym5')
>>> pywt.dwt_max_level(data_len=1000, filter_len=w.dec_len)
6
>>> pywt.dwt_max_level(1000, w)
6

Result coefficients length - dwt_coeff_len

pywt.dwt_coeff_len(data_len, filter_len, mode='symmetric')

Returns length of dwt output for given data length, filter length and mode

Parameters:

data_len : int

Data length.

filter_len : int

Filter length.

mode : str, optional (default: ‘symmetric’)

Signal extension mode, see Modes

Returns:

len : int

Length of dwt output.

Notes

For all modes except periodization:

len(cA) == len(cD) == floor((len(data) + wavelet.dec_len - 1) / 2)

for periodization mode (“per”):

len(cA) == len(cD) == ceil(len(data) / 2)

Based on the given input data length (data_len), wavelet decomposition filter length (filter_len) and signal extension mode, the dwt_coeff_len() function calculates the length of the resulting coefficients arrays that would be created while performing dwt() transform.

filter_len can be either an int or Wavelet object for convenience.