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)