Skip to content

feat(utils): add first-order low-pass filter - #373

Open
molqzone wants to merge 1 commit into
xrobot-org:devfrom
molqzone:feat/low-pass-filter
Open

molqzone wants to merge 1 commit into
xrobot-org:devfrom
molqzone:feat/low-pass-filter

Conversation

@molqzone

Copy link
Copy Markdown
Contributor

Adds a first-order low-pass filter to src/utils. Header-only, no allocation,
no exceptions or RTTI.

The filter stores the time constant and advances with the sample interval:
alpha = dt / (tau + dt). SetCutoffFrequency and SetAlpha convert a cutoff
or a coefficient into the time constant, so the rate dependence of a
coefficient stays visible at the call site. There is no default configuration,
so an unconfigured filter cannot exist.

The first sample becomes the state; Reset() clears it. T can be a scalar or
an Eigen vector, whose coefficient type is deduced from the matrix type.

test/automatic/utils/lpfilter covers the step response, dt <= 0, Reset,
the two conversions, SetParam, and an Eigen vector.

New files only: src/utils/lpfilter.hpp and its test.

@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Low impact] The change adds an optional utility without changing existing workflows.

Fix the cutoff conversion and its test before merging.

Fix All in Claude CodeFindings

  1. P1 Cutoff settings smooth too little ▶
Fix with agent prompt
### Issue 1
src/utils/lpfilter.hpp:93-94
`SetCutoffFrequency` computes `1 / (4πf)` because `TWO_PI` already equals `2π`. Calling `SetCutoffFrequency(40)` sets a time constant of about 0.001989 seconds instead of 0.003979 seconds. The filter responds faster and passes more noise than requested.

Remove the extra `Scalar(2)` and fix the test, which repeats the same formula.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds a header-only first-order low-pass filter for scalars and Eigen vectors.

  • LPFilter smooths timed scalar and vector samples into a running state.

Reviews (1) · Last reviewed commit: "feat(utils): add first-order low-pass fi..." · Reviewed by Greptile

Comment thread src/utils/lpfilter.hpp
Comment on lines +93 to +94
param_.time_constant =
Scalar(1) / (Scalar(2) * static_cast<Scalar>(TWO_PI) * cutoff_frequency);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Cutoff settings smooth too little

SetCutoffFrequency computes 1 / (4πf) because TWO_PI already equals 2π. Calling SetCutoffFrequency(40) sets a time constant of about 0.001989 seconds instead of 0.003979 seconds. The filter responds faster and passes more noise than requested.

Remove the extra Scalar(2) and fix the test, which repeats the same formula.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/utils/lpfilter.hpp
Line: 93-94

Comment:
**Cutoff settings smooth too little**

`SetCutoffFrequency` computes `1 / (4πf)` because `TWO_PI` already equals `2π`. Calling `SetCutoffFrequency(40)` sets a time constant of about 0.001989 seconds instead of 0.003979 seconds. The filter responds faster and passes more noise than requested.

Remove the extra `Scalar(2)` and fix the test, which repeats the same formula.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Low-pass filtering was only available through the verdancy-org/LPFilter
module, so a module that needs it pulls a repository dependency for twenty
lines of arithmetic. Add the filter to src/utils, next to PID and the
transform utilities.

The filter follows the PID shape: a Param struct holding the time constant,
a constructor that takes it, SetParam for a later change, one Reset(), and
no default configuration, so an unconfigured filter cannot exist. It
advances with the sample interval: alpha = dt / (tau + dt). SetCutoffFrequency
and SetAlpha convert a cutoff or a coefficient into the time constant,
keeping the rate dependence of a coefficient visible at the call site.

The first sample becomes the state, so callers never track initialization.
The filter allocates nothing and needs no exceptions or RTTI.

test/automatic/utils/low_pass_filter covers the step response, dt <= 0,
Reset, the two conversions, SetParam, and an Eigen vector, whose
coefficient type is deduced from T.
@molqzone
molqzone force-pushed the feat/low-pass-filter branch from 75dd0c4 to 89f242a Compare November 21, 2026 14:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant