| 1 |
.. _ref-wavelets: |
| 2 |
|
| 3 |
.. currentmodule:: pywt |
| 4 |
|
| 5 |
======== |
| 6 |
Wavelets |
| 7 |
======== |
| 8 |
|
| 9 |
Wavelet ``families()`` |
| 10 |
---------------------- |
| 11 |
|
| 12 |
.. autofunction:: families |
| 13 |
|
| 14 |
|
| 15 |
Built-in wavelets - ``wavelist()`` |
| 16 |
---------------------------------- |
| 17 |
|
| 18 |
.. autofunction:: wavelist |
| 19 |
|
| 20 |
Custom discrete wavelets are also supported through the |
| 21 |
:class:`Wavelet` object constructor as described below. |
| 22 |
|
| 23 |
|
| 24 |
``Wavelet`` object |
| 25 |
------------------ |
| 26 |
|
| 27 |
.. class:: Wavelet(name[, filter_bank=None]) |
| 28 |
|
| 29 |
Describes properties of a discrete wavelet identified by the specified |
| 30 |
wavelet ``name``. For continuous wavelets see :class:`pywt.ContinuousWavelet` |
| 31 |
instead. In order to use a built-in wavelet the ``name`` parameter must be a |
| 32 |
valid wavelet name from the :func:`pywt.wavelist` list. |
| 33 |
|
| 34 |
Custom Wavelet objects can be created by passing a user-defined filters set |
| 35 |
with the ``filter_bank`` parameter. |
| 36 |
|
| 37 |
:param name: Wavelet name |
| 38 |
:param filter_bank: Use a user supplied filter bank instead of a built-in :class:`Wavelet`. |
| 39 |
|
| 40 |
The filter bank object can be a list of four filters coefficients or an object |
| 41 |
with :attr:`~Wavelet.filter_bank` attribute, which returns a list of such |
| 42 |
filters in the following order:: |
| 43 |
|
| 44 |
[dec_lo, dec_hi, rec_lo, rec_hi] |
| 45 |
|
| 46 |
Wavelet objects can also be used as a base filter banks. See section on |
| 47 |
:ref:`using custom wavelets <custom-wavelets>` for more information. |
| 48 |
|
| 49 |
**Example:** |
| 50 |
|
| 51 |
.. sourcecode:: python |
| 52 |
|
| 53 |
>>> import pywt |
| 54 |
>>> wavelet = pywt.Wavelet('db1') |
| 55 |
|
| 56 |
.. attribute:: name |
| 57 |
|
| 58 |
Wavelet name. |
| 59 |
|
| 60 |
.. attribute:: short_name |
| 61 |
|
| 62 |
Short wavelet name. |
| 63 |
|
| 64 |
.. attribute:: dec_lo |
| 65 |
|
| 66 |
Decomposition filter values. |
| 67 |
|
| 68 |
.. attribute:: dec_hi |
| 69 |
|
| 70 |
Decomposition filter values. |
| 71 |
|
| 72 |
.. attribute:: rec_lo |
| 73 |
|
| 74 |
Reconstruction filter values. |
| 75 |
|
| 76 |
.. attribute:: rec_hi |
| 77 |
|
| 78 |
Reconstruction filter values. |
| 79 |
|
| 80 |
.. attribute:: dec_len |
| 81 |
|
| 82 |
Decomposition filter length. |
| 83 |
|
| 84 |
.. attribute:: rec_len |
| 85 |
|
| 86 |
Reconstruction filter length. |
| 87 |
|
| 88 |
.. attribute:: filter_bank |
| 89 |
|
| 90 |
Returns filters list for the current wavelet in the following order:: |
| 91 |
|
| 92 |
[dec_lo, dec_hi, rec_lo, rec_hi] |
| 93 |
|
| 94 |
.. attribute:: inverse_filter_bank |
| 95 |
|
| 96 |
Returns list of reverse wavelet filters coefficients. The mapping from |
| 97 |
the ``filter_coeffs`` list is as follows:: |
| 98 |
|
| 99 |
[rec_lo[::-1], rec_hi[::-1], dec_lo[::-1], dec_hi[::-1]] |
| 100 |
|
| 101 |
.. attribute:: short_family_name |
| 102 |
|
| 103 |
Wavelet short family name |
| 104 |
|
| 105 |
.. attribute:: family_name |
| 106 |
|
| 107 |
Wavelet family name |
| 108 |
|
| 109 |
.. attribute:: orthogonal |
| 110 |
|
| 111 |
Set if wavelet is orthogonal |
| 112 |
|
| 113 |
.. attribute:: biorthogonal |
| 114 |
|
| 115 |
Set if wavelet is biorthogonal |
| 116 |
|
| 117 |
.. attribute:: symmetry |
| 118 |
|
| 119 |
``asymmetric``, ``near symmetric``, ``symmetric`` |
| 120 |
|
| 121 |
.. attribute:: vanishing_moments_psi |
| 122 |
|
| 123 |
Number of vanishing moments for the wavelet function |
| 124 |
|
| 125 |
.. attribute:: vanishing_moments_phi |
| 126 |
|
| 127 |
Number of vanishing moments for the scaling function |
| 128 |
|
| 129 |
**Example:** |
| 130 |
|
| 131 |
.. sourcecode:: python |
| 132 |
|
| 133 |
>>> def format_array(arr): |
| 134 |
... return "[%s]" % ", ".join(["%.14f" % x for x in arr]) |
| 135 |
|
| 136 |
>>> import pywt |
| 137 |
>>> wavelet = pywt.Wavelet('db1') |
| 138 |
>>> print(wavelet) |
| 139 |
Wavelet db1 |
| 140 |
Family name: Daubechies |
| 141 |
Short name: db |
| 142 |
Filters length: 2 |
| 143 |
Orthogonal: True |
| 144 |
Biorthogonal: True |
| 145 |
Symmetry: asymmetric |
| 146 |
DWT: True |
| 147 |
CWT: False |
| 148 |
>>> print(format_array(wavelet.dec_lo), format_array(wavelet.dec_hi)) |
| 149 |
[0.70710678118655, 0.70710678118655] [-0.70710678118655, 0.70710678118655] |
| 150 |
>>> print(format_array(wavelet.rec_lo), format_array(wavelet.rec_hi)) |
| 151 |
[0.70710678118655, 0.70710678118655] [0.70710678118655, -0.70710678118655] |
| 152 |
|
| 153 |
|
| 154 |
Approximating wavelet and scaling functions - ``Wavelet.wavefun()`` |
| 155 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 156 |
|
| 157 |
.. method:: Wavelet.wavefun(level) |
| 158 |
|
| 159 |
.. versionchanged:: 0.2 |
| 160 |
The time (space) localisation of approximation function points was |
| 161 |
added. |
| 162 |
|
| 163 |
The :meth:`~Wavelet.wavefun` method can be used to calculate approximations of |
| 164 |
scaling function (``phi``) and wavelet function (``psi``) at the given level |
| 165 |
of refinement. |
| 166 |
|
| 167 |
For :attr:`orthogonal <Wavelet.orthogonal>` wavelets returns approximations of |
| 168 |
scaling function and wavelet function with corresponding x-grid coordinates:: |
| 169 |
|
| 170 |
[phi, psi, x] = wavelet.wavefun(level) |
| 171 |
|
| 172 |
**Example:** |
| 173 |
|
| 174 |
.. sourcecode:: python |
| 175 |
|
| 176 |
>>> import pywt |
| 177 |
>>> wavelet = pywt.Wavelet('db2') |
| 178 |
>>> phi, psi, x = wavelet.wavefun(level=5) |
| 179 |
|
| 180 |
For other (:attr:`biorthogonal <Wavelet.biorthogonal>` but not |
| 181 |
:attr:`orthogonal <Wavelet.orthogonal>`) wavelets returns approximations of |
| 182 |
scaling and wavelet function both for decomposition and reconstruction and |
| 183 |
corresponding x-grid coordinates:: |
| 184 |
|
| 185 |
[phi_d, psi_d, phi_r, psi_r, x] = wavelet.wavefun(level) |
| 186 |
|
| 187 |
**Example:** |
| 188 |
|
| 189 |
.. sourcecode:: python |
| 190 |
|
| 191 |
>>> import pywt |
| 192 |
>>> wavelet = pywt.Wavelet('bior3.5') |
| 193 |
>>> phi_d, psi_d, phi_r, psi_r, x = wavelet.wavefun(level=5) |
| 194 |
|
| 195 |
.. See also plots of Daubechies and Symlets wavelet families generated using |
| 196 |
the :meth:`~Wavelet.wavefun` function: |
| 197 |
|
| 198 |
- `db.png`_ |
| 199 |
- `sym.png`_ |
| 200 |
|
| 201 |
.. seealso:: |
| 202 |
You can find live examples of :meth:`~Wavelet.wavefun` usage and |
| 203 |
images of all the built-in wavelets on the |
| 204 |
`Wavelet Properties Browser <http://wavelets.pybytes.com>`_ page. |
| 205 |
However, **this website is no longer actively maintained** and does not |
| 206 |
include every wavelet present in PyWavelets. The precision of the wavelet |
| 207 |
coefficients at that site is also lower than those included in |
| 208 |
PyWavelets. |
| 209 |
|
| 210 |
.. _using-custom-wavelets: |
| 211 |
.. _custom-wavelets: |
| 212 |
|
| 213 |
Using custom wavelets |
| 214 |
--------------------- |
| 215 |
|
| 216 |
PyWavelets comes with a :func:`long list <pywt.wavelist>` of the most popular |
| 217 |
wavelets built-in and ready to use. If you need to use a specific wavelet which |
| 218 |
is not included in the list it is very easy to do so. Just pass a list of four |
| 219 |
filters or an object with a :attr:`~Wavelet.filter_bank` attribute as a |
| 220 |
``filter_bank`` argument to the :class:`Wavelet` constructor. |
| 221 |
|
| 222 |
.. compound:: |
| 223 |
|
| 224 |
The filters list, either in a form of a simple Python list or returned via |
| 225 |
the :attr:`~Wavelet.filter_bank` attribute, must be in the following order: |
| 226 |
|
| 227 |
* lowpass decomposition filter |
| 228 |
* highpass decomposition filter |
| 229 |
* lowpass reconstruction filter |
| 230 |
* highpass reconstruction filter |
| 231 |
|
| 232 |
just as for the :attr:`~Wavelet.filter_bank` attribute of the |
| 233 |
:class:`Wavelet` class. |
| 234 |
|
| 235 |
The Wavelet object created in this way is a standard :class:`Wavelet` instance. |
| 236 |
|
| 237 |
The following example illustrates the way of creating custom Wavelet objects |
| 238 |
from plain Python lists of filter coefficients and a *filter bank-like* object. |
| 239 |
|
| 240 |
**Example:** |
| 241 |
|
| 242 |
.. sourcecode:: python |
| 243 |
|
| 244 |
>>> import pywt, math |
| 245 |
>>> c = math.sqrt(2)/2 |
| 246 |
>>> dec_lo, dec_hi, rec_lo, rec_hi = [c, c], [-c, c], [c, c], [c, -c] |
| 247 |
>>> filter_bank = [dec_lo, dec_hi, rec_lo, rec_hi] |
| 248 |
>>> myWavelet = pywt.Wavelet(name="myHaarWavelet", filter_bank=filter_bank) |
| 249 |
>>> |
| 250 |
>>> class HaarFilterBank(object): |
| 251 |
... @property |
| 252 |
... def filter_bank(self): |
| 253 |
... c = math.sqrt(2)/2 |
| 254 |
... dec_lo, dec_hi, rec_lo, rec_hi = [c, c], [-c, c], [c, c], [c, -c] |
| 255 |
... return [dec_lo, dec_hi, rec_lo, rec_hi] |
| 256 |
>>> filter_bank = HaarFilterBank() |
| 257 |
>>> myOtherWavelet = pywt.Wavelet(name="myHaarWavelet", filter_bank=filter_bank) |
| 258 |
|
| 259 |
|
| 260 |
``ContinuousWavelet`` object |
| 261 |
---------------------------- |
| 262 |
|
| 263 |
.. class:: ContinuousWavelet(name, dtype=np.float64) |
| 264 |
|
| 265 |
Describes properties of a continuous wavelet identified by the specified wavelet ``name``. |
| 266 |
In order to use a built-in wavelet the ``name`` parameter must be a valid |
| 267 |
wavelet name from the :func:`pywt.wavelist` list. |
| 268 |
|
| 269 |
:param name: Wavelet name |
| 270 |
:param dtype: numpy.dtype to use for the wavelet. Can be numpy.float64 or numpy.float32. |
| 271 |
|
| 272 |
**Example:** |
| 273 |
|
| 274 |
.. sourcecode:: python |
| 275 |
|
| 276 |
>>> import pywt |
| 277 |
>>> wavelet = pywt.ContinuousWavelet('gaus1') |
| 278 |
|
| 279 |
.. attribute:: name |
| 280 |
|
| 281 |
Continuous Wavelet name. |
| 282 |
|
| 283 |
.. attribute:: short_family_name |
| 284 |
|
| 285 |
Wavelet short family name |
| 286 |
|
| 287 |
.. attribute:: family_name |
| 288 |
|
| 289 |
Wavelet family name |
| 290 |
|
| 291 |
.. attribute:: orthogonal |
| 292 |
|
| 293 |
Set if wavelet is orthogonal |
| 294 |
|
| 295 |
.. attribute:: biorthogonal |
| 296 |
|
| 297 |
Set if wavelet is biorthogonal |
| 298 |
|
| 299 |
.. attribute:: complex_cwt |
| 300 |
|
| 301 |
Returns if wavelet is complex |
| 302 |
|
| 303 |
.. attribute:: lower_bound |
| 304 |
|
| 305 |
Set the lower bound of the effective support |
| 306 |
|
| 307 |
.. attribute:: upper_bound |
| 308 |
|
| 309 |
Set the upper bound of the effective support |
| 310 |
|
| 311 |
.. attribute:: center_frequency |
| 312 |
|
| 313 |
Set the center frequency for the shan, fbsp and cmor wavelets |
| 314 |
|
| 315 |
.. attribute:: bandwidth_frequency |
| 316 |
|
| 317 |
Set the bandwidth frequency for the shan, fbsp and cmor wavelets |
| 318 |
|
| 319 |
.. attribute:: fbsp_order |
| 320 |
|
| 321 |
Set the order for the fbsp wavelet |
| 322 |
|
| 323 |
.. attribute:: symmetry |
| 324 |
|
| 325 |
``asymmetric``, ``near symmetric``, ``symmetric``, ``anti-symmetric`` |
| 326 |
|
| 327 |
**Example:** |
| 328 |
|
| 329 |
.. sourcecode:: python |
| 330 |
|
| 331 |
>>> import pywt |
| 332 |
>>> wavelet = pywt.ContinuousWavelet('gaus1') |
| 333 |
>>> print(wavelet) |
| 334 |
ContinuousWavelet gaus1 |
| 335 |
Family name: Gaussian |
| 336 |
Short name: gaus |
| 337 |
Symmetry: anti-symmetric |
| 338 |
DWT: False |
| 339 |
CWT: True |
| 340 |
Complex CWT: False |
| 341 |
|
| 342 |
Approximating wavelet functions - ``ContinuousWavelet.wavefun()`` |
| 343 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 344 |
|
| 345 |
.. method:: ContinuousWavelet.wavefun(level, length = None) |
| 346 |
|
| 347 |
|
| 348 |
The :meth:`~ContinuousWavelet.wavefun` method can be used to calculate approximations of |
| 349 |
scaling function (``psi``) with grid (``x``). The vector length is set by ``length``. |
| 350 |
The vector length can also be defined by ``2**level`` if ``length`` is not set. |
| 351 |
|
| 352 |
For :attr:`complex_cwt <ContinuousWavelet.complex_cwt>` wavelets returns a complex approximations of |
| 353 |
wavelet function with corresponding x-grid coordinates:: |
| 354 |
|
| 355 |
[psi, x] = wavelet.wavefun(level) |
| 356 |
|
| 357 |
**Example:** |
| 358 |
|
| 359 |
.. sourcecode:: python |
| 360 |
|
| 361 |
>>> import pywt |
| 362 |
>>> wavelet = pywt.ContinuousWavelet('gaus1') |
| 363 |
>>> psi, x = wavelet.wavefun(level=5) |
| 364 |
|
| 365 |
Approximating wavelet functions - ``ContinuousWavelet.wavefun()`` |
| 366 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 367 |
|
| 368 |
.. method:: DiscreteContinuousWavelet(name, [filter_bank = None]) |
| 369 |
|
| 370 |
|
| 371 |
The :meth:`~DiscreteContinuousWavelet` returns a |
| 372 |
Wavelet or a ContinuousWavelet object depending on the given name. |
| 373 |
|
| 374 |
**Example:** |
| 375 |
|
| 376 |
.. sourcecode:: python |
| 377 |
|
| 378 |
>>> import pywt |
| 379 |
>>> wavelet = pywt.DiscreteContinuousWavelet('db1') |
| 380 |
>>> print(wavelet) |
| 381 |
Wavelet db1 |
| 382 |
Family name: Daubechies |
| 383 |
Short name: db |
| 384 |
Filters length: 2 |
| 385 |
Orthogonal: True |
| 386 |
Biorthogonal: True |
| 387 |
Symmetry: asymmetric |
| 388 |
DWT: True |
| 389 |
CWT: False |
| 390 |
>>> wavelet = pywt.DiscreteContinuousWavelet('gaus1') |
| 391 |
>>> print(wavelet) |
| 392 |
ContinuousWavelet gaus1 |
| 393 |
Family name: Gaussian |
| 394 |
Short name: gaus |
| 395 |
Symmetry: anti-symmetric |
| 396 |
DWT: False |
| 397 |
CWT: True |
| 398 |
Complex CWT: False |