Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Comment thread
gabrielfrasantos marked this conversation as resolved.
Resolved
Comment thread
gabrielfrasantos marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Refer to the documentation to quickly integrate and utilize the library's signal
| [Controllers](doc/controllers/README.md) | Bang-Bang/Hysteresis, PID, LQR, MPC, Saturation, Rate Limiter, Slew-Limited Saturation, Feedforward/2-DOF, Gain-Scheduled Controller |
| [Dynamics](doc/dynamics/README.md) | Euler-Lagrange, Newton-Euler, Recursive Newton-Euler, ABA |
| [Estimators](doc/estimators/README.md) | Linear Regression, Polynomial Fitting, Yule-Walker (offline), Recursive Least Squares (online) |
| [Filters](doc/filters/README.md) | Kalman, Extended Kalman, Unscented Kalman, Alpha-Beta/Alpha-Beta-Gamma, FIR, IIR, Exponential Moving Average, Moving Average, Complementary, Median Filter, CIC (Cascaded Integrator-Comb) |
| [Filters](doc/filters/README.md) | Kalman, Extended Kalman, Unscented Kalman, Alpha-Beta/Alpha-Beta-Gamma, FIR, IIR, Exponential Moving Average, Moving Average, Complementary, Median Filter, CIC (Cascaded Integrator-Comb), Notch/Comb Filter |
| [Kinematics](doc/kinematics/README.md) | Forward Kinematics |
| [Neural Network](doc/neural_network/README.md) | Layers, activations, losses, model |
| [Optimization](doc/optimization/README.md) | Gradient Descent |
Expand Down
1 change: 0 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Difficulty legend:
| # | Component | Target module | Difficulty |
|----|------------------------------------------------------|---------------------------|------------|
| 15 | Biquad / Second-Order-Section cascade | `filters/passive` | ★★★☆☆ |
| 16 | Notch / comb filter | `filters/passive` | ★★★☆☆ |
| 17 | Lead-lag compensator | `controllers` | ★★★☆☆ |
| 18 | Quaternion type | `math` | ★★★☆☆ |
| 19 | Luenberger observer + pole placement (Ackermann) | `controllers` | ★★★☆☆ |
Expand Down
120 changes: 120 additions & 0 deletions doc/filters/passive/NotchCombFilter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Notch / Comb Filter

## Overview & Motivation

Narrow-band interference — mains hum at 50/60 Hz and its harmonics — contaminates bio-signal
acquisition (ECG, EEG, EMG) and precision instrumentation. A notch filter surgically eliminates
a single frequency while leaving the rest of the spectrum intact. A comb filter extends this idea
to an entire harmonic series using a single delay line, making it far more efficient than cascading
individual notches when the interference is periodic.

## Mathematical Theory

### Notch Filter (RBJ Biquad)

The continuous-time transfer function for a band-reject filter with centre frequency $f_0$ and
quality factor $Q$ is:

$$H(s) = \frac{s^2 + \omega_0^2}{s^2 + \frac{\omega_0}{Q}s + \omega_0^2}$$

Applying the bilinear transform with $\omega_0 = 2\pi f_0 / f_s$, the discrete-time biquad
coefficients (Bristow-Johnson "Cookbook" notch) are:

$$\alpha = \frac{\sin(\omega_0)}{2Q}, \quad c_\omega = \cos(\omega_0)$$

$$b_0 = b_2 = \frac{1}{1+\alpha}, \quad b_1 = \frac{-2c_\omega}{1+\alpha}$$

$$a_1 = \frac{-2c_\omega}{1+\alpha}, \quad a_2 = \frac{1-\alpha}{1+\alpha}$$

The zeros lie exactly on the unit circle at $e^{\pm j\omega_0}$, guaranteeing infinite attenuation
at $f_0$ in exact arithmetic.

Transposed Direct Form II (TDF-II) realises this with two state variables $z_1, z_2$:

$$y[n] = b_0 x[n] + z_1[n-1]$$
$$z_1[n] = b_1 x[n] - a_1 y[n] + z_2[n-1]$$
$$z_2[n] = b_2 x[n] - a_2 y[n]$$

### Comb Filter

A comb filter exploits the identity: subtracting (or adding) a signal delayed by $D$ samples
creates periodic nulls (or peaks) spaced every $f_s / D$ Hz:

**Feedforward (FIR) comb:**

