SOSFILTFILT

The sosfiltfilt function applies a digital filter using cascaded second-order sections (SOS) both forwards and backwards. This results in zero phase distortion and is numerically much more stable than using numerator/denominator (b, a) coefficients, especially for higher-order filters.

SOS representation is generally recommended for all filtering tasks in digital signal processing to avoid precision issues.

Excel Usage

=SOSFILTFILT(x, sos, axis, padtype, padlen)
  • x (list[list], required): The input array of data to be filtered.
  • sos (list[list], required): Array of second-order sections filter coefficients (n_sections by 6).
  • axis (int, optional, default: -1): The axis along which the filter is applied.
  • padtype (str, optional, default: “odd”): Extension mode for padding at edges.
  • padlen (int, optional, default: null): Number of elements to extend x by at both ends.

Returns (list[list]): The filtered signal as a 2D array.

Example 1: SOS zero-phase filtering

Inputs:

x sos
1 2 3 4 5 4 3 2 1 2 3 4 0.02008337 0.04016673 0.02008337 1 -1.56101808 0.64135154

Excel formula:

=SOSFILTFILT({1,2,3,4,5,4,3,2,1,2,3,4}, {0.02008337,0.04016673,0.02008337,1,-1.56101808,0.64135154})

Expected output:

Result
0.891233 1.36469 1.81575 2.21546 2.54703 2.81074 3.02431 3.21646 3.41574 3.6397 3.889 4.14893
Example 2: Even padding extension

Inputs:

x sos padtype
1 2 3 4 5 4 3 2 1 2 3 4 0.02008337 0.04016673 0.02008337 1 -1.56101808 0.64135154 even

Excel formula:

=SOSFILTFILT({1,2,3,4,5,4,3,2,1,2,3,4}, {0.02008337,0.04016673,0.02008337,1,-1.56101808,0.64135154}, "even")

Expected output:

Result
3.00993 3.0197 3.03295 3.03579 3.01407 2.96221 2.88721 2.80537 2.73416 2.68416 2.65592 2.64429

Python Code

Show Code
import numpy as np
from scipy.signal import sosfiltfilt as scipy_sosfiltfilt

def sosfiltfilt(x, sos, axis=-1, padtype='odd', padlen=None):
    """
    Forward-backward digital filter using cascaded second-order sections.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.sosfiltfilt.html

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

    Args:
        x (list[list]): The input array of data to be filtered.
        sos (list[list]): Array of second-order sections filter coefficients (n_sections by 6).
        axis (int, optional): The axis along which the filter is applied. Default is -1.
        padtype (str, optional): Extension mode for padding at edges. Valid options: Odd, Even, Constant. Default is 'odd'.
        padlen (int, optional): Number of elements to extend x by at both ends. Default is None.

    Returns:
        list[list]: The filtered signal as a 2D array.
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        x_arr = np.array(to2d(x))
        sos_arr = np.array(to2d(sos))

        result = scipy_sosfiltfilt(
            sos_arr, 
            x_arr, 
            axis=int(axis), 
            padtype=padtype, 
            padlen=int(padlen) if padlen is not None else None
        )

        return result.tolist()
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

The input array of data to be filtered.
Array of second-order sections filter coefficients (n_sections by 6).
The axis along which the filter is applied.
Extension mode for padding at edges.
Number of elements to extend x by at both ends.