| 1 |
.. _reg-wp: |
| 2 |
|
| 3 |
.. currentmodule:: pywt |
| 4 |
|
| 5 |
>>> from __future__ import print_function |
| 6 |
|
| 7 |
Wavelet Packets |
| 8 |
=============== |
| 9 |
|
| 10 |
Import pywt |
| 11 |
----------- |
| 12 |
|
| 13 |
>>> import pywt |
| 14 |
|
| 15 |
>>> def format_array(a): |
| 16 |
... """Consistent array representation across different systems""" |
| 17 |
... import numpy |
| 18 |
... a = numpy.where(numpy.abs(a) < 1e-5, 0, a) |
| 19 |
... return numpy.array2string(a, precision=5, separator=' ', suppress_small=True) |
| 20 |
|
| 21 |
|
| 22 |
Create Wavelet Packet structure |
| 23 |
------------------------------- |
| 24 |
|
| 25 |
Ok, let's create a sample :class:`WaveletPacket`: |
| 26 |
|
| 27 |
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 28 |
>>> wp = pywt.WaveletPacket(data=x, wavelet='db1', mode='symmetric') |
| 29 |
|
| 30 |
The input ``data`` and decomposition coefficients are stored in the |
| 31 |
:attr:`WaveletPacket.data` attribute: |
| 32 |
|
| 33 |
>>> print(wp.data) |
| 34 |
[1, 2, 3, 4, 5, 6, 7, 8] |
| 35 |
|
| 36 |
:class:`Nodes <Node>` are identified by :attr:`paths <~Node.path>`. For the root |
| 37 |
node the path is ``''`` and the decomposition level is ``0``. |
| 38 |
|
| 39 |
>>> print(repr(wp.path)) |
| 40 |
'' |
| 41 |
>>> print(wp.level) |
| 42 |
0 |
| 43 |
|
| 44 |
The ``maxlevel``, if not given as param in the constructor, is automatically |
| 45 |
computed: |
| 46 |
|
| 47 |
>>> print(wp['ad'].maxlevel) |
| 48 |
3 |
| 49 |
|
| 50 |
|
| 51 |
Traversing WP tree: |
| 52 |
------------------- |
| 53 |
|
| 54 |
Accessing subnodes: |
| 55 |
~~~~~~~~~~~~~~~~~~~ |
| 56 |
|
| 57 |
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 58 |
>>> wp = pywt.WaveletPacket(data=x, wavelet='db1', mode='symmetric') |
| 59 |
|
| 60 |
First check what is the maximum level of decomposition: |
| 61 |
|
| 62 |
>>> print(wp.maxlevel) |
| 63 |
3 |
| 64 |
|
| 65 |
and try accessing subnodes of the WP tree: |
| 66 |
|
| 67 |
* 1st level: |
| 68 |
|
| 69 |
>>> print(wp['a'].data) |
| 70 |
[ 2.12132034 4.94974747 7.77817459 10.60660172] |
| 71 |
>>> print(wp['a'].path) |
| 72 |
a |
| 73 |
|
| 74 |
* 2nd level: |
| 75 |
|
| 76 |
>>> print(wp['aa'].data) |
| 77 |
[ 5. 13.] |
| 78 |
>>> print(wp['aa'].path) |
| 79 |
aa |
| 80 |
|
| 81 |
|
| 82 |
* 3rd level: |
| 83 |
|
| 84 |
>>> print(wp['aaa'].data) |
| 85 |
[ 12.72792206] |
| 86 |
>>> print(wp['aaa'].path) |
| 87 |
aaa |
| 88 |
|
| 89 |
|
| 90 |
Ups, we have reached the maximum level of decomposition and got an |
| 91 |
:exc:`IndexError`: |
| 92 |
|
| 93 |
>>> print(wp['aaaa'].data) |
| 94 |
Traceback (most recent call last): |
| 95 |
... |
| 96 |
IndexError: Path length is out of range. |
| 97 |
|
| 98 |
Now try some invalid path: |
| 99 |
|
| 100 |
>>> print(wp['ac']) |
| 101 |
Traceback (most recent call last): |
| 102 |
... |
| 103 |
ValueError: Subnode name must be in ['a', 'd'], not 'c'. |
| 104 |
|
| 105 |
which just yielded a :exc:`ValueError`. |
| 106 |
|
| 107 |
|
| 108 |
Accessing Node's attributes: |
| 109 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 110 |
|
| 111 |
:class:`WaveletPacket` object is a tree data structure, which evaluates to a set |
| 112 |
of :class:`Node` objects. :class:`WaveletPacket` is just a special subclass |
| 113 |
of the :class:`Node` class (which in turn inherits from the :class:`BaseNode`). |
| 114 |
|
| 115 |
Tree nodes can be accessed using the ``obj[x]`` (:meth:`Node.__getitem__`) |
| 116 |
operator. |
| 117 |
Each tree node has a set of attributes: :attr:`~Node.data`, :attr:`~Node.path`, |
| 118 |
:attr:`~Node.node_name`, :attr:`~Node.parent`, :attr:`~Node.level`, |
| 119 |
:attr:`~Node.maxlevel` and :attr:`~Node.mode`. |
| 120 |
|
| 121 |
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 122 |
>>> wp = pywt.WaveletPacket(data=x, wavelet='db1', mode='symmetric') |
| 123 |
|
| 124 |
>>> print(wp['ad'].data) |
| 125 |
[-2. -2.] |
| 126 |
|
| 127 |
>>> print(wp['ad'].path) |
| 128 |
ad |
| 129 |
|
| 130 |
>>> print(wp['ad'].node_name) |
| 131 |
d |
| 132 |
|
| 133 |
>>> print(wp['ad'].parent.path) |
| 134 |
a |
| 135 |
|
| 136 |
>>> print(wp['ad'].level) |
| 137 |
2 |
| 138 |
|
| 139 |
>>> print(wp['ad'].maxlevel) |
| 140 |
3 |
| 141 |
|
| 142 |
>>> print(wp['ad'].mode) |
| 143 |
symmetric |
| 144 |
|
| 145 |
|
| 146 |
Collecting nodes |
| 147 |
~~~~~~~~~~~~~~~~ |
| 148 |
|
| 149 |
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 150 |
>>> wp = pywt.WaveletPacket(data=x, wavelet='db1', mode='symmetric') |
| 151 |
|
| 152 |
|
| 153 |
We can get all nodes on the particular level either in ``natural`` order: |
| 154 |
|
| 155 |
>>> print([node.path for node in wp.get_level(3, 'natural')]) |
| 156 |
['aaa', 'aad', 'ada', 'add', 'daa', 'dad', 'dda', 'ddd'] |
| 157 |
|
| 158 |
or sorted based on the band frequency (``freq``): |
| 159 |
|
| 160 |
>>> print([node.path for node in wp.get_level(3, 'freq')]) |
| 161 |
['aaa', 'aad', 'add', 'ada', 'dda', 'ddd', 'dad', 'daa'] |
| 162 |
|
| 163 |
Note that :meth:`WaveletPacket.get_level` also performs automatic decomposition |
| 164 |
until it reaches the specified ``level``. |
| 165 |
|
| 166 |
|
| 167 |
Reconstructing data from Wavelet Packets: |
| 168 |
----------------------------------------- |
| 169 |
|
| 170 |
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 171 |
>>> wp = pywt.WaveletPacket(data=x, wavelet='db1', mode='symmetric') |
| 172 |
|
| 173 |
|
| 174 |
Now create a new :class:`Wavelet Packet <WaveletPacket>` and set its nodes with |
| 175 |
some data. |
| 176 |
|
| 177 |
>>> new_wp = pywt.WaveletPacket(data=None, wavelet='db1', mode='symmetric') |
| 178 |
|
| 179 |
>>> new_wp['aa'] = wp['aa'].data |
| 180 |
>>> new_wp['ad'] = [-2., -2.] |
| 181 |
|
| 182 |
For convenience, :attr:`Node.data` gets automatically extracted from the |
| 183 |
:class:`Node` object: |
| 184 |
|
| 185 |
>>> new_wp['d'] = wp['d'] |
| 186 |
|
| 187 |
And reconstruct the data from the ``aa``, ``ad`` and ``d`` packets. |
| 188 |
|
| 189 |
>>> print(new_wp.reconstruct(update=False)) |
| 190 |
[ 1. 2. 3. 4. 5. 6. 7. 8.] |
| 191 |
|
| 192 |
If the ``update`` param in the reconstruct method is set to ``False``, the |
| 193 |
node's :attr:`~Node.data` will not be updated. |
| 194 |
|
| 195 |
>>> print(new_wp.data) |
| 196 |
None |
| 197 |
|
| 198 |
Otherwise, the :attr:`~Node.data` attribute will be set to the reconstructed |
| 199 |
value. |
| 200 |
|
| 201 |
>>> print(new_wp.reconstruct(update=True)) |
| 202 |
[ 1. 2. 3. 4. 5. 6. 7. 8.] |
| 203 |
>>> print(new_wp.data) |
| 204 |
[ 1. 2. 3. 4. 5. 6. 7. 8.] |
| 205 |
|
| 206 |
|
| 207 |
>>> print([n.path for n in new_wp.get_leaf_nodes(False)]) |
| 208 |
['aa', 'ad', 'd'] |
| 209 |
|
| 210 |
>>> print([n.path for n in new_wp.get_leaf_nodes(True)]) |
| 211 |
['aaa', 'aad', 'ada', 'add', 'daa', 'dad', 'dda', 'ddd'] |
| 212 |
|
| 213 |
|
| 214 |
Removing nodes from Wavelet Packet tree: |
| 215 |
---------------------------------------- |
| 216 |
|
| 217 |
Let's create a sample data: |
| 218 |
|
| 219 |
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 220 |
>>> wp = pywt.WaveletPacket(data=x, wavelet='db1', mode='symmetric') |
| 221 |
|
| 222 |
First, start with a tree decomposition at level 2. Leaf nodes in the tree are: |
| 223 |
|
| 224 |
>>> dummy = wp.get_level(2) |
| 225 |
>>> for n in wp.get_leaf_nodes(False): |
| 226 |
... print(n.path, format_array(n.data)) |
| 227 |
aa [ 5. 13.] |
| 228 |
ad [-2. -2.] |
| 229 |
da [-1. -1.] |
| 230 |
dd [ 0. 0.] |
| 231 |
|
| 232 |
>>> node = wp['ad'] |
| 233 |
>>> print(node) |
| 234 |
ad: [-2. -2.] |
| 235 |
|
| 236 |
To remove a node from the WP tree, use Python's ``del obj[x]`` |
| 237 |
(:class:`Node.__delitem__`): |
| 238 |
|
| 239 |
>>> del wp['ad'] |
| 240 |
|
| 241 |
The leaf nodes that left in the tree are: |
| 242 |
|
| 243 |
>>> for n in wp.get_leaf_nodes(): |
| 244 |
... print(n.path, format_array(n.data)) |
| 245 |
aa [ 5. 13.] |
| 246 |
da [-1. -1.] |
| 247 |
dd [ 0. 0.] |
| 248 |
|
| 249 |
And the reconstruction is: |
| 250 |
|
| 251 |
>>> print(wp.reconstruct()) |
| 252 |
[ 2. 3. 2. 3. 6. 7. 6. 7.] |
| 253 |
|
| 254 |
Now restore the deleted node value. |
| 255 |
|
| 256 |
>>> wp['ad'].data = node.data |
| 257 |
|
| 258 |
Printing leaf nodes and tree reconstruction confirms the original state of the |
| 259 |
tree: |
| 260 |
|
| 261 |
>>> for n in wp.get_leaf_nodes(False): |
| 262 |
... print(n.path, format_array(n.data)) |
| 263 |
aa [ 5. 13.] |
| 264 |
ad [-2. -2.] |
| 265 |
da [-1. -1.] |
| 266 |
dd [ 0. 0.] |
| 267 |
|
| 268 |
>>> print(wp.reconstruct()) |
| 269 |
[ 1. 2. 3. 4. 5. 6. 7. 8.] |
| 270 |
|
| 271 |
|
| 272 |
Lazy evaluation: |
| 273 |
---------------- |
| 274 |
|
| 275 |
.. note:: This section is for demonstration of pywt internals purposes |
| 276 |
only. Do not rely on the attribute access to nodes as presented in |
| 277 |
this example. |
| 278 |
|
| 279 |
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 280 |
>>> wp = pywt.WaveletPacket(data=x, wavelet='db1', mode='symmetric') |
| 281 |
|
| 282 |
1) At first the wp's attribute ``a`` is None |
| 283 |
|
| 284 |
>>> print(wp.a) |
| 285 |
None |
| 286 |
|
| 287 |
**Remember that you should not rely on the attribute access.** |
| 288 |
|
| 289 |
2) At first attempt to access the node it is computed via decomposition |
| 290 |
of its parent node (the wp object itself). |
| 291 |
|
| 292 |
>>> print(wp['a']) |
| 293 |
a: [ 2.12132034 4.94974747 7.77817459 10.60660172] |
| 294 |
|
| 295 |
3) Now the ``wp.a`` is set to the newly created node: |
| 296 |
|
| 297 |
>>> print(wp.a) |
| 298 |
a: [ 2.12132034 4.94974747 7.77817459 10.60660172] |
| 299 |
|
| 300 |
And so is ``wp.d``: |
| 301 |
|
| 302 |
>>> print(wp.d) |
| 303 |
d: [-0.70710678 -0.70710678 -0.70710678 -0.70710678] |