PyWavelets / pywt

PyWavelets - Wavelet Transforms in Python
http://pywavelets.readthedocs.org
MIT License
2.04k stars 470 forks source link

How can I obtained the original serie by reconstructed approximations and details for multi-level decomposition? #582

Closed omerekmekcioglu closed 2 years ago

omerekmekcioglu commented 3 years ago

Hi everyone, I am quite new in PyWavelets,

sample.xlsx

I tried to decompose my sample time series into three sub-bands by using discrete wavelet. However, I obtained a different series than the original series when I sum the reconstructed approximation and details.

When I reconstruct the decomposed series, should not I reach the same series as the original series?

Here my code,

import pywt import pandas as pd import numpy as np data=pd.read_excel(r'C:\Users\ITU\Desktop\sample.xlsx') sample=data["sample"] (cA3, cD3, cD2, cD1) = pywt.wavedec(sample, 'db1', level=3, mode= 'sym') n = len(sample) sample_recons=pywt.upcoef('a', cA3, 'db1', level=3, take=n)+pywt.upcoef('d', cD3, 'db1', level=3, take=n)+pywt.upcoef('d', cD2, 'db1', level=3, take=n)+pywt.upcoef('d', cD1, 'db1', level=3, take=n)

I attached the sample time series.

Thanks

Ömer

grlee77 commented 3 years ago

On Tue, Jan 19, 2021 at 3:56 AM omerekmekcioglu notifications@github.com wrote:

Hi everyone, I am quite new in PyWavelets,

sample.xlsx https://github.com/PyWavelets/pywt/files/5834730/sample.xlsx

I tried to decompose my sample time series into three sub-bands by using discrete wavelet. However, I obtained a different series than the original series when I sum the reconstructed approximation and details.

When I reconstruct the decomposed series, should not I reach the same series as the original series?

You should get back the original result if you use the pywt.waverec function that was designed for that purpose. I think what you are trying with pywt.upcoef is roughly what is being done there, although I suspect there is some subtle shift or difference in which coefficients are discarded from the front or end after upsampling. Those details can be tricky to get right for all boundary modes and I would recommend using the specific function pairs such as wavedec/waverec (or dwt/idwt for single level transforms) rather than those downcoef or upcoef functions.

Here my code,

import pywt import pandas as pd import numpy as np data=pd.read_excel(r'C:\Users\ITU\Desktop\sample.xlsx') sample=data["sample"] (cA3, cD3, cD2, cD1) = pywt.wavedec(sample, 'db1', level=3, mode= 'sym') n = len(sample) sample_recons=pywt.upcoef('a', cA3, 'db1', level=3, take=n)+pywt.upcoef('d', cD3, 'db1', level=3, take=n)+pywt.upcoef('d', cD2, 'db1', level=3, take=n)+pywt.upcoef('d', cD1, 'db1', level=3, take=n)

I attached the sample time series.

Thanks

Ömer

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/PyWavelets/pywt/issues/582, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRZ7PJ2UAJMWIJIQ3OIML3S2VCKXANCNFSM4WIOEGQQ .

omerekmekcioglu commented 3 years ago

I have no doubt that I can reach the original result with the waverec function. But it utilizes the set of coefficients. I would like to get the sub-bands separately as: Approximation3, Detail1, Detail2 and Detail3. I assume that when I sum all of these sub-bands, I should reach the original series. Is there any way to obtain these sub-bands separately by using waverec?

grlee77 commented 3 years ago

On Tue, Jan 19, 2021 at 8:37 AM omerekmekcioglu notifications@github.com wrote:

I have no doubt that I can reach the original result with the waverec function. But it utilizes the set of coefficients. I would like to get the sub-bands separately as: Approximation3, Detail1, Detail2 and Detail3. I assume that when I sum all of these sub-bands, I should reach the original series. Is there any way to obtain these sub-bands separately by using waverec?

I was working on a function to do that a while back. It is in this PR: https://github.com/PyWavelets/pywt/pull/527.

It has been over a year since we made a new PyWavelets release, so I will try to see about planning a new release in the coming month or so, hopefully including that feature. In the meantime, you can copy the functions in this _mra.py file from that PR for use locally: https://github.com/PyWavelets/pywt/blob/06da8bffda54cba61ee5722a35385da7c167ea59/pywt/_mra.py

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/PyWavelets/pywt/issues/582#issuecomment-762843695, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRZ7PMWZLEIKKWTBIOGOV3S2WDKLANCNFSM4WIOEGQQ .

rgommers commented 2 years ago

The question was answered, and we should get a new release out soon (needed to support Python 3.10) which will likely include gh-527. So let's close this.