Skip to content

Change the default window length to 5 s - #22

Closed
mikech76 wants to merge 2 commits into
cbrnr:mainfrom
mikech76:default-window-5
Closed

mikech76 wants to merge 2 commits into
cbrnr:mainfrom
mikech76:default-window-5

Conversation

@mikech76

Copy link
Copy Markdown

This changes the default window_length of remove_line_noise() from 10 s to 5 s.

Why. A shorter window follows line-frequency drift better. On a 64-channel, 20-minute recording with a line that drifts from 60.00 to 59.97 Hz, the residual line power with a neighbouring tone was:

window / overlap line power left
10 s / 0.5 (old default) 4.0e-04
5 s / 0.5 1.1e-04
3 s / 0.75 1.0e-04
2 s / 0.75 1.1e-04
5 s / 0.75 3.5e-04

Why 5 s and not 3 s. 3 s is fine on that recording, but it makes two of the existing tracking tests fail: test_tracking_matches_fit_at_true_frequency_with_nearby_signals and test_wandering_frequency_is_tracked both compare against a fit at the true frequency and need the longer window for that precision. Those tests now pass window_length=10 explicitly, so they keep testing what they were written to test, and 5 s is the shortest default that keeps them passing without changes to their assertions.

Why not shorter than 2 s. Below roughly 2 s the filter stops separating the line from real activity next to it. With a 5 µV tone 0.6 Hz away, a 0.5 s window keeps only 14 % of the tone and a 1 s window 46 %, while 2 s keeps 100 %. The new test covers exactly this: it checks that the line is largely removed and that the neighbouring tone keeps its amplitude.

Changes

  • window_length default 10 s -> 5 s, and the docstring now says why the default is not shorter.
  • New test test_default_window_preserves_a_tone_next_to_the_line.
  • The two tracking tests pin window_length=10 so their comparison stays meaningful.
  • Changelog entry under ### 🌀 Changed.

ruff check, ruff format --check and pytest -W error tests/test_line_noise.py all pass locally (39 tests).

@cbrnr

cbrnr commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Thanks for the PR. I looked into this in more detail, and I'd like to keep the 10 s default.

I simulated your scenario (64 channels, 20 minutes, line drifting from 60.00 to 59.97 Hz, plus a neighboring tone) and measured the error against the known clean signal. In that scenario, 10 s gives the lowest error, and the error roughly doubles each time the window is halved:

10 s / 0.5 5 s / 0.5 3 s / 0.75 2 s / 0.75
drift + tone 1.5e-13 2.9e-13 4.3e-13 6.9e-13
no drift, no tone 1.4e-13 2.8e-13 4.3e-13 6.5e-13

Drift barely matters because the frequency is re-estimated in every window. Within a 10 s window, a drift of 0.03 Hz over 20 minutes amounts to only about 0.00025 Hz. Shorter windows, however, have fewer samples per fitted component, so more broadband activity near the line frequencies is removed along with the line. Shorter windows do help when the line frequency or amplitude changes within a few seconds, but that isn't the case in this scenario, and I don't think we have evidence that it's typical for real recordings.

@cbrnr cbrnr closed this Dec 2, 2026
@mikech76

mikech76 commented Dec 2, 2026

Copy link
Copy Markdown
Author

Thanks for taking the time to test this against the known clean signal. Your results make sense, and I agree that they don't support changing the default to 5 s.

I wanted to share a small browser-based lab I built while working on this algorithm:

[EEG line-noise removal demo](https://mikech76.github.io/mne-python/)

One useful feature is that you can change the window length and overlap yourself (for example, compare 5 s and 10 s windows) and rerun the filter on the same recording. The updated signal and spectrum are displayed immediately, making it easy to explore how different settings affect line-noise removal and nearby signal components.

I also posted some implementation details and numerical comparisons with mnextend here:

[MNE Forum: Line-noise removal in the browser](https://mne.discourse.group/t/line-noise-removal-in-the-browser-a-demo-and-how-close-it-is-to-mnextend/12061)

The idea isn't to argue for a 5 s default, but to provide a convenient way to experiment with the window-length trade-off on different recordings.

Perhaps it could be useful for exploring this behavior.

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