•

# Python Implementation of Wavelet Reconstruction Python’s wavelet lib pywt hasn’t implemented wavelet reconstruction function yet, which is wrcoef in Matlab.
Here is a Python version.

# Author: Sam Perry
import pywt
import numpy as np

def wrcoef(X, coef_type, coeffs, wavename, level):
N = np.array(X).size
a, ds = coeffs, list(reversed(coeffs[1:]))

if coef_type =='a':
return pywt.upcoef('a', a, wavename, level=level)[:N]
elif coef_type == 'd':
return pywt.upcoef('d', ds[level-1], wavename, level=level)[:N]
else:
raise ValueError("Invalid coefficient type: {}".format(coef_type))


Test:

level = 5
db = "db1"
X = range(0,32)
coeffs = pywt.wavedec(X, db, level=level)

A5 = wrcoef(X, 'a', coeffs, db, level)
D5 = wrcoef(X, 'd', coeffs, db, level)
D4 = wrcoef(X, 'd', coeffs, db, 4)
D3 = wrcoef(X, 'd', coeffs, db, 3)
D2 = wrcoef(X, 'd', coeffs, db, 2)
D1 = wrcoef(X, 'd', coeffs, db, 1)
print(A5 + D5 + D4 + D3 + D2 + D1)


Output:

# amyang @ archlinux in ~/Projects/BCI/wavelet [21:42:52]
\$ python wrcoef.py
[1.88737914e-15 1.00000000e+00 2.00000000e+00 3.00000000e+00
4.00000000e+00 5.00000000e+00 6.00000000e+00 7.00000000e+00
8.00000000e+00 9.00000000e+00 1.00000000e+01 1.10000000e+01
1.20000000e+01 1.30000000e+01 1.40000000e+01 1.50000000e+01
1.60000000e+01 1.70000000e+01 1.80000000e+01 1.90000000e+01
2.00000000e+01 2.10000000e+01 2.20000000e+01 2.30000000e+01
2.40000000e+01 2.50000000e+01 2.60000000e+01 2.70000000e+01
2.80000000e+01 2.90000000e+01 3.00000000e+01 3.10000000e+01]


Works well!