How do I specify s and w for a morlet wavelet in scipy.signal.cwt?

Viewed 337

I'm trying to tune the s and w parameter in the scipy.signal.cwt wavelet transform

fs=2048
w = 6
f = 1
s = w*fs / (2*f*np.pi)
widths = np.arange(1, 100)
cwtmatr = signal.cwt(np.random.rand(10000), signal.morlet, widths, w=w, s=s)

but I have an issue saying

TypeError: morlet() got multiple values for argument 'w'

Although w can be tuned as it is a kwargs in the morlet prototype :

scipy.signal.morlet(M, w=5.0, s=1.0, complete=True) 

How can I set s and w when I call the scipy.signal.cwt function?

1 Answers

scipy.signal.morlet is incompatible with scipy.signal.cwt, as stated in a "Note" at the bottom of the documentation page and in the "See Also" box of scipy.signal.morlet2. Therefore, you should use morlet2 - the following example is included in the documentation:

from scipy import signal
import matplotlib.pyplot as plt
import numpy as np

t, dt = np.linspace(0, 1, 200, retstep=True)
fs = 1/dt
w = 6.
sig = np.cos(2*np.pi*(50 + 10*t)*t) + np.sin(40*np.pi*t)
freq = np.linspace(1, fs/2, 100)
widths = w*fs / (2*freq*np.pi)
cwtm = signal.cwt(sig, signal.morlet2, widths, w=w)
plt.pcolormesh(t, freq, np.abs(cwtm), cmap='viridis', shading='gouraud')
plt.show()

which produces

enter image description here

You can directly provide Omega0 in the call to cwt through the keyword w. For the width parameter of the wavelet s, it is already included in the widths argument of cwt: cwt will loop over each value in widths and provide it to morlet2. You can readily see this in the source code of cwt.

Related