Debian Codegraph @

doc/source/ref/cwt.rst

raw
1 .. _ref-cwt:
2
3 .. currentmodule:: pywt
4
5 ==================================
6 Continuous Wavelet Transform (CWT)
7 ==================================
8
9 This section describes functions used to perform single continuous wavelet
10 transforms.
11
12 Single level - ``cwt``
13 ----------------------
14
15 .. autofunction:: cwt
16
17
18 Continuous Wavelet Families
19 ---------------------------
20
21 A variety of continuous wavelets have been implemented. A list of the available
22 wavelet names compatible with ``cwt`` can be obtained by:
23
24 .. sourcecode:: python
25
26 wavlist = pywt.wavelist(kind='continuous')
27
28
29 Mexican Hat Wavelet
30 ^^^^^^^^^^^^^^^^^^^
31 The mexican hat wavelet ``"mexh"`` is given by:
32
33 .. math::
34 \psi(t) = \frac{2}{\sqrt{3} \sqrt[4]{\pi}} \exp^{-\frac{t^2}{2}}
35 \left( 1 - t^2 \right)
36
37 where the constant out front is a normalization factor so that the wavelet has
38 unit energy.
39
40
41 Morlet Wavelet
42 ^^^^^^^^^^^^^^
43 The Morlet wavelet ``"morl"`` is given by:
44
45 .. math::
46 \psi(t) = \exp^{-\frac{t^2}{2}} \cos(5t)
47
48
49 Complex Morlet Wavelets
50 ^^^^^^^^^^^^^^^^^^^^^^^
51
52 The complex Morlet wavelet (``"cmorB-C"`` with floating point values B, C) is
53 given by:
54
55 .. math::
56 \psi(t) = \frac{1}{\sqrt{\pi B}} \exp^{-\frac{t^2}{B}}
57 \exp^{\mathrm{j} 2\pi C t}
58
59 where :math:`B` is the bandwidth and :math:`C` is the center frequency.
60
61
62 Gaussian Derivative Wavelets
63 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64 The Gaussian wavelets (``"gausP"`` where P is an integer between 1 and and 8)
65 correspond to the Pth order derivatives of the function:
66
67 .. math::
68 \psi(t) = C \exp^{-t^2}
69
70 where :math:`C` is an order-dependent normalization constant.
71
72 Complex Gaussian Derivative Wavelets
73 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
74 The complex Gaussian wavelets (``"cgauP"`` where P is an integer between 1 and
75 8) correspond to the Pth order derivatives of the function:
76
77 .. math::
78 \psi(t) = C \exp^{-\mathrm{j} t}\exp^{-t^2}
79
80 where :math:`C` is an order-dependent normalization constant.
81
82 Shannon Wavelets
83 ^^^^^^^^^^^^^^^^
84 The Shannon wavelets (``"shanB-C"`` with floating point values B and C)
85 correspond to the following wavelets:
86
87 .. math::
88 \psi(t) = \sqrt{B} \frac{\sin(\pi B t)}{\pi B t} \exp^{\mathrm{j}2 \pi C t}
89
90 where :math:`B` is the bandwidth and :math:`C` is the center frequency.
91
92
93 Frequency B-Spline Wavelets
94 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
95 The frequency B-spline wavelets (``"fpspM-B-C"`` with integer M and floating
96 point B, C) correspond to the following wavelets:
97
98 .. math::
99 \psi(t) = \sqrt{B}
100 \left[\frac{\sin(\pi B \frac{t}{M})}{\pi B \frac{t}{M}}\right]^M
101 \exp^{2\mathrm{j} \pi C t}
102
103 where :math:`M` is the spline order, :math:`B` is the bandwidth and :math:`C` is
104 the center frequency.
105
106
107 Choosing the scales for ``cwt``
108 -------------------------------
109
110 For each of the wavelets described below, the implementation in PyWavelets
111 evaluates the wavelet function for :math:`t` over the range
112 ``[wavelet.lower_bound, wavelet.upper_bound]`` (with default range
113 :math:`[-8, 8]`). ``scale = 1`` corresponds to the case where the extent of the
114 wavelet is ``(wavelet.upper_bound - wavelet.lower_bound + 1)`` samples of the
115 digital signal being analyzed. Larger scales correspond to stretching of the
116 wavelet. For example, at ``scale=10`` the wavelet is stretched by a factor of
117 10, making it sensitive to lower frequencies in the signal.
118
119 To relate a given scale to a specific signal frequency, the sampling period
120 of the signal must be known. :func:`pywt.scale2frequency` can be used to
121 convert a list of scales to their corresponding frequencies. The proper choice
122 of scales depends on the chosen wavelet, so :func:`pywt.scale2frequency` should
123 be used to get an idea of an appropriate range for the signal of interest.
124
125 For the ``cmor``, ``fbsp`` and ``shan`` wavelets, the user can specify a
126 specific a normalized center frequency. A value of 1.0 corresponds to 1/dt
127 where dt is the sampling period. In other words, when analyzing a signal
128 sampled at 100 Hz, a center frequency of 1.0 corresponds to ~100 Hz at
129 ``scale = 1``. This is above the Nyquist rate of 50 Hz, so for this
130 particular wavelet, one would analyze a signal using ``scales >= 2``.
131
132 .. sourcecode:: python
133
134 >>> import numpy as np
135 >>> import pywt
136 >>> dt = 0.01 # 100 Hz sampling
137 >>> frequencies = pywt.scale2frequency('cmor1.5-1.0', [1, 2, 3, 4]) / dt
138 >>> frequencies
139 array([ 100. , 50. , 33.33333333, 25. ])
140
141 The CWT in PyWavelets is applied to discrete data by convolution with samples
142 of the integral of the wavelet. If ``scale`` is too low, this will result in
143 a discrete filter that is inadequately sampled leading to aliasing as shown
144 in the example below. Here the wavelet is ``'cmor1.5-1.0'``. The left column of
145 the figure shows the discrete filters used in the convolution at various
146 scales. The right column are the corresponding Fourier power spectra of each
147 filter.. For scales 1 and 2 it can be seen that aliasing due to violation of
148 the Nyquist limit occurs.
149
150 Converting frequency to scale for ``cwt``
151 -------------------------------
152
153 To convert frequency to scale for use in the wavelet transform the function
154 :func:`pywt.frequency2scale` can be used. This is the complement of the
155 :func:`pywt.scale2frequency` function as seen in the previous section. Note that
156 the input frequency in this function is normalized by 1/dt, or the sampling
157 frequency fs. This function is useful for specifying the transform as a function
158 of frequency directly.
159
160 .. sourcecode:: python
161
162 >>> import numpy as np
163 >>> import pywt
164 >>> dt = 0.01 # 100 Hz sampling
165 >>> fs = 1 / dt
166 >>> frequencies = np.array([100, 50, 33.33333333, 25]) / fs # normalize
167 >>> scale = tfrequency2scale('cmor1.5-1.0', frequencies)
168 >>> scale
169 array([1., 2., 3., 4.])
170
171
172 .. plot:: pyplots/cwt_scaling_demo.py