I am new in the area of using wavelet decomposition. And I am trying to decompose and reconstruct (with very few coefficients) a 1D data in python (using pywt
). From this documentation I wrote the code below which reconstructs the data with 512 coefficients (i.e. size of cA
or cD
) but I think their should be a way choosing (limiting) the number of coefficients that I consider to produce reasonable data reconstruction.
%matplotlib inline
import pylab as plt
import pywt
# Data
data = ll[5].x0
n = len(data)
w = 'db1'
(cA, cD) = pywt.dwt(data, w, 'sp1') # Decomposition
# Perfect Reconstruction of data
perfect_reconstruction = pywt.upcoef('a',cA[:],w,take=n) + pywt.upcoef('d',cD[:],w,take=n)
reconstructed = pywt.upcoef('a',cA[:],w,take=n) # Approximate Reconstruction of data
x = np.arange(1.008,1.008+1024*0.001,0.001)
plt.figure(figsize=(20,8))
plt.subplot2grid((2,1),(0,0))
plt.title('Perfect Reconstruction of data - %s with rms error of 1.39 x e$^{-15}$'%w, fontsize=20)
plt.plot(x,data,'-',label='Data')
plt.plot(x,perfect_reconstruction,'-r',label='Reconstructed data')
plt.legend(loc='best',fontsize='x-large')
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.subplot2grid((2,1),(1,0))
plt.title('Approximate Reconstruction of data - %s with rms error of 1.30 x e$^{-3}$'%w, fontsize=20)
plt.plot(x,data,'-',label='Data')
plt.plot(x,reconstructed,'-r',label='Reconstructed data')
plt.legend(loc='best',fontsize='x-large')
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.show()
Please, if anyone can help me with any suggestions on what I can do to achieve proper decomposition and reconstruction with fewer coefficients I will highly appreciate it and any information on how to write the maths behind this because my goal is the to find a mathematical expression that best describes the data with fewer coefficients after deomposition.