$$y[n] = x[n] - g \cdot x[n-D]$$

Zeros at $\omega = 2\pi k / D$ for $k = 0, 1, \ldots, D-1$. The system is always stable.

**Feedback (IIR) comb:**

$$y[n] = x[n] + g \cdot y[n-D]$$

Poles at the same locations. The system is stable if and only if $|g| < 1$.

## Complexity Analysis

| Case | Time | Space | Notes |
|-------|--------|--------|-----------------------------------|
| Notch | $O(1)$ | $O(1)$ | Fixed biquad; 2 multiplies/adds |
| Comb | $O(1)$ | $O(D)$ | Single circular-buffer read/write |

## Step-by-Step Walkthrough

**Notch example** — $f_0 = 50$ Hz, $f_s = 1000$ Hz, $Q = 10$:

1. $\omega_0 = 2\pi \cdot 0.05 \approx 0.3142$
2. $c_\omega = \cos(0.3142) \approx 0.9511$, $\alpha = \sin(0.3142)/(20) \approx 0.01564$
3. $a_0 = 1.01564$; divide all coefficients by $a_0$
4. $b_0 = b_2 \approx 0.9846$, $b_1 = a_1 \approx -1.8730$, $a_2 \approx 0.9693$
5. At each sample: compute $y = b_0 x + z_1$, then update $z_1, z_2$.

**Comb example** — $D = 4$, $g = 1$ (FIR):

Impulse response: $[1, 0, 0, 0, -1, 0, 0, 0, 0, \ldots]$ — zeros at 0, fs/4, fs/2, 3fs/4.

## Pitfalls & Edge Cases

- **High Q instability**: a very narrow notch ($Q \gg 10$) places poles close to the unit circle.
Finite-precision arithmetic can shift them outside, causing instability. Use double precision or
second-order-section cascades when $Q > 30$.
- **Detuned comb**: if $f_s$ is not an exact integer multiple of the harmonic spacing, the comb
nulls misalign and leave residual hum. Prefer a sample rate that divides evenly, or cascade
individual biquad notches.
- **Feedback comb stability**: gain must satisfy $|g| < 1$ with margin. Values near 1 produce
extremely long ring-down and amplify quantisation noise.
- **DC gain**: the RBJ notch has unity gain at DC and Nyquist; FIR comb has gain $(1 - g)$ at DC.

## Variants & Generalizations

- **Notch with adjustable bandwidth**: replace the fixed $Q$ with an adaptive estimator tracking
the instantaneous interference frequency (e.g., a phase-locked loop).
- **All-pass + notch cascade**: derive a steep-skirt band-stop by cascading a notch with an
all-pass tuned to the same frequency.
- **Comb as reverberator**: adding (instead of subtracting) a delayed, attenuated copy produces
artificial reverberation (Schroeder comb).
- **Multi-rate comb**: operate the comb at a lower decimated rate, then interpolate — reduces
delay-line length proportionally.

## Applications

- Bio-signal acquisition: removing 50/60 Hz mains hum and harmonics from ECG/EEG/EMG.
- Power-line harmonic suppression in current/voltage sensors.
- Audio effects: chorus, flange, reverberation.
- Synchronous sampling: align comb nulls with switching-converter ripple harmonics.

## Connections to Other Algorithms

- **BiquadCascade** — the notch is a single biquad section; a cascade realizes higher-order
band-stop designs with better numerical conditioning.
- **IirFilterDesign** — a band-stop Butterworth/Chebyshev design from a bilinear transform
produces a family of notches; the RBJ notch here is the minimal single-section special case.
- **GoertzelAlgorithm** — detects the frequency that the notch removes; useful for adaptive
centre-frequency tuning.

## References & Further Reading

- R. Bristow-Johnson, "Cookbook formulae for audio EQ biquad filter coefficients",
<https://www.w3.org/TR/audio-eq-cookbook/>
- R. G. Lyons, *Understanding Digital Signal Processing*, 3rd ed., Prentice Hall, 2011, Ch. 6.
- A. V. Oppenheim & R. W. Schafer, *Discrete-Time Signal Processing*, 3rd ed., Ch. 5.
2 changes: 2 additions & 0 deletions numerical/filters/passive/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_sources(numerical.filters.passive PRIVATE
Iir.hpp
MedianFilter.hpp
MovingAverage.hpp
NotchCombFilter.hpp
)

