PyWavelets / pywt

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

SWT on last samples of sequence #173

Closed overjoy closed 8 years ago

overjoy commented 8 years ago

Hello everybody and thanks for this great library. I'm newbie in wavelets and have a question regarding SWT. I don't know if it's a bug or not, but why do I get some weird spikes at the end of sequence? Here is the plot and corresponding code: swt

import pywt
import numpy as np
import matplotlib.pyplot as plt

# generate some random signal 
x = np.arange(64)
s = np.sin(x/(2*np.pi)) + np.cos(x/(5*np.pi)) + 0.2*np.random.randn(len(x))

# full SWT decomposition
(A2,D2),(A1,D1) = pywt.swt(s, 'haar', 2)
plt.plot(A2, 'b')

# SWT of first 32 samples only
(A2,D2),(A1,D1) = pywt.swt(s[:32], 'haar', 2)
plt.plot(A2, 'ro')
plt.show()

I'm using approximation and detail sequences as inputs of neural network and I think that spiky behaviour will ruin my plans because it makes last samples very misleading. If it is the expected result of SWT, does anybody know how to overcome this issue? Or maybe guide me to some good introduction to SWT.. Everything I found in net was like rocket-science-ego-maniac-mathematical stuff. Can't believe such a thing can not be explained without tons of integrals.

kwohlfahrt commented 8 years ago

I think this is due to the signal extension (to generate the last few elements of the decomposition, the wavelet has to be multiplied with values past the end of the signal). These are generated from a number of available signal extension modes (see pywt.Modes) for the DWT. For SWT, the periodization mode is used, so the samples at the very end are influenced by those at the beginning.

grlee77 commented 8 years ago

Agreed. There is detailed discussion of these border effects here http://www.mathworks.com/help/wavelet/ug/dealing-with-border-distortion.html (The border modes in pywt match those available in Matlab)

kwohlfahrt commented 8 years ago

Looks like the question has been answered. Feel free to re-open if it is not clear.