| 1 |
.. _reg-dwt-idwt: |
| 2 |
|
| 3 |
.. currentmodule:: pywt |
| 4 |
|
| 5 |
DWT and IDWT |
| 6 |
============ |
| 7 |
|
| 8 |
Discrete Wavelet Transform |
| 9 |
-------------------------- |
| 10 |
|
| 11 |
Let's do a :func:`Discrete Wavelet Transform <dwt>` of a sample data ``x`` |
| 12 |
using the ``db2`` wavelet. It's simple.. |
| 13 |
|
| 14 |
>>> import pywt |
| 15 |
>>> x = [3, 7, 1, 1, -2, 5, 4, 6] |
| 16 |
>>> cA, cD = pywt.dwt(x, 'db2') |
| 17 |
|
| 18 |
And the approximation and details coefficients are in ``cA`` and ``cD`` |
| 19 |
respectively: |
| 20 |
|
| 21 |
>>> print(cA) |
| 22 |
[ 5.65685425 7.39923721 0.22414387 3.33677403 7.77817459] |
| 23 |
>>> print(cD) |
| 24 |
[-2.44948974 -1.60368225 -4.44140056 -0.41361256 1.22474487] |
| 25 |
|
| 26 |
Inverse Discrete Wavelet Transform |
| 27 |
---------------------------------- |
| 28 |
|
| 29 |
Now let's do an opposite operation |
| 30 |
- :func:`Inverse Discrete Wavelet Transform <idwt>`: |
| 31 |
|
| 32 |
>>> print(pywt.idwt(cA, cD, 'db2')) |
| 33 |
[ 3. 7. 1. 1. -2. 5. 4. 6.] |
| 34 |
|
| 35 |
VoilĂ ! That's it! |
| 36 |
|
| 37 |
More Examples |
| 38 |
------------- |
| 39 |
|
| 40 |
Now let's experiment with the :func:`dwt` some more. For example let's pass a |
| 41 |
:class:`Wavelet` object instead of the wavelet name and specify signal |
| 42 |
extension mode (the default is :ref:`symmetric <Modes.symmetric>`) for the |
| 43 |
border effect handling: |
| 44 |
|
| 45 |
>>> w = pywt.Wavelet('sym3') |
| 46 |
>>> cA, cD = pywt.dwt(x, wavelet=w, mode='constant') |
| 47 |
>>> print(cA) |
| 48 |
[ 4.38354585 3.80302657 7.31813271 -0.58565539 4.09727044 7.81994027] |
| 49 |
>>> print(cD) |
| 50 |
[-1.33068221 -2.78795192 -3.16825651 -0.67715519 -0.09722957 -0.07045258] |
| 51 |
|
| 52 |
Note that the output coefficients arrays length depends not only on the input |
| 53 |
data length but also on the :class:Wavelet type (particularly on its |
| 54 |
:attr:`filters length <~Wavelet.dec_len>` that are used in the transformation). |
| 55 |
|
| 56 |
To find out what will be the output data size use the :func:`dwt_coeff_len` |
| 57 |
function: |
| 58 |
|
| 59 |
>>> # int() is for normalizing Python integers and long integers for documentation tests |
| 60 |
>>> int(pywt.dwt_coeff_len(data_len=len(x), filter_len=w.dec_len, mode='symmetric')) |
| 61 |
6 |
| 62 |
>>> int(pywt.dwt_coeff_len(len(x), w, 'symmetric')) |
| 63 |
6 |
| 64 |
>>> len(cA) |
| 65 |
6 |
| 66 |
|
| 67 |
Looks fine. (And if you expected that the output length would be a half of the |
| 68 |
input data length, well, that's the trade-off that allows for the perfect |
| 69 |
reconstruction...). |
| 70 |
|
| 71 |
The third argument of the :func:`dwt_coeff_len` is the already mentioned signal |
| 72 |
extension mode (please refer to the PyWavelets' documentation for the |
| 73 |
:ref:`modes <modes>` description). Currently there are six |
| 74 |
:ref:`extension modes <Modes>` available: |
| 75 |
|
| 76 |
>>> pywt.Modes.modes |
| 77 |
['zero', 'constant', 'symmetric', 'periodic', 'smooth', 'periodization', 'reflect', 'antisymmetric', 'antireflect'] |
| 78 |
|
| 79 |
As you see in the above example, the :ref:`periodization <Modes.periodization>` |
| 80 |
(periodization) mode is slightly different from the others. It's aim when |
| 81 |
doing the :func:`DWT <dwt>` transform is to output coefficients arrays that |
| 82 |
are half of the length of the input data. |
| 83 |
|
| 84 |
Knowing that, you should never mix the periodization mode with other modes when |
| 85 |
doing :func:`DWT <dwt>` and :func:`IDWT <idwt>`. Otherwise, it will produce |
| 86 |
**invalid results**: |
| 87 |
|
| 88 |
>>> x |
| 89 |
[3, 7, 1, 1, -2, 5, 4, 6] |
| 90 |
>>> cA, cD = pywt.dwt(x, wavelet=w, mode='periodization') |
| 91 |
>>> print(pywt.idwt(cA, cD, 'sym3', 'symmetric')) # invalid mode |
| 92 |
[ 1. 1. -2. 5.] |
| 93 |
>>> print(pywt.idwt(cA, cD, 'sym3', 'periodization')) |
| 94 |
[ 3. 7. 1. 1. -2. 5. 4. 6.] |
| 95 |
|
| 96 |
|
| 97 |
Tips & tricks |
| 98 |
------------- |
| 99 |
|
| 100 |
Passing ``None`` instead of coefficients data to :func:`idwt` |
| 101 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 102 |
|
| 103 |
Now some tips & tricks. Passing ``None`` as one of the coefficient arrays |
| 104 |
parameters is similar to passing a *zero-filled* array. The results are simply |
| 105 |
the same: |
| 106 |
|
| 107 |
>>> print(pywt.idwt([1,2,0,1], None, 'db2', 'symmetric')) |
| 108 |
[ 1.19006969 1.54362308 0.44828774 -0.25881905 0.48296291 0.8365163 ] |
| 109 |
|
| 110 |
>>> print(pywt.idwt([1, 2, 0, 1], [0, 0, 0, 0], 'db2', 'symmetric')) |
| 111 |
[ 1.19006969 1.54362308 0.44828774 -0.25881905 0.48296291 0.8365163 ] |
| 112 |
|
| 113 |
>>> print(pywt.idwt(None, [1, 2, 0, 1], 'db2', 'symmetric')) |
| 114 |
[ 0.57769726 -0.93125065 1.67303261 -0.96592583 -0.12940952 -0.22414387] |
| 115 |
|
| 116 |
>>> print(pywt.idwt([0, 0, 0, 0], [1, 2, 0, 1], 'db2', 'symmetric')) |
| 117 |
[ 0.57769726 -0.93125065 1.67303261 -0.96592583 -0.12940952 -0.22414387] |
| 118 |
|
| 119 |
Remember that only one argument at a time can be ``None``: |
| 120 |
|
| 121 |
>>> print(pywt.idwt(None, None, 'db2', 'symmetric')) |
| 122 |
Traceback (most recent call last): |
| 123 |
... |
| 124 |
ValueError: At least one coefficient parameter must be specified. |
| 125 |
|
| 126 |
|
| 127 |
Coefficients data size in :attr:`idwt` |
| 128 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 129 |
|
| 130 |
When doing the :func:`IDWT <idwt>` transform, usually the coefficient arrays |
| 131 |
must have the same size. |
| 132 |
|
| 133 |
>>> print(pywt.idwt([1, 2, 3, 4, 5], [1, 2, 3, 4], 'db2', 'symmetric')) |
| 134 |
Traceback (most recent call last): |
| 135 |
... |
| 136 |
ValueError: Coefficients arrays must have the same size. |
| 137 |
|
| 138 |
|
| 139 |
Not every coefficient array can be used in :func:`IDWT <idwt>`. In the |
| 140 |
following example the :func:`idwt` will fail because the input arrays are |
| 141 |
invalid - they couldn't be created as a result of :func:`DWT <dwt>`, because |
| 142 |
the minimal output length for dwt using ``db4`` wavelet and the :ref:`symmetric |
| 143 |
<Modes.symmetric>` mode is ``4``, not ``3``: |
| 144 |
|
| 145 |
>>> pywt.idwt([1,2,4], [4,1,3], 'db4', 'symmetric') |
| 146 |
Traceback (most recent call last): |
| 147 |
... |
| 148 |
ValueError: Invalid coefficient arrays length for specified wavelet. Wavelet and mode must be the same as used for decomposition. |
| 149 |
|
| 150 |
>>> int(pywt.dwt_coeff_len(1, pywt.Wavelet('db4').dec_len, 'symmetric')) |
| 151 |
4 |