Skip to content

Use mnextend line noise method for ~50x speed #213

Open

Description

@sappelhoff

to replace

def remove_line_noise(self, line_freqs=None):
"""Remove line noise from all EEG channels.
Line noise is removed by detrending the signal, applying a notch filter,
and adding the slow drifts back. By default the notch filter uses MNE's
``spectrum_fit`` method, which attempts to isolate and remove line noise
while preserving unrelated background signal in the same frequency ranges
(to minimize distortions in the power-spectral density). The filter can be
configured via the ``filter_kwargs`` argument of :class:`PrepPipeline`.
Parameters
----------
line_freqs : {np.ndarray, list, None}, optional
A list of the frequencies (in Hz) at which line noise should be removed
(e.g., ``np.arange(60, sfreq / 2, 60)`` for a recording with a powerline
noise of 60 Hz). If ``None`` (default), the ``"line_freqs"`` entry of the
``prep_params`` passed to :class:`PrepPipeline` is used.
"""
if line_freqs is None:
line_freqs = self.prep_params["line_freqs"]
# Remove slow drifts from the recording prior to filtering
self.EEG_new = removeTrend(
self.EEG_raw, self.sfreq, matlab_strict=self.matlab_strict
)
# Remove line noise. When no filter kwargs are given, fall back to PREP's
# default ``spectrum_fit`` settings; otherwise use the provided kwargs as-is.
if self.filter_kwargs is None:
self.EEG_clean = mne.filter.notch_filter(
self.EEG_new,
Fs=self.sfreq,
freqs=line_freqs,
method="spectrum_fit",
mt_bandwidth=2,
p_value=0.01,
filter_length="10s",
)
else:
self.EEG_clean = mne.filter.notch_filter(
self.EEG_new,
Fs=self.sfreq,
freqs=line_freqs,
**self.filter_kwargs,
)
# Add the slow drifts back
self.EEG = self.EEG_raw - self.EEG_new + self.EEG_clean
self.raw_eeg._data = self.EEG
self._line_noise_removed = True

cc @cbrnr

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions