8

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).
Community
  • 1
  • 1
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • Since this question received no answer, I have posted it on Quora: https://www.quora.com/What-is-the-equivalent-of-Matlabs-cwt-in-Python-continuous-1-D-wavelet-transform – Franck Dernoncourt Apr 13 '16 at 02:37
  • 1
    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. – Matteo Mar 21 '19 at 04:53
  • @Matteo Thanks for your answer, I haven't found any better library on my side. – Franck Dernoncourt Mar 21 '19 at 05:04
  • 1
    @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. – Matteo Mar 21 '19 at 05:05
  • @Matteo Sounds good, upvoted (last month) and accepted (now)! – Franck Dernoncourt Apr 01 '19 at 22:30

2 Answers2

3

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
3

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
Matteo
  • 7,924
  • 24
  • 84
  • 129