0

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.

TBA
  • 1,921
  • 4
  • 13
  • 26
Grace Wang
  • 67
  • 5

1 Answers1

0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sheldon
  • 4,084
  • 3
  • 20
  • 41
  • Thanks for your reply. I confirm now I would like to use CWT with haar wavelet. However, pywt does not provide CWT with haar, and scipy does not support self-defined wavelet. Thus, I still do not know how to do CWT with haar wavelet now. Do you have any comments? Thanks again. – Grace Wang Mar 10 '22 at 05:52
  • Indeed, in pywt the Haar wavelet is meant to be used with the DWT (you can confirm this by trying `print(my_wavelet)`). I am not sure what alternative to recommend. This SO answer suggests using Pyscellania: https://stackoverflow.com/questions/27169856/continuous-haar-wavelet-for-python – Sheldon Mar 10 '22 at 18:05