1

As the title shows, I want to define my own continuous wavelet by Python. However, I don't know how to achieve this exactly.

The formula of my wavelet mother function is below

enter image description here

It looks something like the Mexican hat wavelet, but they are different.

So how can I define such a custom wavelet by Python, then CWT can be performed using this wavelet?

Stephen Wong
  • 101
  • 12

1 Answers1

1

Per this you need a function that takes a number of points and a scale to provide as a wavelet argument

So we define it as such:

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

mother_wavelet = lambda z : np.exp(-z*z/4)*(2-z*z)/(4*math.sqrt(math.pi))

def mexican_hat_like(n,scale):
    x = np.linspace(-n/2,n/2,n)
    return mother_wavelet(x/scale)

Let's test it. We note that in fact something that looks very similar to yours is available. The difference is in scaling a and also the constant is front looks slightly different. Note math.sqrt(2) scaling for the Ricker wavelet

points = 100
a = 4.0
vec_ours = mexican_hat_like(points, a)
vec_theirs = signal.ricker(points, a*math.sqrt(2))
plt.plot(vec_ours, label = 'ours')
plt.plot(vec_theirs, label = 'ricker')
plt.legend(loc = 'best')
plt.show()

Here is the graph:

wavelets

piterbarg
  • 8,089
  • 2
  • 6
  • 22
  • Thanks for your nice answer! Why not define ```mother_wavelet``` inside the function ```mexican_hat_like```? – Stephen Wong Nov 28 '20 at 09:55
  • no reason -- can go there if you do not need it elsewhere – piterbarg Nov 28 '20 at 10:28
  • I noticed that the x-coordinate of your plot is not the true value of the wavelet's coordinate. Is there a way to show the true x values in the wavelet plot? – Stephen Wong Nov 28 '20 at 14:01
  • you can see them in the definition of the wavelet `x = np.linspace(-n/2,n/2,n) return mother_wavelet(x/scale)` so not sure what you mean? I am not sure what are the 'true' values of wavelets but you can change your x-s there – piterbarg Nov 28 '20 at 14:11