I am currently decomposing a signal using the discrete wavelet transformation and the 'wavedec' function. Say that I wish to get the detail coefficients for 4 levels. I expect that for each following sub-band, for the coefficients, it gets down sampled by a factor of 2, hence a subsequent sub-band is represented by only half the amount of samples. Say i have a signal length 2560 Then, at level 0 it should be decomposed into 1280 approximation coefficients and 1280 details coefficients. At level 1, those 1280 approximation coefficients should be decomposed into 640 samples. At level 2, we should get 320 samples at level 3 we should get 160 samples. so sample set should be of lengths 1280, 640, 320, 160 But instead of this i am getting 1283, 645, 326, 166 samples. I tries even changing mode to 'periodic' but it didn't worked for me why the length of the sub-band vector are not equal with different Daubechies wavelets? I need the the wavelet coefficients length equal the original signal length. pl help me how do that
# Here is the code I tried implementing
import numpy as np
import matplotlib.pyplot as plt
import pywt
import mne
# an edf file consisting of the signal sampled at 256 Hz
data = mne.io.read_raw_edf("/content/chb01_03.edf")
# selecting 10 seconds of the signal
data_array = np.array(data.get_data(picks="F7-T7", tmin=2987, tmax=2997))
# Flattening the array
data_1d = data_array.flatten()
# Generate a sample signal
signal_length = 2560
t = np.linspace(0, 1, signal_length, endpoint=False)
# Perform db4 wavelet decomposition
wavelet = 'db4'
level = 4
coeffs = pywt.wavedec(data_1d, wavelet, mode='periodic', level=level)
l0,l1,l2,l3,l4=coeffs
for i in range(level + 1):
print(f'Level {i} coefficients: {coeffs[i]}')
print(f'Length of coefficients:', len(coeffs[i]))
You were close, just used a bit wrong name for the mode
argument. It should be periodization
, not "periodic". I changed only this and got desired lengths (640, 320, etc.):
coeffs = pywt.wavedec(data_1d, wavelet, mode='periodization', level=level)
Also, you may want to look at documentation of modes to learn about those modes:
https://pywavelets.readthedocs.io/en/latest/ref/signal-extension-modes.html#ref-modes