FIRWIN
The window method of FIR filter design involves multiplying an ideal impulse response by a window function to produce a practical finite impulse response filter.
This function computes the coefficients of a finite impulse response filter with linear phase. These filters are stable and have a predictable phase response.
Excel Usage
=FIRWIN(numtaps, cutoff, window, pass_zero, scale, fs)
numtaps(int, required): Length of the filter (number of coefficients).cutoff(list[list], required): Cutoff frequency or frequencies (expressed in same units as fs).window(str, optional, default: “hamming”): Desired window to use (e.g., ‘hamming’, ‘hann’, ‘blackman’).pass_zero(bool, optional, default: true): If True, the gain at frequency 0 is 1.scale(bool, optional, default: true): Set to True to scale the coefficients for unity gain.fs(float, optional, default: null): The sampling frequency of the signal.
Returns (list[list]): Coefficients of the FIR filter as a 2D array.
Example 1: Simple low-pass
Inputs:
| numtaps | cutoff |
|---|---|
| 3 | 0.1 |
Excel formula:
=FIRWIN(3, 0.1)
Expected output:
| Result | ||
|---|---|---|
| 0.0679902 | 0.86402 | 0.0679902 |
Example 2: Band-pass
Inputs:
| numtaps | cutoff | pass_zero | |
|---|---|---|---|
| 11 | 0.2 | 0.4 | false |
Excel formula:
=FIRWIN(11, {0.2,0.4}, FALSE)
Expected output:
| Result | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| -3.70383e-18 | -0.0406874 | -0.128586 | -0.0780689 | 0.208783 | 0.395894 | 0.208783 | -0.0780689 | -0.128586 | -0.0406874 | -3.70383e-18 |
Python Code
Show Code
import numpy as np
from scipy.signal import firwin as scipy_firwin
def firwin(numtaps, cutoff, window='hamming', pass_zero=True, scale=True, fs=None):
"""
FIR filter design using the window method.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.firwin.html
This example function is provided as-is without any representation of accuracy.
Args:
numtaps (int): Length of the filter (number of coefficients).
cutoff (list[list]): Cutoff frequency or frequencies (expressed in same units as fs).
window (str, optional): Desired window to use (e.g., 'hamming', 'hann', 'blackman'). Default is 'hamming'.
pass_zero (bool, optional): If True, the gain at frequency 0 is 1. Default is True.
scale (bool, optional): Set to True to scale the coefficients for unity gain. Default is True.
fs (float, optional): The sampling frequency of the signal. Default is None.
Returns:
list[list]: Coefficients of the FIR filter as a 2D array.
"""
try:
if isinstance(cutoff, list):
cutoff_flat = [float(v) for row in cutoff for v in row]
cutoff_val = cutoff_flat[0] if len(cutoff_flat) == 1 else cutoff_flat
else:
cutoff_val = float(cutoff)
result = scipy_firwin(
int(numtaps),
cutoff_val,
window=window,
pass_zero=bool(pass_zero),
scale=bool(scale),
fs=float(fs) if fs is not None else None
)
# Return as a 2D list (single row)
return [result.tolist()]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Length of the filter (number of coefficients).
Cutoff frequency or frequencies (expressed in same units as fs).
Desired window to use (e.g., 'hamming', 'hann', 'blackman').
If True, the gain at frequency 0 is 1.
Set to True to scale the coefficients for unity gain.
The sampling frequency of the signal.