Skip to content

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

Open
sohailshk wants to merge 1 commit into
mne-tools:mainfrom
sohailshk:fix/minphase-firwin2-highpass
Open

sohailshk wants to merge 1 commit into
mne-tools:mainfrom
sohailshk:fix/minphase-firwin2-highpass

Conversation

@sohailshk

Copy link
Copy Markdown

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

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