File: dwt-discrete-wavelet-transform.rst

package info (click to toggle)
pywavelets 0.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,584 kB
  • sloc: ansic: 3,357; python: 1,843; makefile: 143
file content (180 lines) | stat: -rw-r--r-- 5,177 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
.. _ref-dwt:

.. currentmodule:: pywt
.. include:: ../substitutions.rst

================================
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``
--------------------

.. function:: dwt(data, wavelet[, mode='sym'])

  The :func:`dwt` function is used to perform single level, one dimensional
  Discrete Wavelet Transform.

  ::

    (cA, cD) = dwt(data, wavelet, mode='sym')

  :param data: |data|

  :param wavelet: |wavelet_arg|

  :param mode: |mode|

  The transform coefficients are returned as two arrays containing
  approximation (*cA*) and detail (*cD*) coefficients respectively. Length
  of returned arrays depends on the selected signal extension *mode* - see
  the :ref:`signal extension modes <ref-modes>` section for the list of
  available options and the :func:`dwt_coeff_len` function for information on
  getting the expected result length:

  * for all :ref:`modes <ref-modes>` except :ref:`periodization <MODES.per>`::

      len(cA) == len(cD) == floor((len(data) + wavelet.dec_len - 1) / 2)

  * for :ref:`periodization <MODES.per>` mode (``"per"``)::

      len(cA) == len(cD) == ceil(len(data) / 2)

  **Example:**

  .. sourcecode:: python

    >>> import pywt
    >>> (cA, cD) = pywt.dwt([1,2,3,4,5,6], 'db1')
    >>> print cA
    [ 2.12132034  4.94974747  7.77817459]
    >>> print cD
    [-0.70710678 -0.70710678 -0.70710678]


Multilevel decomposition using ``wavedec``
------------------------------------------

.. function:: wavedec(data, wavelet, mode='sym', level=None)

  .. compound::

    The :func:`wavedec` function performs 1D multilevel Discrete Wavelet
    Transform decomposition of given signal and returns ordered list of
    coefficients arrays in the form:

      ::

      [cA_n, cD_n, cD_n-1, ..., cD2, cD1],

    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.

  :param data: |data|

  :param wavelet: |wavelet_arg|

  :param mode: |mode|

  :param level: Number of decomposition steps to performe. If the level is
                ``None``, then the full decomposition up to the level computed
                with :func:`dwt_max_level` function for the given data and
                wavelet lengths is performed.

  **Example:**

  .. sourcecode:: python

    >>> import pywt
    >>> coeffs = pywt.wavedec([1,2,3,4,5,6,7,8], 'db1', level=2)
    >>> cA2, cD2, cD1 = coeffs
    >>> print cD1
    [-0.70710678 -0.70710678 -0.70710678 -0.70710678]
    >>> print cD2
    [-2. -2.]
    >>> print cA2
    [  5.  13.]


Partial Discrete Wavelet Transform data decomposition ``downcoef``
------------------------------------------------------------------

.. function:: downcoef(part, data, wavelet[, mode='sym'[, level=1]])

   Similar to :func:`~pywt.dwt`, but computes only one set of coefficients.
   Useful when you need only approximation or only details at the given level.

   :param part: decomposition type. For ``a`` computes approximation
                coefficients, for ``d`` - details coefficients.

   :param data: |data|

   :param wavelet: |wavelet_arg|

   :param mode: |mode|

   :param level: Number of decomposition steps to perform.



Maximum decomposition level - ``dwt_max_level``
-----------------------------------------------

.. function:: dwt_max_level(data_len, filter_len)

  The :func:`~pywt.dwt_max_level` function can be used to compute the maximum
  *useful* level of decomposition for the given *input data length* and *wavelet
  filter length*.

  The returned value equals to::

    floor( log(data_len/(filter_len-1)) / log(2) )

  Although the maximum decomposition level can be quite high for long signals,
  usually smaller values are chosen depending on the application.

  The *filter_len* can be either an ``int`` or :class:`Wavelet` object for
  convenience.

  **Example:**

  .. sourcecode:: python

    >>> import pywt
    >>> w = pywt.Wavelet('sym5')
    >>> print pywt.dwt_max_level(data_len=1000, filter_len=w.dec_len)
    6
    >>> print pywt.dwt_max_level(1000, w)
    6

.. _`dwt_coeff_len`:


Result coefficients length - ``dwt_coeff_len``
----------------------------------------------

.. function:: dwt_coeff_len(data_len, filter_len, mode)

Based on the given *input data length*, Wavelet *decomposition filter length*
and :ref:`signal extension mode <MODES>`, the :func:`dwt_coeff_len` function
calculates length of resulting coefficients arrays that would be created while
performing :func:`dwt` transform.

For :ref:`periodization <MODES.per>` mode this equals::

  ceil(data_len / 2)

which is the lowest possible length guaranteeing perfect reconstruction.

For other :ref:`modes <ref-modes>`::

  floor((data_len + filter_len - 1) / 2)

The *filter_len* can be either an *int* or :class:`Wavelet` object for
convenience.