Search code examples
pythonscipywaveletwavelet-transformpywavelets

How to define my own continuous wavelet by using Python?


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?


Solution

  • 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