Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).
like image 280
Franck Dernoncourt Avatar asked May 19 '14 15:05

Franck Dernoncourt


2 Answers

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
like image 83
Matteo Avatar answered Nov 07 '22 16:11

Matteo


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

  • Daubechies family: scipy.signal.daub(1)
  • Morlet: scipy.signal.morlet
  • Ricker: scipy.signal.ricker

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

like image 33
Mad Physicist Avatar answered Nov 07 '22 18:11

Mad Physicist