WAVEDEC_PYWT
This function performs a multilevel one-dimensional discrete wavelet transform and returns approximation and detail coefficient arrays by level.
Decomposition repeatedly applies low-pass and high-pass filtering followed by downsampling. The first coefficient row is the coarsest approximation, followed by detail rows from coarse to fine scales.
At level j, coefficients are computed as:
cA_j[k]=\sum_n x_j[n]h[2k-n], \quad cD_j[k]=\sum_n x_j[n]g[2k-n]
with the next-level signal defined by x_{j+1}=cA_j.
Excel Usage
=WAVEDEC_PYWT(data, wavelet, wavelet_mode, level, axis)
data(list[list], required): Input signal values as an Excel range.wavelet(str, required): Wavelet name.wavelet_mode(str, optional, default: “symmetric”): Signal extension mode.level(int, optional, default: null): Decomposition level (empty uses maximum valid level).axis(int, optional, default: -1): Axis index for decomposition.
Returns (list[list]): Coefficient rows ordered as [cA_n, cD_n, …, cD1], padded to a rectangular range.
Example 1: Two-level db1 decomposition
Inputs:
| data | wavelet | wavelet_mode | level | axis | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | db1 | symmetric | 2 | -1 |
Excel formula:
=WAVEDEC_PYWT({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 | wavelet_mode | level | axis | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | db2 | symmetric | -1 |
Excel formula:
=WAVEDEC_PYWT({2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32}, "db2", "symmetric", , -1)
Expected output:
| Result | ||||||||
|---|---|---|---|---|---|---|---|---|
| 5.38397 | 5.81523 | 19.6077 | 35.6077 | 52.3708 | 63.1651 | |||
| -0.665064 | -0.591506 | -4.44089e-16 | -1.33227e-15 | 2.84808 | 0.616025 | |||
| -1.22474 | 3.33067e-16 | 6.66134e-16 | 4.44089e-16 | 4.44089e-16 | 8.88178e-16 | 1.77636e-15 | 8.88178e-16 | 1.22474 |
Example 3: Scalar input is normalized to one-point signal
Inputs:
| data | wavelet | wavelet_mode | level | axis | |
|---|---|---|---|---|---|
| 5 | 6 | db1 | symmetric | 1 | -1 |
Excel formula:
=WAVEDEC_PYWT({5,6}, "db1", "symmetric", 1, -1)
Expected output:
| Result |
|---|
| 7.77817 |
| -0.707107 |
Example 4: Decomposition using periodization mode
Inputs:
| data | wavelet | wavelet_mode | level | axis | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 3 | 1 | 0 | 4 | 8 | 6 | 5 | 2 | sym2 | periodization | 2 | -1 |
Excel formula:
=WAVEDEC_PYWT({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
import numpy as np
import pywt
def wavedec_pywt(data, wavelet, wavelet_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.
wavelet_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 (empty uses maximum valid level). Default is None.
axis (int, optional): Axis index for decomposition. Default is -1.
Returns:
list[list]: Coefficient rows ordered as [cA_n, cD_n, ..., cD1], padded to 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
signal = flatten_numeric(to2d(data))
if signal is None:
return "Error: Invalid input - data must be a 2D list"
if len(signal) == 0:
return "Error: Input must contain at least one numeric value"
level_value = None if level is None else int(level)
coeffs = pywt.wavedec(
np.asarray(signal, dtype=float),
wavelet=wavelet,
mode=wavelet_mode,
level=level_value,
axis=int(axis),
)
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)}"