Search code examples
pythonwavelethaar-waveletpywaveletspywt

how to change dilation and translation coefficient in pywt


I am new to pywt and wavelet analysis. I am now facing several problems, and I hope someone can help me with them.

enter image description here

First, I would like to change dilation (D) and translation (x) in my_wavelet, but I do not know how to do it exactly. I will be very grateful if anyone provides an example(s).

Second, can I add zeros at the edge of array s? Since the output of value cA and cD are in size 7 while input s is size 13. Or is there any method to avoid the reduction of size?

Third, is C_P,D in the formula above the same as cA output from pywt.dwt function? I still don't understand why in the formula there's only one output but pywt.dwt gave two?

s = [1,2,3,7,8,9,5,1,1,0,1]
my_filter_bank = ( [1,1], [-1,1], [1,1], [-1,1] )
my_wavelet = pywt.Wavelet('haar', filter_bank = my_filter_bank)
cA, cD = pywt.dwt(s, my_wavelet)

Thanks in advance.


Solution

  • I think that you get mixed-up between the Continuous Wavelet Transform and the Discrete Wavelet Transform. The formula that you provide is that of the CWT, yet what you try computing in Python is the DWT.

    First, I would like to change dilation (D) and translation (x) in my_wavelet, but I do not know how to do it exactly. I will be very grateful if anyone provides an example(s).

    You don't have to, at least not if you're planning on applying a DWT to your signal s. DWT will decompose your signal using a single level (using dwt) or multiple levels (using wavedec). Think of each level as being related to the scale of the wavelet, i.e. how dilated it is. So the dilation and the translation of your wavelet are already "taken care of".

    Second, can I add zeros at the edge of array s? Since the output of value cA and cD are in size 7 while input s is size 13. Or is there any method to avoid the reduction of size?

    The size of cA and cD is defined here:

    Length of coefficients arrays depends on the selected mode. For all modes except periodization:

    len(cA) == len(cD) == floor((len(data) + wavelet.dec_len - 1) / 2)
    

    For periodization mode (“per”):

    len(cA) == len(cD) == ceil(len(data) / 2)
    

    The fact that cA and cD have a different size than your input signal s should not bother you. Of course, you can add zeros at the edge of your signal, but this would only result in you computing the coefficients from the zero-padded signal.

    Third, is C_P,D in the formula above the same as cA output from pywt.dwt function? I still don't understand why in the formula there's only one output but pywt.dwt gave two?

    No, they are not the same thing. C_P,D in your formula is the wavelet coefficient for a given scale and position, whereas cA and cD correspond to the detailed and approximate coefficients.

    One last tip: I personally find it very hard to get an understanding of the wavelet transform with a small array such as yours. I would suggest analyzing example data such as scipy's ecg:

    import pywt
    import matplotlib.pyplot as plt
    from scipy.misc import electrocardiogram
    
    s = electrocardiogram()
    plt.plot(s)
    plt.title('INPUT SIGNAL')
    

    enter image description here

    my_wavelet = pywt.Wavelet('db2')
    cA, cD = pywt.dwt(s, my_wavelet) 
    plt.plot(cD)
    plt.title('DETAILED COEFF.')
    

    enter image description here

    plt.plot(cA)
    plt.title('APPROXIMATION COEFF.')
    

    enter image description here