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
Maximum decomposition level - dwt_max_level
, dwtn_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, str or Wavelet
The wavelet filter length. Alternatively, the name of a discrete wavelet or a Wavelet object can be specified.
Returns: - max_level : int
Maximum level.
Notes
The rational for the choice of levels is the maximum level where at least one coefficient in the output is uncorrupted by edge effects caused by signal extension. Put another way, decomposition stops when the signal becomes shorter than the FIR filter length for a given wavelet. This corresponds to:
\[\mathtt{max\_level} = \left\lfloor\log_2\left(\mathtt{ \frac{data\_len}{filter\_len - 1}}\right)\right\rfloor\]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 >>> pywt.dwt_max_level(1000, 'sym5') 6
-
pywt.
dwtn_max_level
(shape, wavelet, axes=None)¶ Compute the maximum level of decomposition for n-dimensional data.
This returns the maximum number of levels of decomposition suitable for use with wavedec, wavedec2 or wavedecn.
Parameters: - shape : sequence of ints
Input data shape.
- wavelet : Wavelet object or name string, or tuple of wavelets
Wavelet to use. This can also be a tuple containing a wavelet to apply along each axis in axes.
- axes : sequence of ints, optional
Axes over which to compute the DWT. Axes may not be repeated.
Returns: - level : int
Maximum level.
Notes
The level returned is the smallest dwt_max_level over all axes.
Examples
>>> import pywt >>> pywt.dwtn_max_level((64, 32), 'db2') 3
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.