THRESHOLD
This function applies wavelet-style thresholding to each value in an input range. Values are modified according to a selected thresholding rule and a threshold magnitude.
In soft thresholding, values are shrunk toward zero by the threshold after sign preservation, while in hard thresholding values below the threshold are replaced by a substitute value. Additional modes include garrote, greater, and less thresholding rules.
For soft thresholding, an input value x and threshold \lambda produce:
\hat{x} = \operatorname{sign}(x)\max(|x|-\lambda, 0)
This enables denoising workflows by attenuating small-magnitude coefficients.
Excel Usage
=THRESHOLD(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 the 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({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({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({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({1,2,3,4}, 2.5, "less", 9)
Expected output:
| Result | |||
|---|---|---|---|
| 1 | 2 | 9 | 9 |
Python Code
Show Code
import numpy as np
import pywt
def threshold(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 the input shape.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
data = to2d(data)
if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
return "Error: Invalid input - data must be a 2D list"
matrix = []
for row in data:
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)}"