Wavelet Transform¶
Extend data¶
This function should be used in dwt()
and uwt()
to extend
the length of data to power of two. cwt()
use it as internal function.
-
mlpy.
extend
(x, method='reflection', length='powerof2')¶ Extend the 1D numpy array x beyond its original length.
Parameters: - x : 1d ndarray
data
- method : string (‘reflection’, ‘periodic’, ‘zeros’)
indicates which extension method to use
- length : string (‘powerof2’, ‘double’)
indicates how to determinate the length of the extended data
Returns: - xext : 1d ndarray
extended version of x
Example:
>>> import numpy as np >>> import mlpy >>> a = np.array([1,2,3,4,5]) >>> mlpy.extend(a, method='periodic', length='powerof2') array([1, 2, 3, 4, 5, 1, 2, 3])
New in version 2.0.6.
Discrete Wavelet Transform¶
Discrete Wavelet Transform based on the GSL DWT [Gsldwt].
-
mlpy.
dwt
(x, wf, k)¶ Discrete Wavelet Tranform
Parameters: - x : 1d ndarray float (the length is restricted to powers of two)
data
- wf : string (‘d’: daubechies, ‘h’: haar, ‘b’: bspline)
wavelet type
- k : integer
member of the wavelet family
- daubechies : k = 4, 6, ..., 20 with k even
- haar : the only valid choice of k is k = 2
- bspline : k = 103, 105, 202, 204, 206, 208, 301, 303, 305 307, 309
Returns: - X : 1d ndarray float
discrete wavelet transformed data
Example:
>>> import numpy as np >>> import mlpy >>> x = np.array([1,2,3,4,3,2,1,0]) >>> mlpy.dwt(x=x, wf='d', k=6) array([ 5.65685425, 3.41458985, 0.29185347, -0.29185347, -0.28310081, -0.07045258, 0.28310081, 0.07045258])
-
mlpy.
idwt
(X, wf, k)¶ Inverse Discrete Wavelet Tranform
Parameters: - X : 1d ndarray float
discrete wavelet transformed data
- wf : string (‘d’: daubechies, ‘h’: haar, ‘b’: bspline)
wavelet type
- k : integer
member of the wavelet family
- daubechies : k = 4, 6, ..., 20 with k even
- haar : the only valid choice of k is k = 2
- bspline : k = 103, 105, 202, 204, 206, 208, 301, 303, 305 307, 309
Returns: - x : 1d ndarray float
data
Example:
>>> import numpy as np >>> import mlpy >>> X = np.array([ 5.65685425, 3.41458985, 0.29185347, -0.29185347, -0.28310081, ... -0.07045258, 0.28310081, 0.07045258]) >>> mlpy.idwt(X=X, wf='d', k=6) array([ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00, 3.00000000e+00, 2.00000000e+00, 1.00000000e+00, -3.53954610e-09])
Undecimated Wavelet Transform¶
Undecimated Wavelet Transform based on the “wavelets” R package.
-
mlpy.
uwt
(x, wf, k, levels=0)¶ Undecimated Wavelet Tranform
Parameters: - x : 1d ndarray float (the length is restricted to powers of two)
data
- wf : string (‘d’: daubechies, ‘h’: haar, ‘b’: bspline)
wavelet type
- k : integer
member of the wavelet family
- daubechies : k = 4, 6, ..., 20 with k even
- haar : the only valid choice of k is k = 2
- bspline : k = 103, 105, 202, 204, 206, 208, 301, 303, 305 307, 309
- levels : integer
level of the decomposition (J). If levels = 0 this is the value J such that the length of X is at least as great as the length of the level J wavelet filter, but less than the length of the level J+1 wavelet filter. Thus, j <= log_2((n-1)/(l-1)+1), where n is the length of x.
Returns: - X : 2d ndarray float (2J x len(x))
undecimated wavelet transformed data
Data:
[[wavelet coefficients W_1] [wavelet coefficients W_2] : [wavelet coefficients W_J] [scaling coefficients V_1] [scaling coefficients V_2] : [scaling coefficients V_J]]
Example:
>>> import numpy as np >>> import mlpy >>> x = np.array([1,2,3,4,3,2,1,0]) >>> mlpy.uwt(x=x, wf='d', k=6, levels=0) array([[ 0.0498175 , 0.22046721, 0.2001825 , -0.47046721, -0.0498175 , -0.22046721, -0.2001825 , 0.47046721], [ 0.28786838, 0.8994525 , 2.16140162, 3.23241633, 3.71213162, 3.1005475 , 1.83859838, 0.76758367]])
-
mlpy.
iuwt
(X, wf, k)¶ Inverse Undecimated Wavelet Tranform
Parameters: - X : 2d ndarray float
undecimated wavelet transformed data
- wf : string (‘d’: daubechies, ‘h’: haar, ‘b’: bspline)
wavelet type
- k : integer
member of the wavelet family
- daubechies : k = 4, 6, ..., 20 with k even
- haar : the only valid choice of k is k = 2
- bspline : k = 103, 105, 202, 204, 206, 208, 301, 303, 305 307, 309
Returns: - x : 1d ndarray float
data
Example:
>>> import numpy as np >>> import mlpy >>> X = np.array([[ 0.0498175 , 0.22046721, 0.2001825 , -0.47046721, -0.0498175, ... -0.22046721, -0.2001825 , 0.47046721], ... [ 0.28786838, 0.8994525 , 2.16140162, 3.23241633, 3.71213162, ... 3.1005475 , 1.83859838, 0.76758367]]) >>> mlpy.iuwt(X=X, wf='d', k=6) array([ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00, 3.00000000e+00, 2.00000000e+00, 1.00000000e+00, 2.29246158e-09])
New in version 2.0.2.
Continuous Wavelet Transform¶
Continuous Wavelet Transform based on [Torrence98].
-
mlpy.
cwt
(x, dt, dj, wf='dog', p=2, extmethod='none', extlength='powerof2')¶ Continuous Wavelet Tranform.
Parameters: - x : 1d ndarray float
data
- dt : float
time step
- dj : float
scale resolution (smaller values of dj give finer resolution)
- wf : string (‘morlet’, ‘paul’, ‘dog’)
wavelet function
- p : float
wavelet function parameter
- extmethod : string (‘none’, ‘reflection’, ‘periodic’, ‘zeros’)
indicates which extension method to use
- extlength : string (‘powerof2’, ‘double’)
indicates how to determinate the length of the extended data
Returns: - (X, scales) : (2d ndarray complex, 1d ndarray float)
transformed data, scales
Example:
>>> import numpy as np >>> import mlpy >>> x = np.array([1,2,3,4,3,2,1,0]) >>> mlpy.cwt(x=x, dt=1, dj=2, wf='dog', p=2) (array([[ -4.66713159e-02 -6.66133815e-16j, -3.05311332e-16 +2.77555756e-16j, 4.66713159e-02 +1.38777878e-16j, 6.94959463e-01 -8.60422844e-16j, 4.66713159e-02 +6.66133815e-16j, 3.05311332e-16 -2.77555756e-16j, -4.66713159e-02 -1.38777878e-16j, -6.94959463e-01 +8.60422844e-16j], [ -2.66685280e+00 +2.44249065e-15j, -1.77635684e-15 -4.44089210e-16j, 2.66685280e+00 -3.10862447e-15j, 3.77202823e+00 -8.88178420e-16j, 2.66685280e+00 -2.44249065e-15j, 1.77635684e-15 +4.44089210e-16j, -2.66685280e+00 +3.10862447e-15j, -3.77202823e+00 +8.88178420e-16j]]), array([ 0.50329212, 2.01316848]))
-
mlpy.
icwt
(X, dt, dj, wf='dog', p=2, recf=True)¶ Inverse Continuous Wavelet Tranform.
Parameters: - X : 2d ndarray complex
transformed data
- dt : float
time step
- dj : float
scale resolution (smaller values of dj give finer resolution)
- wf : string (‘morlet’, ‘paul’, ‘dog’)
wavelet function
- p : float
wavelet function parameter
- morlet : 2, 4, 6
- paul : 2, 4, 6
- dog : 2, 6, 10
- recf : bool
use the reconstruction factor (C_{\delta} \Psi_0(0))
Returns: - x : 1d ndarray float
data
Example:
>>> import numpy as np >>> import mlpy >>> X = np.array([[ -4.66713159e-02 -6.66133815e-16j, ... -3.05311332e-16 +2.77555756e-16j, ... 4.66713159e-02 +1.38777878e-16j, ... 6.94959463e-01 -8.60422844e-16j, ... 4.66713159e-02 +6.66133815e-16j, ... 3.05311332e-16 -2.77555756e-16j, ... -4.66713159e-02 -1.38777878e-16j, ... -6.94959463e-01 +8.60422844e-16j], ... [ -2.66685280e+00 +2.44249065e-15j, ... -1.77635684e-15 -4.44089210e-16j, ... 2.66685280e+00 -3.10862447e-15j, ... 3.77202823e+00 -8.88178420e-16j, ... 2.66685280e+00 -2.44249065e-15j, ... 1.77635684e-15 +4.44089210e-16j, ... -2.66685280e+00 +3.10862447e-15j, ... -3.77202823e+00 +8.88178420e-16j]]) >>> mlpy.icwt(X=X, dt=1, dj=2, wf='dog', p=2) array([ -1.24078928e+00, -1.07301771e-15, 1.24078928e+00, 2.32044753e+00, 1.24078928e+00, 1.07301771e-15, -1.24078928e+00, -2.32044753e+00])
Other functions¶
See [Torrence98].
-
mlpy.
angularfreq
(N, dt)¶ Compute angular frequencies.
Input
- N - [integer] number of data samples
- dt - [float] time step
Output
- angular frequencies - [1D numpy array float]
-
mlpy.
scales
(N, dj, dt, s0)¶ Compute scales.
Input
- N - [integer] number of data samples
- dj - [float] scale resolution
- dt - [float] time step
Output
- scales - [1D numpy array float]
-
mlpy.
compute_s0
(dt, p, wf)¶ Compute s0.
Input
- dt - [float] time step
- p - [float] omega0 (‘morlet’) or order (‘paul’, ‘dog’)
- wf - [string] wavelet function (‘morlet’, ‘paul’, ‘dog’)
Output
- s0 - [float]
[Torrence98] | (1, 2) C Torrence and G P Compo. Practical Guide to Wavelet Analysis |
[Gsldwt] | Gnu Scientific Library, http://www.gnu.org/software/gsl/ |