pywt的wavedec方法的源码
时间: 2023-08-04 22:09:17 浏览: 96
wave波形显示源码
3星 · 编辑精心推荐
以下是PyWavelets库的wavedec方法的源代码:
```python
def wavedec(data, wavelet, mode='symmetric', level=None, axis=-1):
"""
Multilevel 1D wavelet decomposition.
Parameters
----------
data : array_like
Input data. Must be a 1D numpy array.
wavelet : Wavelet object or name string
Wavelet to use. This can be a wavelet name string, or a Wavelet
object which represents the wavelet filter bank. Wavelet families
supported are Daubechies ('db'), Symlets ('sym'), Coiflets ('coif'),
Discrete Meyer ('dmey'), Biorthogonal ('bior'), and Reverse Biorthogonal
('rbio').
mode : str, optional
Signal extension mode, see Modes (default: 'symmetric').
level : int, optional
The number of decomposition levels to compute. If level is None (default)
then as many levels as possible are computed. Otherwise, level must be
a positive integer <= min(floor(log2(data.shape[axis]))), and indicates
the number of decomposition levels. Note that if the length of the data
is not divisible by 2**level, then the data is truncated to ensure this
property.
axis : int, optional
Axis over which to compute the DWT. If not given, the last axis is used.
Returns
-------
coeffs : list
Coefficients list. At each level, the coefficients are arranged
in a tuple (cA_n, cD_n, cD_n-1, ..., cD2, cD1), where cA_n and cD_n
represent the approximation and detail coefficients at level n, and
cD_i represents the detail coefficients at level i.
Notes
-----
The multilevel DWT is performed by recursively applying the single-level
DWT to the approximation coefficients of the previous level, and collecting
the detail coefficients. The process continues until the specified number
of levels is reached, or the data size is too small for further decomposition.
"""
if not isinstance(data, np.ndarray):
data = np.asarray(data)
if not isinstance(wavelet, Wavelet):
wavelet = Wavelet(wavelet)
if level is None:
level = pywt.dwt_max_level(len(data), wavelet)
if level < 0:
raise ValueError("Level value must be non-negative")
# check for valid wavelet
if len(wavelet.dec_lo) != 2 or len(wavelet.dec_hi) != 2:
raise ValueError("Wavelet should have 2 decomposition filters. "
"Found wavelet with %d and %d coefficients."
% (len(wavelet.dec_lo), len(wavelet.dec_hi)))
if axis < 0:
axis = data.ndim + axis
if axis != data.ndim-1:
data = np.swapaxes(data, axis, -1)
coeffs = []
# Decompose down to level
for i in range(level):
if data.shape[-1] % 2:
# Insert 0 at end to even the length of data
# This is cropped off after DWT
data = np.pad(data, [(0, 1)] * (data.ndim - 1) + [(0, 0)], mode='constant')
_need_crop = True
else:
_need_crop = False
cD, cA = dwt(data, wavelet, mode)
if _need_crop:
cD = cD[..., :-1]
cA = cA[..., :-1]
coeffs.append(cD)
data = cA
coeffs.append(data)
if axis != data.ndim - 1:
# move the constructed axis to its target position
coeffs = list(map(lambda x: np.swapaxes(x, axis, -1), coeffs))
return coeffs
```
阅读全文