-
-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why Gabor wavelet does not implemented in CWT? #637
Comments
When using Numba, the scratch code is below; could you fix the code to be faaaaaaaaaaaaaaster? from numba import njit, prange, objmode, complex128
import numpy as np
@njit("c16(f8, f8)", fastmath=False)
def jit_gabor_t(
t: float,
sigma: float = 0.8909
) -> complex:
return 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( -t**2/(2 * sigma**2) + 2 * np.pi * 1j * t )
@njit("c16(f8, f8, f8)", fastmath=False)
def jit_gabor_f_tau_t(
f: float,
tau: float,
t: float
) -> complex:
return abs(f) * jit_gabor_t(t=f * (t - tau), sigma=0.8909)
@njit("f8(f8, f8, f8)", fastmath=False)
def jit_calc_wavelet_window_width(
f: float,
sigma: float,
amp: float = 0.005,
) -> float:
return 1/f * sigma * np.sqrt(-2 * np.log(amp))
@njit("c16(f8[:], f8[:], f8, f8)", fastmath=False)
def jit_gabor_J(
sig: np.ndarray,
time: np.ndarray,
f: float,
tau: float
) -> complex:
tmp = time - tau
for i in prange(len(tmp)):
tmp[i] = abs(tmp[i])
tauidx = np.argmin(tmp)
wavelet_window_width = jit_calc_wavelet_window_width(f=f, sigma=0.8909, amp=0.005)
_indexes = np.where( (time[tauidx] - wavelet_window_width <= time)
& (time <= time[tauidx] + wavelet_window_width) )
idxs = np.arange(len(time))[_indexes]
min_, max_ = idxs.min(), idxs.max()
out = 0. + 0.j
for i in range(min_, max_+1):
out += sig[i] * jit_gabor_f_tau_t(f=f, tau=tau, t=time[i])
return out
@njit("c16[:,:](f8[:], f8[:], f8[:])")
def jit_gabor(
time: np.ndarray,
sig: np.ndarray,
freqs: np.ndarray
) -> np.ndarray:
N_t = len(time)
N_f = len(freqs)
out = np.zeros((N_f, N_t), dtype=complex128)
for fi in range(N_f):
for ti in range(N_t):
out[fi,ti] = jit_gabor_J(sig=sig, time=time, f=freqs[fi], tau=time[ti])
return out Sample: import time
import matplotlib.pyplot as plt
import numpy
N_t = 200
N_f = 20
dt = 0.01 # sampling intervals
t = np.arange(-1, 1, dt)
sig = np.cos(2 * np.pi * 7 * t) + np.real(np.exp(-7*(t-0.4)**2)*np.exp(1j*2*np.pi*2*(t-0.4)))
start = time.time()
res = jit_gabor(time=t, sig=sig, freqs=np.arange(1, N_f).astype(float))
fin = time.time() - start
print(fin, "sec")
fig, ax = plt.subplots()
ax.imshow(np.abs(res), aspect='auto', origin='lower')
ax.twinx().plot(sig)
ax.set(xlabel="Time [sec]", ylabel="Frequency [Hz]")
plt.show() |
Hi @kaz0120, unfortunately the original CWT contributor to PyWavelets is no longer active with the project and I am more familiar with the discrete transforms. In general it is mostly @rgommers and myself doing basic maintenance of the library at the moment, but neither of us has the bandwidth to develop new features. That said, I have looked at the code briefly in the past and had done some research regarding your question about the integral. There is a summary with links to more info in this comment: |
Hi there, at first, sorry for my poor English.
I want to apply CWT on my signal data by Python, i found this library; PyWavelets.
I used Morlet mother-wavelet, and get great result. Next, I found the Improved Gavor wavelet.
Improved Gabor wavelet (IGW) is great one to understand the relationships between time ans frequency domains, so just I thought I wanna use this wavelet.
I wrote the scratch code for IGW successfully, but it's toooooooo heavy to calculate the coefficients. So I refered
pywt.cwt
and rewrite the code as below; but I can't make sure how to implemet the calculations because IGW does not use the scale parameter. Whypywt.cwt
compute the integrals? Is it faster method? How can I see its argorithm? Please help me...The text was updated successfully, but these errors were encountered: