PyWavelets / pywt

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

scaling function filter and wavelet function filter for reconstruction #624

Closed LeErnst closed 2 years ago

LeErnst commented 2 years ago

Hello guys,

first of all nice work. Currently i am trying to use your wavelet filters for my own pytorch implementation and i am wondering about your scaling function filter and wavelet function filter for reconstruction from your biorthogonal b-spline wavelets. Is it possible that you already flipped them for the calculations when reconstructing a decomposed signal?

The second question is: What is the logic behind the zero padding for the filter coefficients?

Greetings, LeErnst

LeErnst commented 2 years ago

I've made some numerical experiments with your filters to find out the logic and to me it is very unclear what is happening: This results gives the right coefficients in the decomposition for every wavelet 'biorx,y'.

# define a function, with which we simulate our signal
def f(x):
    result = np.zeros(x.shape)
    idx = (0 <= x)*(x < 0.5)
    result[idx] = x[idx]**2
    idx = (0.5 <= x)*(x <= 1.)
    result[idx] = (1-x[idx])**2
    return result

# get the signal for the dwt
j = 6
N = 2**j
h      = 1/N
x      = np.linspace(0,1,N)
signal = f(x)

wave = 'bior6.8'

w = pywt.Wavelet(wave)
(dec_lo, dec_hi, rec_lo, rec_hi) = w.filter_bank
print(dec_lo)
print(dec_hi)
L = len(dec_hi)

sig_pat = np.zeros(L)           # some sort of zero padding
T = 6                                    # this gives you the n-th value in the decomposition for T = 2*n, for a natural number n
sig_pat[-T:] = signal[:T]

print(np.sum(sig_pat*dec_lo[::-1]))
print(np.sum(sig_pat*dec_hi[::-1]))

# multiresolution analysis
maxl = pywt.dwt_max_level(len(signal), wave)

AD = pywt.wavedec(signal, wave, mode='zero', level=maxl)
print(signal)
[print(a) for a in AD]

Mention that i've used dec_lo[::-1] instead of dec_lo and zero padding of length filter_lenght -2. The latter makes sense to me, but the former doesn't. Or do you use a different zero padding for each wavelet?