2

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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your nice answer! Why not define mother_wavelet inside the function mexican_hat_like?
no reason -- can go there if you do not need it elsewhere
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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.