PyWavelets library provides an easy method wavedec
to decompose a signal into multiple levels.
pywt.wavedec(S,'wavelet', mode='symmetric', level=None, axis=-1)
Parameters:
Returns:
[cAn, cDn, cDn-1, …,cD2, cD1] : Ordered list of coefficients arrays where n denotes the level of decomposition.
Let's see a very simple example in order to understand the method better. Suppose we have a simple signal, S, which contains values from 1 t0 20.
S = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
# import PyWavelets library
import pywt
cA2, cD2, cD1 = pywt.wavedec(S, 'db2','symmetric',level=2)
Or we use a list as follows:
[cA2, cD2, cD1] = pywt.wavedec(S, 'db2','symmetric',level=2)
The third way is to use a variable and we unpack the coefficients when we need.
coeffs = pywt.wavedec(S, 'db2','symmetric',level=2)
cA2, cD2, cD1 = coeffs
cA2 = coeffs[0]
After decomposition, we can easily display the approximation coefficient at the last level and detail coefficients at different levels.
print('cA2 = ',cA2)
print('cD2 = ',cD2)
print('cD1 = ',cD1)
cA2 = [ 2.6919873 2.90761345 9.80384758 17.80384758 25.80384758 34.18541744 39.58253175] cD2 = [-3.32531755e-01 -2.95753175e-01 -2.22044605e-16 -6.66133815e-16 -4.44089210e-16 1.42403811e+00 3.08012702e-01] cD1 = [-6.12372436e-01 1.66533454e-16 3.33066907e-16 2.22044605e-16 2.22044605e-16 4.44089210e-16 8.88178420e-16 4.44089210e-16 0.00000000e+00 4.44089210e-16 6.12372436e-01]
print(len(S))
print(len(cA2))
print(len(cD2))
print(len(cD1))
20 7 7 11
How about before wavelet decomposition? PyWavelets library also provides a convenient method to calculate the lengths of the coefficients at the different levels.
pywt.dwt_coeff_len(data_len, filter_len, mode='symmetric')
Parameters:
Returns:
# first level
filter_l = w.dec_len
pywt.dwt_coeff_len(len(S), filter_l, 'symmetric')
11
Or just pass the defined wavelet instead of the wavelet filter length.
w = pywt.Wavelet('db2')
pywt.dwt_coeff_len(len(S), w, 'symmetric')
11
We get the same result, 11 as above. To calculate coefficient length other than the first level, use the previous coefficient length as the data length. In this example, we use the coefficient length of first level, 11 as the data length to calculate the coefficient length at the second level.
# second level
pywt.dwt_coeff_len(11, w, 'symmetric')
7
In this section, we will investigate how to calculate maximum decomposition level. Maximum decomposition level of a signal mainly depends on the length of the signal ($data\_len$) and the length of the wavelet filter, and the mathematic equation is as follows.
$\lfloor$ $\rfloor$: round down (or take the floor)
The first method is to calculate the maximum decomposition level directly based on the equation.
import math
l = len(S)
w = pywt.Wavelet('db2')
filter_l = w.dec_len
level = math.log(l/(filter_l-1), 2)
print(level)
max_level = math.floor(level)
print(max_level)
2.736965594166206 2
PyWavelets library provide a built-in function dwt_max_level()
to easily calculate the maximum decomposition level.
pywt.dwt_max_level(data_len, filter_len)
Parameters:
Returns:
let's see an example for the simple signal that we created.
data_len = len(S)
w = pywt.Wavelet('db2')
filter_len = w.dec_len
pywt.dwt_max_level(data_len, filter_len)
2
To decompose a signal to the max level, we just calculate the maximum level and then pass the maximum level to the argument level
. In this example, let's extend the signal to 40.
S2 = [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]
max_l = pywt.dwt_max_level(len(S2), filter_len)
coeffs = pywt.wavedec(S, 'db2','symmetric', level=max_l)
print('Maximum level:', max_l)
print('Decomposition Coefficients:\n', coeffs)
Maximum level: 3 Decomposition Coefficients: [array([ 3.88328031, 3.62588099, 20.98795921, 44.80878567, 56.5515161 ]), array([-0.13204351, -1.39761227, -0.18428409, 2.99282302, 2.13981933]), array([-3.32531755e-01, -2.95753175e-01, -2.22044605e-16, -6.66133815e-16, -4.44089210e-16, 1.42403811e+00, 3.08012702e-01]), array([-6.12372436e-01, 1.66533454e-16, 3.33066907e-16, 2.22044605e-16, 2.22044605e-16, 4.44089210e-16, 8.88178420e-16, 4.44089210e-16, 0.00000000e+00, 4.44089210e-16, 6.12372436e-01])]