Skip to content

Evaluate filter gain at the requested frequency - #729

Merged
tompollard merged 3 commits into
MIT-LCP:mainfrom
Kulraj69:fix-filter-gain-frequency
Nov 25, 2026
Merged

tompollard merged 3 commits into
MIT-LCP:mainfrom
Kulraj69:fix-filter-gain-frequency

Conversation

@Kulraj69

Copy link
Copy Markdown
Contributor

Problem

processing.get_filter_gain evaluates 512 default scipy.signal.freqz grid points, then takes the first point at or above the requested frequency. This rounds off-grid measurements upward. It also raises IndexError at Nyquist and sufficiently close below Nyquist because the default grid excludes that endpoint.

For example, a two-tap moving-average filter has analytic gain abs(cos(pi * f / fs)). Its gain at f = 0.1234 * fs should be measured there, rather than at the next grid point.

Change

Evaluate freqz at the requested angular frequency using worN=[w_gain]. Clarify the frequency units and Nyquist behavior in the docstring, and add a changelog fragment.

The 32 parameterized regression cases use analytic identity, moving-average, first-difference and first-order IIR responses at DC, off-grid frequencies and Nyquist for two sample rates. They also check that coefficient inputs are unchanged.

Validation

Against upstream 167f6714aac9b2a8b47bb11ff94c1e3782f02d3a:

  • Unpatched regression file: 22 failed, 10 passed.
  • Patched regression file plus tests/test_processing.py and tests/test_hr.py: 248 passed, with network tests excluded.
  • Ruff 0.17.3 check and format: passed for both changed Python files.
  • Sphinx 9.0.4 strict HTML build (-W --keep-going -E): passed.

Python 3.12.16. Full-repository tests, other dependency/Python versions and remote CI were not run. This was prepared and tested with AI assistance, without independent human review.

This intentionally replaces historical grid rounding with exact-frequency evaluation, so callers using the gain for normalization can see small numerical differences. No new validation contract for negative, nonfinite or above-Nyquist inputs is introduced. No clinical validation is claimed.

Add tests for filter gain at various frequencies and coefficients.
Updated filter gain evaluation to handle frequencies more accurately, fixing an IndexError at the Nyquist frequency.

@tompollard tompollard left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @Kulraj69! I reviewed the exact-frequency evaluation and its tests. This fixes the off-grid gain error and the Nyquist failure. The relevant offline tests passed locally (248 passed), and all PR checks are green. Looks good to me.

@tompollard
tompollard merged commit d6e0bb4 into MIT-LCP:main Nov 25, 2026
19 checks passed
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.

2 participants