I want to compute the wavelet of a signal with different scales and timeshifts.
In Matlab using the cwt()
function (Continuous 1-D wavelet transform) provided in the Wavelet Toolbox I can specify the scale(s) I want as a parameter to cwt(), and it will return all possible timeshifts:
x = [1, 2, 3, 4];
scales = [1, 2, 3];
wavelet_name = 'db1';
coefs = cwt(x,scales, wavelet_name);
>> coefs =
-0.0000 -0.0000 -0.0000 0.0000
-0.7071 -0.7071 -0.7071 -0.7071
-1.1553 -1.1553 -1.1553 1.7371
How can I achieve that in Python?
Here are my two attempts so far:
scipy.signal.cwt
, I can't find the list of the built-in wavelet functions that I can pass to scipy.signal.cwt: I want to have at least the most common wavelet functions such as sym2 and db1. (e.g. see Matlab's built-in wavelet list).It seems like there are a few python libraries out there for Wavelet operations beyond scipy
:
Here's a link to the documentation, github and a basic snippet for usage. It's pretty intuitive to use and has a pretty extended library of implemented wavelets.
import pywt
import numpy as np
import matplotlib.pyplot as plt
num_steps = 512
x = np.arange(num_steps)
y = np.sin(2*np.pi*x/32)
delta_t = x[1] - x[0]
scales = np.arange(1,num_steps+1)
wavelet_type = 'morl'
coefs, freqs = pywt.cwt(y, scales, wavelet_type, delta_t)
plt.matshow(coefs)
plt.show()
Here's a link to the documentation, github and a basic snippet for usage. This library has a steeper learning curve and the api is not as nice, but supports functionalities such as cone of influence
or significance testing
.
import pycwt as wavelet
import numpy as np
import matplotlib.pyplot as plt
num_steps = 512
x = np.arange(num_steps)
y = np.sin(2*np.pi*x/32)
delta_t = x[1] - x[0]
scales = np.arange(1,num_steps+1)
freqs = 1/(wavelet.Morlet().flambda() * scales)
wavelet_type = 'morlet'
coefs, scales, freqs, coi, fft, fftfreqs = wavelet.cwt(y, delta_t, wavelet=wavelet_type, freqs=freqs)
plt.matshow(coefs.real)
plt.show()
You can easily install them using pip
or conda
.
Finally, here's other references that I haven't tried using:
You will probably want to use scipy.signal.cwt
. Some wavelet functions are provided in the scipy.signal
package:
scipy.signal.daub(1)
scipy.signal.morlet
scipy.signal.ricker
Symlets do not appear to be provided as-such, but you may be able to get them from daub
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With