numerical_add_coverage_sources(numerical.filters.passive
Expand All @@ -26,6 +27,7 @@ numerical_add_coverage_sources(numerical.filters.passive
Iir.cpp
MedianFilter.cpp
MovingAverage.cpp
NotchCombFilter.cpp
)

add_subdirectory(test)
8 changes: 8 additions & 0 deletions numerical/filters/passive/NotchCombFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "numerical/filters/passive/NotchCombFilter.hpp"

namespace filters::passive
{
template class NotchFilter<float>;
template class CombFilter<float, 20, false>;
template class CombFilter<float, 20, true>;
}
128 changes: 128 additions & 0 deletions numerical/filters/passive/NotchCombFilter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#pragma once

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC optimize("O3", "fast-math")
#endif

#include "numerical/math/CompilerOptimizations.hpp"
#include <array>
#include <cmath>
#include <cstddef>
#include <numbers>
#include <type_traits>

namespace filters::passive
{
template<typename T>
class NotchFilter
{
static_assert(std::is_floating_point_v<T>, "NotchFilter supports floating-point types");

public:
NotchFilter(T f0, T fs, T Q) noexcept;

OPTIMIZE_FOR_SPEED T Filter(T x) noexcept;
void Reset() noexcept;

private:
T b0{};
T b1{};
T b2{};
T a1{};
T a2{};
T z1{};
T z2{};
};

template<typename T, std::size_t D, bool Feedback = false>
class CombFilter
{
static_assert(std::is_floating_point_v<T>, "CombFilter supports floating-point types");
static_assert(D > 0, "CombFilter delay length must be greater than zero");

public:
explicit CombFilter(T gain) noexcept;

OPTIMIZE_FOR_SPEED T Filter(T x) noexcept;
void Reset() noexcept;

private:
T gain{};
std::array<T, D> delayLine{};
std::size_t writeIndex{};
};

//// NotchFilter Implementation ////

template<typename T>
NotchFilter<T>::NotchFilter(T f0, T fs, T Q) noexcept
{
const T w0{ T{ 2 } * std::numbers::pi_v<T> * f0 / fs };
const T cw{ std::cos(w0) };
const T alpha{ std::sin(w0) / (T{ 2 } * Q) };
const T a0{ T{ 1 } + alpha };
b0 = T{ 1 } / a0;
b1 = (T{ -2 } * cw) / a0;
b2 = T{ 1 } / a0;
a1 = (T{ -2 } * cw) / a0;
a2 = (T{ 1 } - alpha) / a0;
}

template<typename T>
OPTIMIZE_FOR_SPEED T NotchFilter<T>::Filter(T x) noexcept
{
const T y{ b0 * x + z1 };
z1 = b1 * x - a1 * y + z2;
z2 = b2 * x - a2 * y;
return y;
}

template<typename T>
void NotchFilter<T>::Reset() noexcept
{
z1 = T{};
z2 = T{};
}

//// CombFilter Implementation ////

template<typename T, std::size_t D, bool Feedback>
CombFilter<T, D, Feedback>::CombFilter(T gain) noexcept
: gain{ gain }
, writeIndex{ 0 }
{
delayLine.fill(T{});
}

template<typename T, std::size_t D, bool Feedback>
OPTIMIZE_FOR_SPEED T CombFilter<T, D, Feedback>::Filter(T x) noexcept
{
const T d{ delayLine[writeIndex] };
T y{};
if constexpr (!Feedback)
{
y = x - gain * d;
delayLine[writeIndex] = x;
}
else
{
y = x + gain * d;
delayLine[writeIndex] = y;
}
writeIndex = (writeIndex + 1) % D;
return y;
}

template<typename T, std::size_t D, bool Feedback>
void CombFilter<T, D, Feedback>::Reset() noexcept
{
delayLine.fill(T{});
writeIndex = 0;
}

#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD
extern template class NotchFilter<float>;
extern template class CombFilter<float, 20, false>;
extern template class CombFilter<float, 20, true>;
#endif
}
1 change: 1 addition & 0 deletions numerical/filters/passive/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ target_sources(numerical.filters.passive_test PRIVATE
TestIir.cpp
TestMedianFilter.cpp
TestMovingAverage.cpp
TestNotchCombFilter.cpp
)
Loading
Loading