# 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='sym'])

  The `dwt()` function is used to perform single level, one dimensional Discrete Wavelet Transform.

      (cA, cD) = dwt(data, wavelet, mode='sym')

  Parameters:

  - data – Input signal can be NumPy array, Python list or other iterable object. Both single and double precision floating-point data types are supported and the output type depends on the input type. If the input data is not in one of these types it will be converted to the default double precision data format before performing computations.
  - wavelet – Wavelet to use in the transform. This can be a name of the wavelet from the `wavelist()` list or a `Wavelet` object instance.
  - mode – Signal extension mode to deal with the border distortion problem. See MODES for details.

  The transform coefficients are returned as two arrays containing approximation (cA) and detail (cD) coefficients respectively. Length of returned arrays depends on the selected signal extension mode - 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:

  - 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)

  Example:

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

## Multilevel decomposition using `wavedec`

`pywt.``wavedec`(data, wavelet, mode='sym', level=None)

  The `wavedec()` function performs 1D multilevel Discrete Wavelet Transform decomposition of given signal and returns ordered list of coefficients arrays in the form:

  >     [cA_n, cD_n, cD_n-1, ..., cD2, cD1],

  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.

  Parameters:

  - data – Input signal can be NumPy array, Python list or other iterable object. Both single and double precision floating-point data types are supported and the output type depends on the input type. If the input data is not in one of these types it will be converted to the default double precision data format before performing computations.
  - wavelet – Wavelet to use in the transform. This can be a name of the wavelet from the `wavelist()` list or a `Wavelet` object instance.
  - mode – Signal extension mode to deal with the border distortion problem. See MODES for details.
  - level – Number of decomposition steps to perform. If the level is `None`, then the full decomposition up to the level computed with `dwt_max_level()` function for the given data and wavelet lengths is performed.

  Example:

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

## Partial Discrete Wavelet Transform data decomposition `downcoef`

`pywt.``downcoef`(part, data, wavelet[, mode='sym'[, level=1]])

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

  Parameters:

  - part – decomposition type. For `a` computes approximation coefficients, for `d` - details coefficients.
  - data – Input signal can be NumPy array, Python list or other iterable object. Both single and double precision floating-point data types are supported and the output type depends on the input type. If the input data is not in one of these types it will be converted to the default double precision data format before performing computations.
  - wavelet – Wavelet to use in the transform. This can be a name of the wavelet from the `wavelist()` list or a `Wavelet` object instance.
  - mode – Signal extension mode to deal with the border distortion problem. See MODES for details.
  - level – Number of decomposition steps to perform.

## Maximum decomposition level - `dwt_max_level`

`pywt.``dwt_max_level`(data_len, filter_len)

  The `dwt_max_level()` function can be used to compute the maximum useful level of decomposition for the given input data length and wavelet filter length.

  The returned value equals to:

      floor( log(data_len/(filter_len-1)) / log(2) )

  Although the maximum decomposition level can be quite high for long signals, usually smaller values are chosen depending on the application.

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

  Example:

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

## Result coefficients length - `dwt_coeff_len`

`pywt.``dwt_coeff_len`(data_len, filter_len, mode)

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

For periodization mode this equals:

    ceil(data_len / 2)

which is the lowest possible length guaranteeing perfect reconstruction.

For other modes:

    floor((data_len + filter_len - 1) / 2)

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