PyWavelets / pywt

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

Size mismatch between odd/even signal shapes #636

Closed ranabanik closed 3 months ago

ranabanik commented 2 years ago

I am using the package to perform denoising/smoothing as following:

def wavelet_denoising(x, wavelet='db8', level=2):
    coeff = pywt.wavedec(x, wavelet, mode="per")
    num_ = 1.0
    den_ = 0.6745*3
    sigma = (num_/den_) * madev(coeff[-level])
    uthresh = sigma * np.sqrt(2 * np.log(len(x)))
    coeff[1:] = (pywt.threshold(i, value=uthresh, mode='hard') for i in coeff[1:])
    return pywt.waverec(coeff, wavelet, mode='per')

filtered = wavelet_denoising(signal)

If the `signal`(1D vector) has an odd number of samples, the `filtered` output seems to be even with +1. 
When `signal` is even the output size is identical. 

I have a signal with size (1, 36755), how do I process it to get a similar number of output/filtered signal(I get 36756 for the signal)

AdriaJ commented 1 year ago

Hello, This issue seems to be outdated, but I just encountered a similar problem in my code today. So if a workaround has been found in the meantime, I would be happy to hear it.

I am working with images and I apply 2D wavelet decomposition and reconstruction operations. When my input image has an odd size on one axes, the output image has an additional line of white pixels so that it has an even size on that axes. I specifically use mode='zero', but this happens for any level of decomposition or wavelet family considered.

I was wondering if this was known and planned to be fixed or if it is considered as a characteristics of pywt and need to be handled manually by the user ?

I can provide a dummy code example if need.

Thanks a lot!

blakdayz commented 8 months ago

Its the nature of the db family, check to see if you can evenly divide in the incoming sample by 2, you you mod2 and get anything, just add one zero with the pywt.pad()

rgommers commented 3 months ago

This question was answered (thanks @blakdayz), so I'll close this issue.