DWPT Fully Decomposed Signal Issue?

99 views
Skip to first unread message

WGG ARA

unread,
May 11, 2022, 12:05:04 PM5/11/22
to PyWavelets
Hello,

I am a beginner to PyWavelets and Wavelet transforms in general, so apologies if this is an ignorant question.

I am following a methodology by Sanchez et al 2009 "Wavelet-based cepstrum calculation," which takes a signal whose length is a power of 2 and decomposes it into it's mth-level Discrete Wavelet Packet Transform (where m = log2(signal_length) ). Because of the downsampling by 2 of each filterbank, the mth level decomposition should boil down to a single value for each node. The authors also note that this for this problem it is only appropiate to use symlet and coiflet wavelets.

The issue I'm not understanding is that when I use PyWT to compute the mth level DWPT for a (for example) signal of length 2048, the 11th-level of the decomposition tree does not leave a single value, but rather an array of values whose length depends on the Wavelet being used. For example, a `sym2` wavelet creates 11th level nodes whose data is 3 values long, whereas a `sym12` wavelet creates data that are 23 values long in their "final" decomposition.

I've pasted a minimum usage example below to demonstrate my issue. Thanks!

----------------------------------------------------------------------------------------------------------------------------------------------

import numpy as np

def make_harmonic_wave(f0, sampling_frequency, frame_size, n_harmonics=10):

    waveform = np.zeros((frame_size,), dtype=float)
    for f in [f0 * i for i in range(1, n_harmonics + 1)]:
        waveform += f0 / f * make_sine_wave(f, sampling_frequency, frame_size, phase=f)
    return waveform

import pywt
from math import log2

sample_freq = 16000 # Hz
frame_size = 2048
time_vector = np.arange(frame_size) / sample_freq
f0 = 440
# Create a harmonic wave
signal = make_harmonic_wave(f0, sample_freq, frame_size, n_harmonics=20)

m = log2(frame_size)

# Sym2 Wavelet
wtype='sym2'
dwpt = pywt.WaveletPacket(data=signal, wavelet=wtype, mode='symmetric', maxlevel=m)
# Array of mth level node points -- each node.data should be a single value
s = np.array( [ node.data for node in dwpt.get_level(m, 'natural') ] )
print(s.shape)

# Sym12 Wavelet
wtype='sym2'
dwpt = pywt.WaveletPacket(data=signal, wavelet=wtype, mode='symmetric', maxlevel=m)
# Array of mth level node points -- each node.data should be a single value
s = np.array( [ node.data for node in dwpt.get_level(m, 'natural') ] )
print(s.shape)


Deepu

unread,
May 22, 2023, 2:24:43 AM5/22/23
to PyWavelets
Hello,

In the code you provided, there seems to be an error in the second part where you use the `sym12` wavelet. It appears that you mistakenly set `wtype='sym2'` instead of `wtype='sym12'`. This is why you are getting the same results as with the `sym2` wavelet.

To clarify, the number of values in the mth level of the wavelet decomposition does depend on the wavelet used. Different wavelets have different filter lengths, which affect the decomposition structure. In general, the mth level of the decomposition should have a length of 2^m, which corresponds to a single value for each node in the mth level.

Here's the corrected code snippet:

```python
# Sym2 Wavelet
wtype = 'sym2'

dwpt = pywt.WaveletPacket(data=signal, wavelet=wtype, mode='symmetric', maxlevel=m)
# Array of mth level node points -- each node.data should be a single value
s = np.array([node.data for node in dwpt.get_level(m, 'natural')])
print(s.shape)

# Sym12 Wavelet
wtype = 'sym12'

dwpt = pywt.WaveletPacket(data=signal, wavelet=wtype, mode='symmetric', maxlevel=m)
# Array of mth level node points -- each node.data should be a single value
s = np.array([node.data for node in dwpt.get_level(m, 'natural')])
print(s.shape)
```

With this correction, you should observe that the shape of `s` for the `sym2` wavelet is (2048,), and for the `sym12` wavelet is (2048,). These shapes indicate that you have a single value for each node in the 11th level of the decomposition, as expected.

I hope this clarifies the issue. Let me know if you have further questions!
Reply all
Reply to author
Forward
0 new messages