Tip
This page can also be run or downloaded as a Jupyter notebook.
Run in a new tab using JupyterLite[1]:
Download:
Jupyter notebook:
multilevel.ipynbMyST markdown:
multilevel.md
Multilevel DWT, IDWT and SWT#
Multilevel DWT decomposition#
Here is an example of multilevel DWT decomposition using the db1 wavelet into
approximation and detail coefficients:
import pywt
x = [3, 7, 1, 1, -2, 5, 4, 6]
db1 = pywt.Wavelet('db1')
cA3, cD3, cD2, cD1 = pywt.wavedec(x, db1)
cA3
array([8.83883476])
cD3
array([-0.35355339])
cD2
array([ 4. , -3.5])
cD1
array([-2.82842712, 0. , -4.94974747, -1.41421356])
The number of levels in the decomposition can be determined as well:
pywt.dwt_max_level(len(x), db1)
3
or decompose to a specific level:
cA2, cD2, cD1 = pywt.wavedec(x, db1, mode='constant', level=2)
Multilevel IDWT reconstruction#
coeffs = pywt.wavedec(x, db1)
pywt.waverec(coeffs, db1)
array([ 3., 7., 1., 1., -2., 5., 4., 6.])
Multilevel SWT decomposition#
x = [3, 7, 1, 3, -2, 6, 4, 6]
(cA2, cD2), (cA1, cD1) = pywt.swt(x, db1, level=2)
cA1
array([7.07106781, 5.65685425, 2.82842712, 0.70710678, 2.82842712,
7.07106781, 7.07106781, 6.36396103])
cD1
array([-2.82842712, 4.24264069, -1.41421356, 3.53553391, -5.65685425,
1.41421356, -1.41421356, 2.12132034])
cA2
array([ 7. , 4.5, 4. , 5.5, 7. , 9.5, 10. , 8.5])
cD2
array([ 3. , 3.5, 0. , -4.5, -3. , 0.5, 0. , 0.5])
[(cA2, cD2)] = pywt.swt(cA1, db1, level=1, start_level=1)
cA2
array([ 7. , 4.5, 4. , 5.5, 7. , 9.5, 10. , 8.5])
cD2
array([ 3. , 3.5, 0. , -4.5, -3. , 0.5, 0. , 0.5])
coeffs = pywt.swt(x, db1)
len(coeffs)
3
pywt.swt_max_level(len(x))
3