DWT
This function computes a one-dimensional single-level discrete wavelet transform and returns approximation and detail coefficients.
The approximation coefficients capture low-frequency content and the detail coefficients capture high-frequency content. Both are produced by filtering and downsampling the input signal.
For filters h and g and signal x[n]:
cA[k]=\sum_n x[n]h[2k-n], \quad cD[k]=\sum_n x[n]g[2k-n]
Results are returned as two rows in a 2D range.
Excel Usage
=DWT(data, wavelet, wavelet_mode, 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.axis(int, optional, default: -1): Axis index for decomposition.
Returns (list[list]): Two rows containing approximation and detail coefficients.
Example 1: Db1 transform on even-length signal
Inputs:
| data | wavelet | wavelet_mode | axis | |||
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | db1 | symmetric | -1 |
Excel formula:
=DWT({1,2,3,4}, "db1", "symmetric", -1)
Expected output:
| Result | |
|---|---|
| 2.12132 | 4.94975 |
| -0.707107 | -0.707107 |
Example 2: Db2 transform with periodic mode
Inputs:
| data | wavelet | wavelet_mode | axis | |||||
|---|---|---|---|---|---|---|---|---|
| 2 | 4 | 6 | 8 | 10 | 12 | db2 | periodic | -1 |
Excel formula:
=DWT({2,4,6,8,10,12}, "db2", "periodic", -1)
Expected output:
| Result | |||
|---|---|---|---|
| 14.7985 | 4.62158 | 10.2784 | 14.7985 |
| -4.24264 | 3.33067e-16 | 6.66134e-16 | -4.24264 |
Example 3: Scalar input is normalized to one-point signal
Inputs:
| data | wavelet | wavelet_mode | axis |
|---|---|---|---|
| 7 | db1 | symmetric | -1 |
Excel formula:
=DWT(7, "db1", "symmetric", -1)
Expected output:
| Result |
|---|
| 9.89949 |
| 0 |
Example 4: Matrix input is flattened before transform
Inputs:
| data | wavelet | wavelet_mode | axis | |
|---|---|---|---|---|
| 1 | 2 | sym2 | periodization | -1 |
| 3 | 4 |
Excel formula:
=DWT({1,2;3,4}, "sym2", "periodization", -1)
Expected output:
| Result | |
|---|---|
| 2.82843 | 4.24264 |
| -0.517638 | 1.93185 |
Python Code
import numpy as np
import pywt
def dwt(data, wavelet, wavelet_mode='symmetric', axis=-1):
"""
Perform a single-level one-dimensional discrete wavelet transform.
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'.
axis (int, optional): Axis index for decomposition. Default is -1.
Returns:
list[list]: Two rows containing approximation and detail coefficients.
"""
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"
c_a, c_d = pywt.dwt(np.asarray(signal, dtype=float), wavelet=wavelet, mode=wavelet_mode, axis=int(axis))
row_a = np.asarray(c_a).tolist()
row_d = np.asarray(c_d).tolist()
max_len = max(len(row_a), len(row_d))
return [row_a + [""] * (max_len - len(row_a)), row_d + [""] * (max_len - len(row_d))]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Input signal values as an Excel range.
Wavelet name.
Signal extension mode.
Axis index for decomposition.