WAVEDEC
This function performs a multilevel one-dimensional discrete wavelet transform (DWT) on a signal and returns approximation and detail coefficients by level. The first output row contains the coarsest approximation coefficients, and the following rows contain detail coefficients from coarse to fine scales.
The decomposition repeatedly applies low-pass and high-pass filtering with downsampling, producing coefficient sets of varying lengths. If the decomposition level is omitted, a maximum valid level is chosen automatically from the signal length and wavelet filter length.
Let x[n] be the input signal. At each level j, approximation and detail coefficients are produced by filtered and downsampled sequences:
cA_j[k] = \sum_n x_j[n] h[2k-n], \quad cD_j[k] = \sum_n x_j[n] g[2k-n]
where h and g are analysis low-pass and high-pass filters and x_{j+1} = cA_j.
Excel Usage
=WAVEDEC(data, wavelet, wavedec_mode, level, axis)
data(list[list], required): Input signal values as an Excel range.wavelet(str, required): Wavelet name (for example db1, db2, sym5).wavedec_mode(str, optional, default: “symmetric”): Signal extension mode.level(int, optional, default: null): Decomposition level (if empty, the maximum valid level is used).axis(int, optional, default: -1): Axis index for decomposition.
Returns (list[list]): Coefficient arrays as rows ordered as [cA_n, cD_n, …, cD1], padded with blanks to form a rectangular range.
Example 1: Two-level db1 decomposition
Inputs:
| data | wavelet | wavedec_mode | level | axis | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | db1 | symmetric | 2 | -1 |
Excel formula:
=WAVEDEC({1,2,3,4,5,6,7,8}, "db1", "symmetric", 2, -1)
Expected output:
| Result | |||
|---|---|---|---|
| 5 | 13 | ||
| -2 | -2 | ||
| -0.707107 | -0.707107 | -0.707107 | -0.707107 |
Example 2: Db2 decomposition with automatic level
Inputs:
| data | wavelet | wavedec_mode | level | axis | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | db2 | symmetric | -1 |
Excel formula:
=WAVEDEC({2,4,6,8,10,12,14,16}, "db2", "symmetric", , -1)
Expected output:
| Result | ||||
|---|---|---|---|---|
| 3.53553 | 4.62158 | 10.2784 | 15.9353 | 21.9203 |
| -1.22474 | 3.33067e-16 | 6.66134e-16 | 4.44089e-16 | 1.22474 |
Example 3: Scalar input is normalized to one-point signal
Inputs:
| data | wavelet | wavedec_mode | level | axis |
|---|---|---|---|---|
| 5 | db1 | symmetric | 1 | -1 |
Excel formula:
=WAVEDEC(5, "db1", "symmetric", 1, -1)
Expected output:
| Result |
|---|
| 7.07107 |
| 0 |
Example 4: Decomposition using periodization mode
Inputs:
| data | wavelet | wavedec_mode | level | axis | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 3 | 1 | 0 | 4 | 8 | 6 | 5 | 2 | sym2 | periodization | 2 | -1 |
Excel formula:
=WAVEDEC({3,1,0,4,8,6,5,2}, "sym2", "periodization", 2, -1)
Expected output:
| Result | |||
|---|---|---|---|
| 5.41418 | 9.08582 | ||
| -5.96739 | 2.05232 | ||
| -0.0947343 | -0.647048 | 0.293494 | -1.67303 |
Python Code
Show Code
import numpy as np
import pywt
def wavedec(data, wavelet, wavedec_mode='symmetric', level=None, axis=-1):
"""
Compute a multilevel one-dimensional discrete wavelet decomposition.
See: https://pywavelets.readthedocs.io/en/latest/ref/dwt-discrete-wavelet-transform.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input signal values as an Excel range.
wavelet (str): Wavelet name (for example db1, db2, sym5).
wavedec_mode (str, optional): Signal extension mode. Valid options: Symmetric, Periodization, Zero, Constant, Smooth, Periodic, Reflect, Antisymmetric, Antireflect. Default is 'symmetric'.
level (int, optional): Decomposition level (if empty, the maximum valid level is used). Default is None.
axis (int, optional): Axis index for decomposition. Default is -1.
Returns:
list[list]: Coefficient arrays as rows ordered as [cA_n, cD_n, ..., cD1], padded with blanks to form a rectangular range.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
def flatten_numeric(matrix):
values = []
for row in matrix:
if not isinstance(row, list):
return None
for item in row:
try:
values.append(float(item))
except (TypeError, ValueError):
continue
return values
data = to2d(data)
flat = flatten_numeric(data)
if flat is None:
return "Error: Invalid input - data must be a 2D list"
if len(flat) == 0:
return "Error: Input must contain at least one numeric value"
level_value = None if level is None else int(level)
axis_value = int(axis)
coeffs = pywt.wavedec(np.asarray(flat, dtype=float), wavelet=wavelet, mode=wavedec_mode, level=level_value, axis=axis_value)
rows = [np.asarray(coeff).tolist() for coeff in coeffs]
max_len = max(len(row) for row in rows)
return [row + [""] * (max_len - len(row)) for row in rows]
except Exception as e:
return f"Error: {str(e)}"