Debian Codegraph @

doc/source/ref/signal-extension-modes.rst

raw
1 .. _ref-modes:
2
3 .. currentmodule:: pywt
4
5
6 ======================
7 Signal extension modes
8 ======================
9
10 .. _Modes:
11
12 Because the most common and practical way of representing digital signals
13 in computer science is with finite arrays of values, some extrapolation
14 of the input data has to be performed in order to extend the signal before
15 computing the :ref:`Discrete Wavelet Transform <ref-dwt>` using the cascading
16 filter banks algorithm.
17
18 Depending on the extrapolation method, significant artifacts at the signal's
19 borders can be introduced during that process, which in turn may lead to
20 inaccurate computations of the :ref:`DWT <ref-dwt>` at the signal's ends.
21
22 PyWavelets provides several methods of signal extrapolation that can be used to
23 minimize this negative effect:
24
25 .. _`Modes.zero`:
26
27 * ``zero`` - **zero-padding** - signal is extended by adding zero samples::
28
29 ... 0 0 | x1 x2 ... xn | 0 0 ...
30
31 .. _`Modes.constant`:
32
33 * ``constant`` - **constant-padding** - border values are replicated::
34
35 ... x1 x1 | x1 x2 ... xn | xn xn ...
36
37 .. _`Modes.symmetric`:
38
39 * ``symmetric`` - **symmetric-padding** - signal is extended by *mirroring*
40 samples. This mode is also known as half-sample symmetric.::
41
42 ... x2 x1 | x1 x2 ... xn | xn xn-1 ...
43
44 .. _`Modes.reflect`:
45
46 * ``reflect`` - **reflect-padding** - signal is extended by *reflecting*
47 samples. This mode is also known as whole-sample symmetric.::
48
49 ... x3 x2 | x1 x2 ... xn | xn-1 xn-2 ...
50
51 .. _`Modes.periodic`:
52 .. _`periodic-padding`:
53
54 * ``periodic`` - **periodic-padding** - signal is treated as a periodic one::
55
56 ... xn-1 xn | x1 x2 ... xn | x1 x2 ...
57
58 .. _`Modes.smooth`:
59
60 * ``smooth`` - **smooth-padding** - signal is extended according to the first
61 derivatives calculated on the edges (straight line)
62
63 .. _`Modes.antisymmetric`:
64
65 * ``antisymmetric`` - **anti-symmetric padding** - signal is extended by
66 *mirroring* and negating samples. This mode is also known as half-sample
67 anti-symmetric::
68
69 ... -x2 -x1 | x1 x2 ... xn | -xn -xn-1 ...
70
71 .. _`Modes.antireflect`:
72
73 * ``antireflect`` - **anti-symmetric-reflect padding** - signal is extended by
74 *reflecting* anti-symmetrically about the edge samples. This mode is also
75 known as whole-sample anti-symmetric::
76
77 ... (2*x1 - x3) (2*x1 - x2) | x1 x2 ... xn | (2*xn - xn-1) (2*xn - xn-2) ...
78
79 :ref:`DWT <ref-dwt>` performed for these extension modes is slightly redundant, but ensures
80 perfect reconstruction. To receive the smallest possible number of coefficients,
81 computations can be performed with the `periodization`_ mode:
82
83 .. _`periodization`:
84 .. _`Modes.periodization`:
85
86 * ``periodization`` - **periodization** - is like `periodic-padding`_ but gives the
87 smallest possible number of decomposition coefficients. :ref:`IDWT <ref-idwt>` must be
88 performed with the same mode.
89
90 **Example:**
91
92 .. sourcecode:: python
93
94 >>> import pywt
95 >>> print(pywt.Modes.modes)
96 ['zero', 'constant', 'symmetric', 'periodic', 'smooth', 'periodization', 'reflect', 'antisymmetric', 'antireflect']
97
98 The following figure illustrates how a short signal (red) gets extended (black)
99 outside of its original extent. Note that periodization first extends the
100 signal to an even length prior to using periodic boundary conditions.
101
102 .. plot:: pyplots/plot_boundary_modes.py
103
104 Notice that you can use any of the following ways of passing wavelet and mode
105 parameters:
106
107 .. sourcecode:: python
108
109 >>> import pywt
110 >>> (a, d) = pywt.dwt([1,2,3,4,5,6], 'db2', 'smooth')
111 >>> (a, d) = pywt.dwt([1,2,3,4,5,6], pywt.Wavelet('db2'), pywt.Modes.smooth)
112
113 .. note::
114 Extending data in context of PyWavelets does not mean reallocation of the
115 data in the computer's physical memory and copying values, but rather
116 computing the extra values only when they are needed.
117 This feature saves extra memory and CPU resources and helps to avoid page
118 swapping when handling relatively big data arrays on computers with low
119 physical memory.
120
121 Naming Conventions
122 ------------------
123 The correspondence between PyWavelets edge modes and the extension modes
124 available in Matlab's dwtmode and numpy's pad are tabulated here for reference.
125
126 ================== ============= ===========================
127 **PyWavelets** **Matlab** **numpy.pad**
128 ================== ============= ===========================
129 symmetric sym, symh symmetric
130 reflect symw reflect
131 smooth spd, sp1 N/A
132 constant sp0 edge
133 zero zpd constant, cval=0
134 periodic ppd wrap
135 periodization per N/A
136 antisymmetric asym, asymh N/A
137 antireflect asymw reflect, reflect_type='odd'
138 ================== ============= ===========================
139
140 Padding using PyWavelets Signal Extension Modes - ``pad``
141 ---------------------------------------------------------
142
143 .. autofunction:: pad
144
145 Pywavelets provides a function, :func:`pad`, that operate like
146 :func:`numpy.pad`, but supporting the PyWavelets signal extension modes
147 discussed above. For efficiency, the DWT routines in PyWavelets do not
148 expclitly create padded signals using this function. It can be used to manually
149 prepad signals to reduce boundary effects in functions such as :func:`cwt` and
150 :func:`swt` that do not currently support all of these signal extension modes.