What is the equivalent of Matlab's cwt() in Python? (continuous 1-D wavelet transform)
Asked Answered
D

2

9

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:

  1. In PyWavelets (Discrete Wavelet Transform in Python), I don't see how I can specify the scale parameter of the wavelet.
  2. In 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).
Doerrer answered 19/5, 2014 at 15:39 Comment(5)
Since this question received no answer, I have posted it on Quora: quora.com/…Doerrer
I posted an answer, would you mind taking a look? I'd be curious to know if you found better libraries that I could use as well.Enameling
@Enameling Thanks for your answer, I haven't found any better library on my side.Doerrer
@Frank - Thanks for the quick reply. It's really too bad, cause there is nothing comparable to the matlab toolbox for python. Pywavelets is my choice but the fact the don't have cone of influence or significance testing really limits the usage.Enameling
@Enameling Sounds good, upvoted (last month) and accepted (now)!Doerrer
E
5

It seems like there are a few python libraries out there for Wavelet operations beyond scipy:

Pywavelets

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()

PyCWT

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:

  1. one
  2. two
  3. three
Enameling answered 21/3, 2019 at 4:50 Comment(0)
U
4

You will probably want to use scipy.signal.cwt. Some wavelet functions are provided in the scipy.signal package:

Symlets do not appear to be provided as-such, but you may be able to get them from daub.

Unmeant answered 12/7, 2016 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.