From b2678af5710c69570ebd7f598d45f98eee10bfcd Mon Sep 17 00:00:00 2001 From: sohailshk <122166523+sohailshk@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:55:58 +0530 Subject: [PATCH 1/2] FIX: Make minimum-phase firwin2 high-pass filters work with even filter lengths --- doc/changes/dev/14627.bugfix.rst | 1 + doc/changes/names.inc | 1 + mne/filter.py | 2 +- mne/tests/test_filter.py | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 doc/changes/dev/14627.bugfix.rst diff --git a/doc/changes/dev/14627.bugfix.rst b/doc/changes/dev/14627.bugfix.rst new file mode 100644 index 00000000000..2ea657c29cd --- /dev/null +++ b/doc/changes/dev/14627.bugfix.rst @@ -0,0 +1 @@ +Fix :func:`mne.filter.create_filter` and :meth:`mne.io.Raw.filter` raising a ``ValueError`` for high-pass and band-stop filters with ``phase="minimum"`` and ``fir_design="firwin2"`` when the filter length is even, by :newcontrib:`Sohail Shaikh`. diff --git a/doc/changes/names.inc b/doc/changes/names.inc index eae29a6b676..71adf536fcf 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -442,6 +442,7 @@ .. _Simon M. Hofmann: https://github.com/SHEscher .. _Simon-Shlomo Poil: https://github.com/simon-shlomo .. _Sina Esmaeili: https://github.com/snesmaeili +.. _Sohail Shaikh: https://github.com/sohailshk .. _Soham Mitra: https://github.com/SohamXYZDev .. _Sondre Foslien: https://github.com/sondrfos .. _Sophie Herbst: https://github.com/SophieHerbst diff --git a/mne/filter.py b/mne/filter.py index e02e8687582..50567082c6e 100644 --- a/mne/filter.py +++ b/mne/filter.py @@ -523,7 +523,7 @@ def _check_zero_phase_length(N, phase, gain_nyq=0): if N % 2 == 0: if phase == "zero": raise RuntimeError(f'filter_length must be odd if phase="zero", got {N}') - elif phase == "zero-double" and gain_nyq == 1: + elif phase in ("zero-double", "minimum") and gain_nyq == 1: N += 1 return N diff --git a/mne/tests/test_filter.py b/mne/tests/test_filter.py index bb0fcf35692..45b026880b1 100644 --- a/mne/tests/test_filter.py +++ b/mne/tests/test_filter.py @@ -1160,6 +1160,24 @@ def test_filter_minimum_phase_bug(): assert not (dB_min_half < -30).all() +@pytest.mark.parametrize("filter_length", (3000, "3s")) +def test_filter_minimum_phase_firwin2_highpass(filter_length): + """Test minimum-phase firwin2 high-pass with an even filter length.""" + h = create_filter( + None, + 1000.0, + 10.0, + None, + filter_length=filter_length, + phase="minimum", + fir_design="firwin2", + ) + assert h.size % 2 == 1 + w, H = freqz(h, worN=2000, fs=1000.0) + assert_allclose(np.abs(H[-1]), 1.0, atol=1e-2) + assert np.abs(H[0]) < 1e-2 + + @pytest.mark.parametrize("dc", (0, 100)) @pytest.mark.parametrize("sfreq", (1000.0, 999.0)) def test_smart_pad(dc, sfreq): From a11f5035b9a03c3eafea817963587b096ba3a42d Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 2 Dec 2026 10:47:25 -0500 Subject: [PATCH 2/2] Shorter test --- mne/tests/test_filter.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/mne/tests/test_filter.py b/mne/tests/test_filter.py index 45b026880b1..a79f68807b8 100644 --- a/mne/tests/test_filter.py +++ b/mne/tests/test_filter.py @@ -1135,6 +1135,10 @@ def test_filter_minimum_phase_bug(): h_min = create_filter(phase="minimum", **kwargs) h_min_half = create_filter(phase="minimum-half", **kwargs) assert h_min.size == h.size + # even length with firwin2 and unit gain at Nyquist must be made odd (gh-14627) + kwargs["filter_length"] = n_taps - 1 + h_even = create_filter(phase="minimum", fir_design="firwin2", **kwargs) + assert h_even.size == n_taps kwargs = dict(worN=10000, fs=sfreq) w, H = freqz(h, **kwargs) assert w[0] == 0 @@ -1160,24 +1164,6 @@ def test_filter_minimum_phase_bug(): assert not (dB_min_half < -30).all() -@pytest.mark.parametrize("filter_length", (3000, "3s")) -def test_filter_minimum_phase_firwin2_highpass(filter_length): - """Test minimum-phase firwin2 high-pass with an even filter length.""" - h = create_filter( - None, - 1000.0, - 10.0, - None, - filter_length=filter_length, - phase="minimum", - fir_design="firwin2", - ) - assert h.size % 2 == 1 - w, H = freqz(h, worN=2000, fs=1000.0) - assert_allclose(np.abs(H[-1]), 1.0, atol=1e-2) - assert np.abs(H[0]) < 1e-2 - - @pytest.mark.parametrize("dc", (0, 100)) @pytest.mark.parametrize("sfreq", (1000.0, 999.0)) def test_smart_pad(dc, sfreq):