PyWavelets / pywt

PyWavelets - Wavelet Transforms in Python
http://pywavelets.readthedocs.org
MIT License
2.06k stars 482 forks source link

Should threshold accept multiple 1-D wavelets? #461

Closed RoMa03 closed 3 years ago

RoMa03 commented 5 years ago
test = np.arange(16).reshape(2,8)
wav = pywt.wavedec(test, 'haar', mode='periodization', level=None, axis=-1)
#wav = pywt.threshold(wav, 3, mode='hard')
recon = pywt.waverec(wav, 'haar', mode='periodization', axis=-1)

The above code runs as I expect, constructing a 1-D wavelet decomposition for each of 2 series and then reconstructing the original series from them. However if the '#' is removed, the result is "ValueError: could not broadcast input array from shape (2,1) into shape (2)". I am not very familiar with the package but this seems strange?

grlee77 commented 5 years ago

The threshold functions do operate on arrays in any number of dimensions. There error in your case is because you were passing in a list of arrays as returned by wavedec instead of a single array. You would need to call threshold on each individual array. Assuming you intended to apply this same hard threshold to all levels of coefficients, you would need to replace your commented line with:

wav = [pywt.threshold(arr, 3, 'hard') for arr in wav]

A more detailed example with automatic determination of threshold levels and potentially variable thresholds across coefficients can be seen in the _wavelet_threshold method used for denoising in scikit-image.

RoMa03 commented 5 years ago

Thank you for your clear explanation and suggestions.

I do want to apply a fixed threshold to a large set of equal-length vectors. It seems a little counter-intuitive to have to loop through each vector, but perhaps my application is unusual.

grlee77 commented 5 years ago

I suspect your application is not uncommon.

In principle, the thresholding functions could accept lists of coefficients, however each of the different transforms (wavedecn, swtn, fswavedecn, etc) have a different coefficient format. Handling all of these within the thresholding functions would add quite a bit of complexity.

rgommers commented 3 years ago

The question is answered, so I'll close this issue.