Debian Codegraph @

doc/source/regression/wavelet.rst

raw
1 .. _reg-wavelet:
2
3 .. currentmodule:: pywt
4
5 The Wavelet object
6 ==================
7
8 Wavelet families and builtin Wavelets names
9 -------------------------------------------
10
11 :class:`Wavelet` objects are really a handy carriers of a bunch of DWT-specific
12 data like *quadrature mirror filters* and some general properties associated
13 with them.
14
15 At first let's go through the methods of creating a :class:`Wavelet` object.
16 The easiest and the most convenient way is to use builtin named Wavelets.
17
18 These wavelets are organized into groups called wavelet families. The most
19 commonly used families are:
20
21 >>> import pywt
22 >>> pywt.families()
23 ['haar', 'db', 'sym', 'coif', 'bior', 'rbio', 'dmey', 'gaus', 'mexh', 'morl', 'cgau', 'shan', 'fbsp', 'cmor']
24
25 The :func:`wavelist` function with family name passed as an argument is used to
26 obtain the list of wavelet names in each family.
27
28 >>> for family in pywt.families():
29 ... print("%s family: " % family + ', '.join(pywt.wavelist(family)))
30 haar family: haar
31 db family: db1, db2, db3, db4, db5, db6, db7, db8, db9, db10, db11, db12, db13, db14, db15, db16, db17, db18, db19, db20, db21, db22, db23, db24, db25, db26, db27, db28, db29, db30, db31, db32, db33, db34, db35, db36, db37, db38
32 sym family: sym2, sym3, sym4, sym5, sym6, sym7, sym8, sym9, sym10, sym11, sym12, sym13, sym14, sym15, sym16, sym17, sym18, sym19, sym20
33 coif family: coif1, coif2, coif3, coif4, coif5, coif6, coif7, coif8, coif9, coif10, coif11, coif12, coif13, coif14, coif15, coif16, coif17
34 bior family: bior1.1, bior1.3, bior1.5, bior2.2, bior2.4, bior2.6, bior2.8, bior3.1, bior3.3, bior3.5, bior3.7, bior3.9, bior4.4, bior5.5, bior6.8
35 rbio family: rbio1.1, rbio1.3, rbio1.5, rbio2.2, rbio2.4, rbio2.6, rbio2.8, rbio3.1, rbio3.3, rbio3.5, rbio3.7, rbio3.9, rbio4.4, rbio5.5, rbio6.8
36 dmey family: dmey
37 gaus family: gaus1, gaus2, gaus3, gaus4, gaus5, gaus6, gaus7, gaus8
38 mexh family: mexh
39 morl family: morl
40 cgau family: cgau1, cgau2, cgau3, cgau4, cgau5, cgau6, cgau7, cgau8
41 shan family: shan
42 fbsp family: fbsp
43 cmor family: cmor
44
45 To get the full list of builtin wavelets' names just use the :func:`wavelist`
46 with no argument.
47
48
49 Creating Wavelet objects
50 ------------------------
51
52 Now when we know all the names let's finally create a :class:`Wavelet` object:
53
54 >>> w = pywt.Wavelet('db3')
55
56 So.. that's it.
57
58
59 Wavelet properties
60 ------------------
61
62 But what can we do with :class:`Wavelet` objects? Well, they carry some
63 interesting information.
64
65 First, let's try printing a :class:`Wavelet` object. This shows a brief
66 information about its name, its family name and some properties like
67 orthogonality and symmetry.
68
69 >>> print(w)
70 Wavelet db3
71 Family name: Daubechies
72 Short name: db
73 Filters length: 6
74 Orthogonal: True
75 Biorthogonal: True
76 Symmetry: asymmetric
77 DWT: True
78 CWT: False
79
80 But the most important information are the wavelet filters coefficients, which
81 are used in :ref:`Discrete Wavelet Transform <ref-dwt>`. These coefficients can
82 be obtained via the :attr:`~Wavelet.dec_lo`, :attr:`Wavelet.dec_hi`,
83 :attr:`~Wavelet.rec_lo` and :attr:`~Wavelet.rec_hi` attributes, which
84 corresponds to lowpass and highpass decomposition filters and lowpass and
85 highpass reconstruction filters respectively:
86
87 >>> def print_array(arr):
88 ... print("[%s]" % ", ".join(["%.14f" % x for x in arr]))
89
90
91
92 Another way to get the filters data is to use the :attr:`~Wavelet.filter_bank`
93 attribute, which returns all four filters in a tuple:
94
95 >>> w.filter_bank == (w.dec_lo, w.dec_hi, w.rec_lo, w.rec_hi)
96 True
97
98
99 Other Wavelet's properties are:
100
101 Wavelet :attr:`~Wavelet.name`, :attr:`~Wavelet.short_family_name` and :attr:`~Wavelet.family_name`:
102
103 >>> print(w.name)
104 db3
105 >>> print(w.short_family_name)
106 db
107 >>> print(w.family_name)
108 Daubechies
109
110 - Decomposition (:attr:`~Wavelet.dec_len`) and reconstruction
111 (:attr:`~.Wavelet.rec_len`) filter lengths:
112
113 >>> int(w.dec_len) # int() is for normalizing longs and ints for doctest
114 6
115 >>> int(w.rec_len)
116 6
117
118 - Orthogonality (:attr:`~Wavelet.orthogonal`) and biorthogonality (:attr:`~Wavelet.biorthogonal`):
119
120 >>> w.orthogonal
121 True
122 >>> w.biorthogonal
123 True
124
125 - Symmetry (:attr:`~Wavelet.symmetry`):
126
127 >>> print(w.symmetry)
128 asymmetric
129
130 - Number of vanishing moments for the scaling function ``phi``
131 (:attr:`~Wavelet.vanishing_moments_phi`) and the wavelet function ``psi``
132 (:attr:`~Wavelet.vanishing_moments_psi`) associated with the filters:
133
134 >>> w.vanishing_moments_phi
135 0
136 >>> w.vanishing_moments_psi
137 3
138
139 Now when we know a bit about the builtin Wavelets, let's see how to create
140 :ref:`custom Wavelets <custom-wavelets>` objects. These can be done in two ways:
141
142 1) Passing the filter bank object that implements the ``filter_bank``
143 attribute. The attribute must return four filters coefficients.
144
145 >>> class MyHaarFilterBank(object):
146 ... @property
147 ... def filter_bank(self):
148 ... from math import sqrt
149 ... return ([sqrt(2)/2, sqrt(2)/2], [-sqrt(2)/2, sqrt(2)/2],
150 ... [sqrt(2)/2, sqrt(2)/2], [sqrt(2)/2, -sqrt(2)/2])
151
152
153 >>> my_wavelet = pywt.Wavelet('My Haar Wavelet', filter_bank=MyHaarFilterBank())
154
155
156 2) Passing the filters coefficients directly as the ``filter_bank``
157 parameter.
158
159 >>> from math import sqrt
160 >>> my_filter_bank = ([sqrt(2)/2, sqrt(2)/2], [-sqrt(2)/2, sqrt(2)/2],
161 ... [sqrt(2)/2, sqrt(2)/2], [sqrt(2)/2, -sqrt(2)/2])
162 >>> my_wavelet = pywt.Wavelet('My Haar Wavelet', filter_bank=my_filter_bank)
163
164
165 Note that such custom wavelets **will not** have all the properties set
166 to correct values:
167
168 >>> print(my_wavelet)
169 Wavelet My Haar Wavelet
170 Family name:
171 Short name:
172 Filters length: 2
173 Orthogonal: False
174 Biorthogonal: False
175 Symmetry: unknown
176 DWT: True
177 CWT: False
178
179 You can however set a couple of them on your own:
180
181 >>> my_wavelet.orthogonal = True
182 >>> my_wavelet.biorthogonal = True
183
184 >>> print(my_wavelet)
185 Wavelet My Haar Wavelet
186 Family name:
187 Short name:
188 Filters length: 2
189 Orthogonal: True
190 Biorthogonal: True
191 Symmetry: unknown
192 DWT: True
193 CWT: False
194
195 And now... the ``wavefun``!
196 ---------------------------
197
198 We all know that the fun with wavelets is in wavelet functions.
199 Now what would be this package without a tool to compute wavelet
200 and scaling functions approximations?
201
202 This is the purpose of the :meth:`~Wavelet.wavefun` method, which is used to
203 approximate scaling function (``phi``) and wavelet function (``psi``) at the
204 given level of refinement, based on the filters coefficients.
205
206 The number of returned values varies depending on the wavelet's
207 orthogonality property. For orthogonal wavelets the result is tuple
208 with scaling function, wavelet function and xgrid coordinates.
209
210 >>> w = pywt.Wavelet('sym3')
211 >>> w.orthogonal
212 True
213 >>> (phi, psi, x) = w.wavefun(level=5)
214
215 For biorthogonal (non-orthogonal) wavelets different scaling and wavelet
216 functions are used for decomposition and reconstruction, and thus five
217 elements are returned: decomposition scaling and wavelet functions
218 approximations, reconstruction scaling and wavelet functions approximations,
219 and the xgrid.
220
221 >>> w = pywt.Wavelet('bior1.3')
222 >>> w.orthogonal
223 False
224 >>> (phi_d, psi_d, phi_r, psi_r, x) = w.wavefun(level=5)
225
226 .. seealso::
227 You can find live examples of :meth:`~Wavelet.wavefun` usage and
228 images of all the built-in wavelets on the
229 `Wavelet Properties Browser <http://wavelets.pybytes.com>`_ page.
230 However, **this website is no longer actively maintained** and does not
231 include every wavelet present in PyWavelets. The precision of the wavelet
232 coefficients at that site is also lower than those included in
233 PyWavelets.