THRESHOLD_PYWT

This function applies wavelet thresholding to each value in an input range using a selected threshold rule.

Soft thresholding shrinks magnitudes toward zero, hard thresholding keeps large magnitudes unchanged while replacing smaller values, and other supported modes implement alternative denoising behaviors.

For soft thresholding with threshold \lambda:

\hat{x}=\operatorname{sign}(x)\max(|x|-\lambda,0)

The output preserves the input matrix shape.

Excel Usage

=THRESHOLD_PYWT(data, value, threshold_mode, substitute)
  • data (list[list], required): Input numeric values as an Excel range.
  • value (float, required): Threshold value.
  • threshold_mode (str, optional, default: “soft”): Thresholding mode.
  • substitute (float, optional, default: 0): Replacement value for thresholded entries.

Returns (list[list]): Thresholded values in a 2D range matching input shape.

Example 1: Soft threshold on a single row

Inputs:

data value threshold_mode substitute
1 1.5 2 2.5 3 2 soft 0

Excel formula:

=THRESHOLD_PYWT({1,1.5,2,2.5,3}, 2, "soft", 0)

Expected output:

Result
0 0 0 0.5 1
Example 2: Hard threshold on a 2x3 matrix

Inputs:

data value threshold_mode substitute
1 2 3 2 hard 0
4 0.5 2.5

Excel formula:

=THRESHOLD_PYWT({1,2,3;4,0.5,2.5}, 2, "hard", 0)

Expected output:

Result
0 2 3
4 0 2.5
Example 3: Greater mode replaces values below threshold

Inputs:

data value threshold_mode substitute
1 2 3 4 2.5 greater -1

Excel formula:

=THRESHOLD_PYWT({1,2,3,4}, 2.5, "greater", -1)

Expected output:

Result
-1 -1 3 4
Example 4: Less mode replaces values above threshold

Inputs:

data value threshold_mode substitute
1 2 3 4 2.5 less 9

Excel formula:

=THRESHOLD_PYWT({1,2,3,4}, 2.5, "less", 9)

Expected output:

Result
1 2 9 9

Python Code

import numpy as np
import pywt

def threshold_pywt(data, value, threshold_mode='soft', substitute=0):
    """
    Apply elementwise wavelet thresholding to numeric data.

    See: https://pywavelets.readthedocs.io/en/latest/ref/thresholding-functions.html

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

    Args:
        data (list[list]): Input numeric values as an Excel range.
        value (float): Threshold value.
        threshold_mode (str, optional): Thresholding mode. Valid options: Soft, Hard, Garrote, Greater, Less. Default is 'soft'.
        substitute (float, optional): Replacement value for thresholded entries. Default is 0.

    Returns:
        list[list]: Thresholded values in a 2D range matching input shape.
    """
    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:
            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)
        result = pywt.threshold(arr, value=float(value), mode=threshold_mode, substitute=float(substitute))
        return np.asarray(result).tolist()
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Input numeric values as an Excel range.
Threshold value.
Thresholding mode.
Replacement value for thresholded entries.