Skip to content

Fix spectrum_fit frequency detection and removal - #14310

Merged
larsoner merged 2 commits into
mne-tools:mainfrom
larsoner:spectrum
Sep 23, 2026
Merged

larsoner merged 2 commits into
mne-tools:mainfrom
larsoner:spectrum

Conversation

@larsoner

Copy link
Copy Markdown
Member

I've rarely had satisfying results with spectrum_fit, and I finally dug into why. When the line freq was exactly on a freq bin (e.g., 1000 Hz data and 60.000 Hz line noise) things were at least okay. but if it was slightly off, like 60.05 line noise, it would fail to detect properly. If it did detect properly, it would fail to remove properly because of bugs in how we removed using adjacent bins. We should have refined the frequency estimate after finding there was a significant peak.

This code adapts some nitime code for deciding which components to choose, and refines the frequencies using a chirp z-transform (rather than zero-padding that nitime uses, which is a less clean / complete way to achieve a similar effect).

Updated tutorial image:

image

On main the 60 Hz harmonics are over-removed:

image

@payamsash I'd suggest running spectrum_fit mode on your data to see if it detects frequencies at 25 Hz etc. It would strongly suggest that these are subharmonics of the 50 Hz line noise (could be due to aliasing or something else).

Code changes drafted using Claude Fable 5.1 and reviewed / iterated on by me.

@payamsash

Copy link
Copy Markdown
Member

with quick plotting with AI, clearly now it works better. thanks @larsoner
09_spectrum_fit_old_vs_new

PS: looks like the reason for these artifacts was that amplifier was plugged during the recording.

@larsoner
larsoner enabled auto-merge (squash) September 23, 2026 22:20
@larsoner
larsoner merged commit 5f7c06d into mne-tools:main Sep 23, 2026
25 checks passed
@larsoner
larsoner deleted the spectrum branch September 23, 2026 23:07
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