BUTTER

The Butterworth filter is a type of signal processing filter designed to have a frequency response as flat as possible in the passband. It is also referred to as a maximally flat magnitude filter.

For an Nth-order Butterworth low-pass filter, the magnitude squared of the frequency response is:

|H(j\omega)|^2 = \frac{1}{1 + (\frac{\omega}{\omega_c})^{2N}}

where N is the filter order and \omega_c is the cutoff frequency.

Excel Usage

=BUTTER(N, Wn, btype, filter_output_form, analog, fs)
  • N (int, required): The order of the filter.
  • Wn (list[list], required): Critical frequency or frequencies.
  • btype (str, optional, default: “lowpass”): The type of filter.
  • filter_output_form (str, optional, default: “ba”): Type of output representation.
  • analog (bool, optional, default: false): When True, return an analog filter.
  • fs (float, optional, default: null): The sampling frequency of the digital system.

Returns (list[list]): Filter coefficients or representation.

Example 1: Low-pass (b, a)

Inputs:

N Wn btype
2 0.1 lowpass

Excel formula:

=BUTTER(2, 0.1, "lowpass")

Expected output:

Result
0.0200834 0.0401667 0.0200834
1 -1.56102 0.641352
Example 2: High-pass (SOS)

Inputs:

N Wn btype fs filter_output_form
4 100 highpass 1000 sos

Excel formula:

=BUTTER(4, 100, "highpass", 1000, "sos")

Expected output:

Result
0.432847 -0.865693 0.432847 1 -1.0486 0.29614
1 -2 1 1 -1.32091 0.632739

Python Code

Show Code
import numpy as np
from scipy.signal import butter as scipy_butter

def butter(N, Wn, btype='lowpass', filter_output_form='ba', analog=False, fs=None):
    """
    Butterworth digital and analog filter design.

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

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

    Args:
        N (int): The order of the filter.
        Wn (list[list]): Critical frequency or frequencies.
        btype (str, optional): The type of filter. Valid options: Lowpass, Highpass, Bandpass, Bandstop. Default is 'lowpass'.
        filter_output_form (str, optional): Type of output representation. Valid options: Numerator/Denominator, Pole-Zero, Second-Order Sections. Default is 'ba'.
        analog (bool, optional): When True, return an analog filter. Default is False.
        fs (float, optional): The sampling frequency of the digital system. Default is None.

    Returns:
        list[list]: Filter coefficients or representation.
    """
    try:
        if isinstance(Wn, list):
            wn_flat = [float(v) for row in Wn for v in row]
            wn_val = wn_flat[0] if len(wn_flat) == 1 else wn_flat
        else:
            wn_val = float(Wn)

        result = scipy_butter(
            int(N), 
            wn_val, 
            btype=btype, 
            analog=bool(analog), 
            output=filter_output_form, 
            fs=float(fs) if fs is not None else None
        )

        if filter_output_form == 'ba':
            b, a = result
            max_len = max(len(b), len(a))
            return [b.tolist() + [""] * (max_len - len(b)), a.tolist() + [""] * (max_len - len(a))]
        elif filter_output_form == 'zpk':
            z, p, k = result
            max_len = max(len(z), len(p), 1)
            return [z.tolist() + [""] * (max_len - len(z)), p.tolist() + [""] * (max_len - len(p)), [float(k)] + [""] * (max_len - 1)]
        else: # sos
            return result.tolist()
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

The order of the filter.
Critical frequency or frequencies.
The type of filter.
Type of output representation.
When True, return an analog filter.
The sampling frequency of the digital system.