PyWavelets / pywt

PyWavelets - Wavelet Transforms in Python
http://pywavelets.readthedocs.org
MIT License
2.04k stars 470 forks source link

Allow user to set `precision` in CWT, increase default to 12 #570

Open OverLordGoldDragon opened 4 years ago

OverLordGoldDragon commented 4 years ago

Addresses this Issue. Detailed explanation here; a summary:

  1. Low precision distorts CWT, and heavily at high scales.
  2. With the L2-norm used, scales beyond 64 are increasingly distorted.
  3. Zipper-like artifacts can be seen in computed heatmaps
  4. precision=10 is low, and precision=12 does not add much computation cost, while considerably remedying 1-3.

At the least, the user should get to decide precision instead of carving it in stone. And a doc/comment should be made about these caveats.


Improvement comparison

Code ```python import numpy as np import matplotlib.pyplot as plt from pywt._extensions._pywt import DiscreteContinuousWavelet from pywt._functions import integrate_wavelet #%%########################################################################## def l2(x): return np.sqrt(np.sum(np.abs(x ** 2))) scales = np.power(2 ** (1 / 32), np.arange(1, 320 + 1)) wavelet = DiscreteContinuousWavelet('morl') #%%########################################################################## l2res = [] for precision in (10, 12, 14): l2res.append([]) for scale in scales: int_psi, x = integrate_wavelet(wavelet, precision=precision) step = x[1] - x[0] linrange = max(x) - min(x) j = np.arange(scale * linrange + 1) / (scale * step) j = j.astype(int) # floor if j[-1] >= int_psi.size: j = np.extract(j < int_psi.size, j) int_psi_scale = int_psi[j][::-1].real l2res[-1].append(l2(np.diff(int_psi_scale) * np.sqrt(scale))) #%%########################################################################## plt.plot(np.log2(scales), l2res[0]) plt.plot(np.log2(scales), l2res[1]) plt.plot(np.log2(scales), l2res[2]) plt.show() ```
grlee77 commented 4 years ago

Thanks, I am fine with exposing this parameter.

I will take a look at the info you found soon and review it a little closer.

OverLordGoldDragon commented 3 years ago

@grlee77 Some interesting findings here (see bottom); to my surprise, pywt seems to outperform scipy on low scales. Also found a caveat2 (bottom) on integrated wavelet.

I'm curious on rationale behind integrated wavelet, which isn't documented on the site you found; perhaps it can be improved further.