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.
How it works¶
- Start from the perfect low-pass filter. Its taps follow a sinc wave that never ends.
- Cut the wave to the chosen number of taps and taper its ends with a window.
- Divide the taps by their sum, so flat areas keep their value.
- Filter every row with the taps, then every column.
Mathematical foundation¶
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\):
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.
The 2D kernel is the outer product \(h\, h^{\mathsf{T}}\). The two outer taps on each side are negative.
Algorithm¶
- Compute the ideal sinc taps for the cutoff, centered on tap M.
- Multiply them by the chosen window.
- Divide the taps by their sum.
- Pad each row with the border mode and filter it with the taps, then do the same for each column.
- 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.
web/src/core/filters/filters.test.tsweb/src/core/filters/linear/linear5a.test.tsweb/src/core/filters/run.test.ts
References¶
- Oppenheim, A. V. and Schafer, R. W. Discrete-Time Signal Processing, 3rd edition. Pearson, 2010. Chapter 7.
- SciPy documentation, scipy.signal.firwin. https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.firwin.html