DWT2

This function computes a single-level two-dimensional discrete wavelet transform of a numeric matrix.

The transform separates the matrix into one approximation component and three directional detail components: horizontal, vertical, and diagonal. These components summarize structure at different orientations and scales.

For separable 2D analysis, filtering and downsampling are applied along both axes to produce:

(cA, cH, cV, cD)

where cA is approximation and cH,cV,cD are detail subbands.

Excel Usage

=DWT2(data, wavelet, wavelet_mode)
  • data (list[list], required): Input 2D numeric matrix as an Excel range.
  • wavelet (str, required): Wavelet name.
  • wavelet_mode (str, optional, default: “symmetric”): Signal extension mode.

Returns (list[list]): Four rows containing flattened cA, cH, cV, and cD coefficient arrays, padded to a rectangular range.

Example 1: Db1 transform on 2x2 matrix

Inputs:

data wavelet wavelet_mode
1 2 db1 symmetric
3 4

Excel formula:

=DWT2({1,2;3,4}, "db1", "symmetric")

Expected output:

Result
5
-2
-1
0
Example 2: Db2 transform on 4x4 matrix

Inputs:

data wavelet wavelet_mode
1 2 3 4 db2 periodic
5 6 7 8
9 10 11 12
13 14 15 16

Excel formula:

=DWT2({1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16}, "db2", "periodic")

Expected output:

Result
25.6603 22.1962 25.6603 11.8038 8.33975 11.8038 25.6603 22.1962 25.6603
-8 -8 -8 6.28037e-16 9.42055e-16 6.28037e-16 -8 -8 -8
-2 -2.22045e-16 -2 -2 -9.99201e-16 -2 -2 -2.22045e-16 -2
6.66134e-16 4.44089e-16 6.66134e-16 -7.42976e-16 -1.9908e-16 -7.42976e-16 6.66134e-16 4.44089e-16 6.66134e-16
Example 3: Transform using periodization mode

Inputs:

data wavelet wavelet_mode
2 0 2 0 sym2 periodization
1 3 1 3
4 2 4 2
3 1 3 1

Excel formula:

=DWT2({2,0,2,0;1,3,1,3;4,2,4,2;3,1,3,1}, "sym2", "periodization")

Expected output:

Result
2.63397 2.63397 5.36603 5.36603
-0.366025 -0.366025 0.366025 0.366025
-1.36603 -1.36603 -0.633975 -0.633975
2.36603 2.36603 -0.366025 -0.366025
Example 4: Scalar input is promoted to 1x1 matrix

Inputs:

data wavelet wavelet_mode
5 db1 symmetric

Excel formula:

=DWT2(5, "db1", "symmetric")

Expected output:

Result
10
0
0
0

Python Code

import numpy as np
import pywt

def dwt2(data, wavelet, wavelet_mode='symmetric'):
    """
    Perform a single-level two-dimensional discrete wavelet transform.

    See: https://pywavelets.readthedocs.io/en/latest/ref/2d-dwt-and-idwt.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): Input 2D numeric matrix 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'.

    Returns:
        list[list]: Four rows containing flattened cA, cH, cV, and cD coefficient arrays, padded to a rectangular range.
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        matrix_in = to2d(data)
        if not isinstance(matrix_in, list) or not all(isinstance(row, list) for row in matrix_in):
            return "Error: Invalid input - data must be a 2D list"

        matrix = []
        for row in matrix_in:
            if len(row) == 0:
                return "Error: Input rows must not be empty"
            out_row = []
            for item in row:
                try:
                    out_row.append(float(item))
                except (TypeError, ValueError):
                    return "Error: Input must contain only numeric values"
            matrix.append(out_row)

        arr = np.asarray(matrix, dtype=float)
        if arr.ndim != 2:
            return "Error: Input must be two-dimensional"

        c_a, details = pywt.dwt2(arr, wavelet=wavelet, mode=wavelet_mode)
        c_h, c_v, c_d = details

        rows = [
            np.asarray(c_a).ravel().tolist(),
            np.asarray(c_h).ravel().tolist(),
            np.asarray(c_v).ravel().tolist(),
            np.asarray(c_d).ravel().tolist(),
        ]
        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)}"

Online Calculator

Input 2D numeric matrix as an Excel range.
Wavelet name.
Signal extension mode.