# ICA find\_bads\_ecg Function

**URL:** <https://mne.discourse.group/t/ica-find-bads-ecg-function/5876>\
**Category:** Support & Discussions\
**Tags:** preprocessing, eeg, ica\
**Created:** [November 4, 2022, 5:55pm UTC](https://mne.discourse.group/t/ica-find-bads-ecg-function/5876 "2022-11-04T17:55:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmctx13](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@lmctx13](https://mne.discourse.group/u/lmctx13)\
**Post date:** [November 4, 2022, 5:55pm UTC](https://mne.discourse.group/t/ica-find-bads-ecg-function/5876/1 "2022-11-04T17:55:38Z")

</div>

Hi everyone, I have been using the find\_bads\_ecg function to automatically identify ICA components with ECG artifacts. I noticed that the default frequency parameters were: 1) a lowpass frequency of 8 Hz and 2) a highpass frequency of 16 Hz.

I am curious as to how these default frequency parameters were chosen since the normal range for ECG waveforms is 0.5-3 Hz. I am also curious as to how these filters are being applied in order to identify ECG components.

Information:

- MNE version: 1.0.3
- operating system: Windows 11

I appreciate your help!

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [November 4, 2022, 10:37pm UTC](https://mne.discourse.group/t/ica-find-bads-ecg-function/5876/2 "2022-11-04T22:37:02Z")

</div>

This function starts by computing the source (IC) time series on epochs around ECG artifacts. Epochs centered around ECG events are created with [`mne.preprocessing.create_ecg_epochs`](https://mne.tools/dev/generated/mne.preprocessing.create_ecg_epochs.html). This is where the low and high pass frequencies are used.

I don’t exactly know how this is beneficial to QRS detection. Still, if you look at an ECG channel filtered between (8, 16) Hz, the ECG event location is apparent and most likely easy to detect with a peak detection algorithm.

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/a/ac816d01939b7b6f8c87bcaf5414835e75420122.png)

---

<div class="post-metadata">

**Author:** ![lmctx13](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@lmctx13](https://mne.discourse.group/u/lmctx13)\
**Post date:** [November 10, 2022, 3:40pm UTC](https://mne.discourse.group/t/ica-find-bads-ecg-function/5876/3 "2022-11-10T15:40:01Z")

</div>

Thank you for this explanation.

I attempted to filter an ECG channel between 8 and 16 Hz, and in my example, it looks like the peaks have been filtered out.

Before filtering:

 ![before_8_16_filtering](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/d/df46ba9d077e2cd586c97cc4ea1844938cc3a83a.png)

After filtering:

 ![after_8_16_filtering](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/0/08955b609b35bc43cd7eb187a33f92c05158c2ad.png)

Since these peaks have been filtered out, I am not sure if the detection of ECG events would be accurate. Would it be possible to try a different frequency range to ensure that these peaks do not get filtered out (and ECG events can be accurately detected)?

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [November 10, 2022, 4:20pm UTC](https://mne.discourse.group/t/ica-find-bads-ecg-function/5876/4 "2022-11-10T16:20:07Z")

</div>

Your plot looks exactly like mine, with this kind of 2 peak/1 peak ondulation. Honestly, I have no idea how the QRS detector implemented works, if you want to dig in, it’s define [here](https://github.com/mne-tools/mne-python/blob/4ef3a88c7c1ea29b4925cece2072ad5c5d826b82/mne/preprocessing/ecg.py#L21-L136).

Also, there is definitely weird lines and filter definition.

- The ECG events are found with [`find_ecg_events`](https://mne.tools/dev/generated/mne.preprocessing.find_ecg_events.html) which defaults to `l_freq=5, h_freq=35`.
- The ECG epochs are created with [`create_ecg_epochs`](https://mne.tools/dev/generated/mne.preprocessing.create_ecg_epochs.html) which defaults to `l_freq=8, h_freq=16` and calls [`find_ecg_events`](https://mne.tools/dev/generated/mne.preprocessing.find_ecg_events.html) with those different filter settings.
- [`find_ecg_events`](https://mne.tools/dev/generated/mne.preprocessing.find_ecg_events.html) calls the `qrs_detector` function with `l_freq=None, h_freq=None` despite this function having also `l_freq=5, h_freq=35` as defaults.

This last point makes sense because at this point the channel is already filtered, but then why do we have those 2 filter parameters at the `qrs_detector` level since they are not even used 😉

* * *

Regardless of this weirdness, let’s just test on an raw (unfiltered) ECG channel. I took one of my files recorded on an ANT Neuro ampifier with an ECG channel on the AUX8 channel:

```python
from matplotlib import pyplot as plt
from mne.io import read_raw_fif
from mne.preprocessing import find_ecg_events

raw = read_raw_fif(
    "my-raw-file-with-an-ECG-channel-raw.fif", preload=False
)
raw.pick_channels(["AUX8"])
raw.set_channel_types({"AUX8": "ecg"})
raw.rename_channels({"AUX8": "ECG"})
raw.crop(0, 30)
raw.load_data()

# default for find_ecg_events (5, 35) Hz bandpass
ecg_events1, _, _ = find_ecg_events(
    raw, ch_name="ECG", l_freq=5, h_freq=35
)
# default for create_ecg_epochs (8, 16) Hz bandpass
ecg_events2, _, _ = find_ecg_events(
    raw, ch_name="ECG", l_freq=8, h_freq=16
)

# plot
f, ax = plt.subplots(2, 1, sharex=True, sharey=True)
ax[0].plot(raw.get_data()[0, :])
ax[1].plot(raw.get_data()[0, :])

for event in ecg_events1:
    ax[0].axvline(event[0], color="teal", linestyle="--")
for event in ecg_events2:
    ax[1].axvline(event[0], color="teal", linestyle="--")
    
# format
ax[0].set_title("BP (5, 35) Hz")
ax[1].set_title("BP (8, 16) Hz")
f.tight_layout()

```

And here is the figure:

 ![Screenshot 2022-11-10 at 17.19.05](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/1/1d7ea9adf1df1018b0a5d166a08f69b8d9f0b235.png)

Looks like both filter yield a very accurate QRS detection. Again, I did not dig in on how the `qrs_detector` function actually works, but it works.
