Repository URL to install this package:
|
Version:
3.3.14 ▾
|
import numpy as np
from ._extensions._pywt import (DiscreteContinuousWavelet, ContinuousWavelet,
Wavelet, _check_dtype)
from ._functions import integrate_wavelet, scale2frequency
__all__ = ["cwt"]
def cwt(data, scales, wavelet, sampling_period=1.):
"""
cwt(data, scales, wavelet)
One dimensional Continuous Wavelet Transform.
Parameters
----------
data : array_like
Input signal
scales : array_like
The wavelet scales to use. One can use
``f = scale2frequency(scale, wavelet)/sampling_period`` to determine
what physical frequency, ``f``. Here, ``f`` is in hertz when the
``sampling_period`` is given in seconds.
wavelet : Wavelet object or name
Wavelet to use
sampling_period : float
Sampling period for the frequencies output (optional).
The values computed for ``coefs`` are independent of the choice of
``sampling_period`` (i.e. ``scales`` is not scaled by the sampling
period).
Returns
-------
coefs : array_like
Continuous wavelet transform of the input signal for the given scales
and wavelet
frequencies : array_like
If the unit of sampling period are seconds and given, than frequencies
are in hertz. Otherwise, a sampling period of 1 is assumed.
Notes
-----
Size of coefficients arrays depends on the length of the input array and
the length of given scales.
Examples
--------
>>> import pywt
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(512)
>>> y = np.sin(2*np.pi*x/32)
>>> coef, freqs=pywt.cwt(y,np.arange(1,129),'gaus1')
>>> plt.matshow(coef) # doctest: +SKIP
>>> plt.show() # doctest: +SKIP
----------
>>> import pywt
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> t = np.linspace(-1, 1, 200, endpoint=False)
>>> 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)))
>>> widths = np.arange(1, 31)
>>> cwtmatr, freqs = pywt.cwt(sig, widths, 'mexh')
>>> plt.imshow(cwtmatr, extent=[-1, 1, 1, 31], cmap='PRGn', aspect='auto',
... vmax=abs(cwtmatr).max(), vmin=-abs(cwtmatr).max()) # doctest: +SKIP
>>> plt.show() # doctest: +SKIP
"""
# accept array_like input; make a copy to ensure a contiguous array
dt = _check_dtype(data)
data = np.array(data, dtype=dt)
if not isinstance(wavelet, (ContinuousWavelet, Wavelet)):
wavelet = DiscreteContinuousWavelet(wavelet)
if np.isscalar(scales):
scales = np.array([scales])
if data.ndim == 1:
if wavelet.complex_cwt:
out = np.zeros((np.size(scales), data.size), dtype=complex)
else:
out = np.zeros((np.size(scales), data.size))
precision = 10
int_psi, x = integrate_wavelet(wavelet, precision=precision)
for i in np.arange(np.size(scales)):
step = x[1] - x[0]
j = np.floor(
np.arange(scales[i] * (x[-1] - x[0]) + 1) / (scales[i] * step))
if np.max(j) >= np.size(int_psi):
j = np.delete(j, np.where((j >= np.size(int_psi)))[0])
coef = - np.sqrt(scales[i]) * np.diff(
np.convolve(data, int_psi[j.astype(np.int)][::-1]))
d = (coef.size - data.size) / 2.
if d > 0:
out[i, :] = coef[int(np.floor(d)):int(-np.ceil(d))]
elif d == 0.:
out[i, :] = coef
else:
raise ValueError(
"Selected scale of {} too small.".format(scales[i]))
frequencies = scale2frequency(wavelet, scales, precision)
if np.isscalar(frequencies):
frequencies = np.array([frequencies])
for i in np.arange(len(frequencies)):
frequencies[i] /= sampling_period
return out, frequencies
else:
raise ValueError("Only dim == 1 supported")