Skip to content

FIX: Make minimum-phase firwin2 high-pass filters work with even filter lengths - #14627

Merged
larsoner merged 2 commits into
mne-tools:mainfrom
sohailshk:fix/minphase-firwin2-highpass
Dec 2, 2026
Merged

larsoner merged 2 commits into
mne-tools:mainfrom
sohailshk:fix/minphase-firwin2-highpass

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

Reference issue (if any)

None found (searched for firwin2, minimum phase, and Type II filter).

What does this implement/fix?

Fixes ValueError: A Type II filter must have zero gain at the Nyquist frequency from scipy.signal.firwin2 when creating or applying minimum-phase (phase="minimum") firwin2 high-pass or band-stop filters with an even filter length.

Reproduction examples:

  • raw.filter(1., None, phase="minimum", fir_design="firwin2") on a 100 Hz recording
  • mne.filter.create_filter(None, 1000., 10., None, filter_length=3000, phase="minimum", fir_design="firwin2") (and filter_length="3s")

Cause & regression history:

  • Regression since MNE 1.7 (PR BUG: Fix bug with minimum phase filters #12507, merged 2024-03-19, changed phase="minimum" from N*2 - 1 taps to N taps). phase="minimum-half" still works for these calls.
  • In mne/filter.py (_check_zero_phase_length, around line 526), phase == "zero-double" increments even filter lengths by 1 when gain at Nyquist is 1, while phase == "minimum" did not.
  • Updating the check to phase in ("zero-double", "minimum") adds 1 tap when needed.

Additional information

  • Found via a differential sweep over MNE filter designs (124 of 585 grid specs failed due to landing on an even filter length).
  • Added test_filter_minimum_phase_firwin2_highpass[3000|3s] in mne/tests/test_filter.py next to test_filter_minimum_phase_bug (2 failed before the fix; test_filter.py passed 294 with 4 skipped after the fix).
  • ruff check and ruff format pass.
  • Changelog entry prepared for doc/changes/dev/.bugfix.rst crediting Sohail Shaikh (:newcontrib:Sohail Shaikh).
  • AI/tooling disclosure: Implementation and tests were written with AI assistance only and every other thing is human reviewed.

@welcome

welcome Bot commented Dec 2, 2026

Copy link
Copy Markdown

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

@sohailshk
sohailshk force-pushed the fix/minphase-firwin2-highpass branch from d3353a2 to b2678af Compare December 2, 2026 06:35

@larsoner larsoner 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.

Makes sense, thanks @sohailshk !

@larsoner
larsoner enabled auto-merge (squash) December 2, 2026 15:47
@larsoner
larsoner disabled auto-merge December 2, 2026 15:47
@larsoner
larsoner enabled auto-merge (squash) December 2, 2026 15:48
@larsoner
larsoner disabled auto-merge December 2, 2026 16:21
@larsoner
larsoner merged commit 8a14a2c into mne-tools:main Dec 2, 2026
25 of 26 checks passed
@welcome

welcome Bot commented Dec 2, 2026

Copy link
Copy Markdown

🎉 Congrats on merging your first pull request! 🥳 Looking forward to seeing more from you in the future! 💪

larsoner added a commit to larsoner/mne-python that referenced this pull request Dec 2, 2026
* upstream/main:
  FIX: Limit NaN propagation during overlap-add FIR filtering (mne-tools#14614)
  ENH: Add save() to ConductorModel and InverseOperator (mne-tools#14612)
  FIX: Make minimum-phase firwin2 high-pass filters work with even filter lengths (mne-tools#14627)
  DOC: Note that cap montages do not line up with fsaverage in template-MRI tutorial (mne-tools#14628)
  DOC: Say adjacency=None is rarely right for sensor or source data in cluster tests (mne-tools#14629)
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