2D Forward and Inverse Discrete Wavelet Transform¶
Single level dwt2
¶
- pywt.dwt2(data, wavelet, mode='symmetric', axes=(-2, -1))¶
2D Discrete Wavelet Transform.
- Parameters
- dataarray_like
2D array with input data
- waveletWavelet object or name string, or 2-tuple of wavelets
Wavelet to use. This can also be a tuple containing a wavelet to apply along each axis in
axes
.- modestr or 2-tuple of strings, optional
Signal extension mode, see Modes. This can also be a tuple of modes specifying the mode to use on each axis in
axes
.- axes2-tuple of ints, optional
Axes over which to compute the DWT. Repeated elements mean the DWT will be performed multiple times along these axes.
- Returns
- (cA, (cH, cV, cD))tuple
Approximation, horizontal detail, vertical detail and diagonal detail coefficients respectively. Horizontal refers to array axis 0 (or
axes[0]
for user-specifiedaxes
).
Examples
>>> import numpy as np >>> import pywt >>> data = np.ones((4,4), dtype=np.float64) >>> coeffs = pywt.dwt2(data, 'haar') >>> cA, (cH, cV, cD) = coeffs >>> cA array([[ 2., 2.], [ 2., 2.]]) >>> cV array([[ 0., 0.], [ 0., 0.]])
The relation to the other common data layout where all the approximation and details coefficients are stored in one big 2D array is as follows:
------------------- | | | | cA(LL) | cH(LH) | | | | (cA, (cH, cV, cD)) <---> ------------------- | | | | cV(HL) | cD(HH) | | | | -------------------
PyWavelets does not follow this pattern because of pure practical reasons of simple access to particular type of the output coefficients.
Single level idwt2
¶
- pywt.idwt2(coeffs, wavelet, mode='symmetric', axes=(-2, -1))¶
2-D Inverse Discrete Wavelet Transform.
Reconstructs data from coefficient arrays.
- Parameters
- coeffstuple
(cA, (cH, cV, cD)) A tuple with approximation coefficients and three details coefficients 2D arrays like from
dwt2
. If any of these components are set toNone
, it will be treated as zeros.- waveletWavelet object or name string, or 2-tuple of wavelets
Wavelet to use. This can also be a tuple containing a wavelet to apply along each axis in
axes
.- modestr or 2-tuple of strings, optional
Signal extension mode, see Modes. This can also be a tuple of modes specifying the mode to use on each axis in
axes
.- axes2-tuple of ints, optional
Axes over which to compute the IDWT. Repeated elements mean the IDWT will be performed multiple times along these axes.
Examples
>>> import numpy as np >>> import pywt >>> data = np.array([[1,2], [3,4]], dtype=np.float64) >>> coeffs = pywt.dwt2(data, 'haar') >>> pywt.idwt2(coeffs, 'haar') array([[ 1., 2.], [ 3., 4.]])
2D multilevel decomposition using wavedec2
¶
- pywt.wavedec2(data, wavelet, mode='symmetric', level=None, axes=(-2, -1))¶
Multilevel 2D Discrete Wavelet Transform.
- Parameters
- datandarray
2D input data
- waveletWavelet object or name string, or 2-tuple of wavelets
Wavelet to use. This can also be a tuple containing a wavelet to apply along each axis in
axes
.- modestr or 2-tuple of str, optional
Signal extension mode, see Modes. This can also be a tuple containing a mode to apply along each axis in
axes
.- levelint, optional
Decomposition level (must be >= 0). If level is None (default) then it will be calculated using the
dwt_max_level
function.- axes2-tuple of ints, optional
Axes over which to compute the DWT. Repeated elements are not allowed.
- Returns
- [cAn, (cHn, cVn, cDn), … (cH1, cV1, cD1)]list
Coefficients list. For user-specified
axes
,cH*
corresponds toaxes[0]
whilecV*
corresponds toaxes[1]
. The first element returned is the approximation coefficients for the nth level of decomposition. Remaining elements are tuples of detail coefficients in descending order of decomposition level. (i.e.cH1
are the horizontal detail coefficients at the first level)
Examples
>>> import pywt >>> import numpy as np >>> coeffs = pywt.wavedec2(np.ones((4,4)), 'db1') >>> # Levels: >>> len(coeffs)-1 2 >>> pywt.waverec2(coeffs, 'db1') array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
2D multilevel reconstruction using waverec2
¶
- pywt.waverec2(coeffs, wavelet, mode='symmetric', axes=(-2, -1))¶
Multilevel 2D Inverse Discrete Wavelet Transform.
- coeffslist or tuple
Coefficients list [cAn, (cHn, cVn, cDn), … (cH1, cV1, cD1)]
- waveletWavelet object or name string, or 2-tuple of wavelets
Wavelet to use. This can also be a tuple containing a wavelet to apply along each axis in
axes
.- modestr or 2-tuple of str, optional
Signal extension mode, see Modes. This can also be a tuple containing a mode to apply along each axis in
axes
.- axes2-tuple of ints, optional
Axes over which to compute the IDWT. Repeated elements are not allowed.
- Returns
- 2D array of reconstructed data.
Notes
It may sometimes be desired to run
waverec2
with some sets of coefficients omitted. This can best be done by setting the corresponding arrays to zero arrays of matching shape and dtype. Explicitly removing list or tuple entries or setting them to None is not supported.Specifically, to ignore all detail coefficients at level 2, one could do:
coeffs[-2] == tuple([np.zeros_like(v) for v in coeffs[-2]])
Examples
>>> import pywt >>> import numpy as np >>> coeffs = pywt.wavedec2(np.ones((4,4)), 'db1') >>> # Levels: >>> len(coeffs)-1 2 >>> pywt.waverec2(coeffs, 'db1') array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
2D coordinate conventions¶
The labels for “horizontal” and “vertical” used by dwt2
and idwt2
follow the common mathematical convention that coordinate axis 0
is horizontal while axis 1 is vertical:
dwt2, idwt2 convention
----------------------
axis 1 ^
|
|
|
|--------->
axis 0
Note that this is different from another common convention used in computer
graphics and image processing (e.g. by matplotlib’s imshow
and functions in
scikit-image
). In those packages axis 0 is a vertical axis and axis 1 is
horizontal as follows:
imshow convention
-------------------
axis 1
|--------->
|
|
|
axis 0 v