Denoising IDWT. is this correct way.

321 views
Skip to first unread message

Fon Tran

unread,
Apr 12, 2018, 6:25:43 AM4/12/18
to PyWavelets
Hi I am new to wavelet and pywavelet. I wrote a simple level=2 denoising w DWT and want to check if this correct. origin of the code is not from me, i just extended it to make it level=2

Is this right or is there a better way to do this? thanks!!


import pywt
import matplotlib.pyplot as plt
import numpy as np


ts = [2, 56, 3, 22, 3, 4, 56, 7, 8, 9, 44, 23, 1, 4, 6, 2]

(ca, cd1) = pywt.dwt(ts,'haar')
(ca, cd2) = pywt.dwt(ca,'haar')

cat = pywt.threshold(ca, np.std(ca)/2,mode='soft')
cdt = pywt.threshold(cd2, np.std(cd2)/2,mode='soft')

cat_rec = pywt.idwt(cat, cdt, 'haar')

cat1 = pywt.threshold(cat_rec, np.std(ca)/2,mode='soft')
cdt1 = pywt.threshold(cd1, np.std(cd2)/2,mode='soft')
ts_rec = pywt.idwt(cat1, cdt1, 'haar')

Gregory Lee

unread,
Apr 12, 2018, 10:06:06 AM4/12/18
to pywav...@googlegroups.com
Hi Tran,

I would remove the thresholding on "ca" and "cat_rec".  Typically soft-thresholding is only be applied to the detail coefficients, not the approximation coefficients (soft thresholding shrinks everything toward zero so this will cause some bias in intensity if you shrink the low-pass coefficients).  You could save a bit of typing by using pywt.wavedec with level=2 to do the two-level decomposition (and pywt.waverec to do reconstruction).  There are many different thresholding schemes of varying complexity and performance. PyWavelets only offers some basic thresholding functions. A more detailed denoising built on PyWavelets and BayesShrink thresholding is in scikit-image (see skimage.restoration.denoise_wavelet).  The scikit-image function should also work for 1D data, but you may have to first scale the signal amplitudes to the range [0, 1] or [-1, 1]. The scaling is not something that is required in principle, but scikit-image often enforces that convention on floating point inputs.  I also don't think that the BayesShrink approach will work well in practice on extremely short signals such as in the example above, but if you have a longer signal of >100 samples or so it is probably fine.



--
You received this message because you are subscribed to the Google Groups "PyWavelets" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pywavelets+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fon Tran

unread,
Apr 13, 2018, 6:27:38 AM4/13/18
to PyWavelets
Gregory, Thank you. This is very helpful. 

I wrote a function that combined wavedec and _sigma_est_dwt

as i found denoise_wavelet didn't work for my 1D array.  I can use it to change the threshold type.  Still trying to figure out what is the best threshold type to use for my financial time series.  but this is of a great help. 

Thanks again!
To unsubscribe from this group and stop receiving emails from it, send an email to pywavelets+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages