Skip to content

Low-pass FIR filter

Fact Value
Also called Windowed sinc filter
Status Implemented and tested
Family Linear spatial filters
Engine Browser
Type Linear, spatial, separable convolution
Output Image
Operator shown as dynamic-kernel (exact)
Support radius (taps - 1) / 2
Code web/src/core/filters/linear/fir-lowpass.ts, firLowpassFilter
Operator code web/src/core/filters/linear/fir-lowpass.ts, firTaps
Channels RGB, Brightness only, Color only

Overview

The low-pass FIR filter keeps patterns coarser than a cutoff frequency and removes finer ones. Its taps come from the window method, the classic way to design a finite impulse response (FIR) filter. You set the cutoff in cycles per pixel, the number of taps, and the window that shapes the taps.

A line plot across a sharp edge after low-pass filtering with 31 taps and cutoff 0.08. The Hamming curve dips and overshoots a little more beside the edge than the Blackman curve. A line plot across a sharp edge after low-pass filtering with 31 taps and cutoff 0.08. The Hamming curve dips and overshoots a little more beside the edge than the Blackman curve.
Near a sharp edge, Hamming taps ring a little more than Blackman taps.

How it works

  1. Start from the perfect low-pass filter. Its taps follow a sinc wave that never ends.
  2. Cut the wave to the chosen number of taps and taper its ends with a window.
  3. Divide the taps by their sum, so flat areas keep their value.
  4. Filter every row with the taps, then every column.

Mathematical foundation

\[ \begin{gathered} h[n] = \frac{2 f_c \operatorname{sinc}\!\big(2 f_c (n - M)\big)\, w[n]}{\sum_{k=0}^{N-1} 2 f_c \operatorname{sinc}\!\big(2 f_c (k - M)\big)\, w[k]}, \qquad M = \frac{N-1}{2}, \\[4pt] I_{\text{out}}(x,y) = \sum_{j=0}^{N-1} \sum_{i=0}^{N-1} h[i]\, h[j]\, I(x+i-M,\, y+j-M), \qquad N = 15, \quad f_c = 0.15 \end{gathered} \]

The formula shows the default parameter values.

where

  • \(h[n]\): Tap n of the 1D filter
  • \(N\): Number of taps, odd
  • \(M\): Index of the center tap
  • \(f_c\): Cutoff frequency in cycles per pixel
  • \(\operatorname{sinc}(t)\): sin(pi t) / (pi t), and 1 at t = 0
  • \(w[n]\): The window value at tap n
  • \(I\): Input channel value
  • \(I_{\text{out}}\): Output channel value

The ideal low-pass filter has the sinc function as its taps, but they never end. The window w tapers them to N taps. Dividing by the sum makes the gain at frequency 0 exactly 1, so flat areas keep their value. The 2D filter applies the same taps along rows and columns.

The windows are the symmetric ones that scipy.signal.get_window gives, for \(n\) from 0 to \(N - 1\):

\[ \begin{aligned} w_{\text{Hamming}}[n] &= 0.54 - 0.46 \cos\frac{2\pi n}{N-1}, \\ w_{\text{Hann}}[n] &= 0.5 - 0.5 \cos\frac{2\pi n}{N-1}, \\ w_{\text{Blackman}}[n] &= 0.42 - 0.5 \cos\frac{2\pi n}{N-1} + 0.08 \cos\frac{4\pi n}{N-1}, \\ w_{\text{Kaiser}}[n] &= \frac{I_0\!\left(\beta \sqrt{1 - \left(\frac{2n}{N-1} - 1\right)^2}\right)}{I_0(\beta)} \end{aligned} \]

where \(N\) is the number of taps, \(\beta\) the Kaiser shape and \(I_0\) the modified Bessel function of the first kind of order 0. These are mathematical definitions. The code computes \(I_0\) from its power series.

The ideal sinc taps have a perfect cutoff but infinite length. Cutting them off sharply makes ripples near the cutoff, the Gibbs phenomenon. A window trades a slower transition for smaller ripples. Hamming has a narrow transition and moderate leakage. Blackman leaks less but changes more slowly. Kaiser lets you choose with \(\beta\). In the image, the ripples show as ringing: light and dark bands parallel to sharp edges.

Kernel or operator

The 1D taps for 9 taps, cutoff 0.2 and the Hamming window, from the filter code.

\[ h = \begin{bmatrix} -0.0061 & -0.0136 & 0.0512 & 0.2657 & 0.4057 & 0.2657 & 0.0512 & -0.0136 & -0.0061 \end{bmatrix}^{\mathsf{T}} \qquad (N = 9,\ f_c = 0.2,\ \text{Hamming}) \]

The 2D kernel is the outer product \(h\, h^{\mathsf{T}}\). The two outer taps on each side are negative.

Algorithm

  1. Compute the ideal sinc taps for the cutoff, centered on tap M.
  2. Multiply them by the chosen window.
  3. Divide the taps by their sum.
  4. Pad each row with the border mode and filter it with the taps, then do the same for each column.
  5. Clip to 0 to 255 and round to 8 bits. Keep alpha unchanged.

Parameters

Parameter Default Values Meaning
Taps taps 15 3 to 63, odd Number of 1D weights N. The window is N by N pixels.
Cutoff cutoff 0.15 0.01 to 0.5 cycles/px Frequency where the filter changes from keeping to removing. 0.5 is the finest pattern, one cycle per two pixels.
Window window Hamming Hamming, Hann, Blackman, Kaiser The taper applied to the ideal sinc taps.
Kaiser beta beta 5 0 to 14 Shape of the Kaiser window. Used only with the Kaiser window.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope RGB RGB, Brightness only, Color only Which part of the color image the filter changes.
Linear light linearLight off On or off Filter linear light values instead of sRGB encoded values.

Implementation

firTaps in web/src/core/filters/linear/fir-lowpass.ts builds the taps. The filter runs as two 1D passes with the shared correlation code. Some taps are negative, so alpha weighting, which needs positive weights, is off. Results past 0 or 255 are clipped.

Worked example

n 0 1 2 3 4 5 6 7 8
h[n] -0.0061 -0.0136 0.0512 0.2657 0.4057 0.2657 0.0512 -0.0136 -0.0061

Nine Hamming taps for the cutoff 0.2 cycles per pixel. They are symmetric around n = 4 and add up to 1, so flat areas keep their value. The outer taps are negative, which is where ringing comes from.

Complexity

  • Time: \(O(N_p\,N)\)
  • Memory: \(O(N_p)\)

N_p is the number of pixels per channel and N the number of taps. The filter is separable.

Artifacts and limitations

Limitations

  • The taps have negative lobes, so sharp edges ring.
  • A separable 2D filter cuts off along the axes, so diagonal frequencies are treated a little differently.

Artifacts

  • Ringing, light and dark bands parallel to sharp edges.
  • Clipping where the ringing pushes values past 0 or 255.

Tests

The reference taps come from scipy.signal.firwin with the cutoff given relative to the Nyquist frequency, for all four windows. The test compares the taps to 6 decimals and the filtered output with scipy.ndimage.correlate1d along both axes, for all five border modes.

References