# Failed to know how to apply a stopband over a passband

**URL:** <https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187>\
**Category:** Support & Discussions\
**Tags:** preprocessing, eeg\
**Created:** [August 8, 2024, 4:54pm UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187 "2024-08-08T16:54:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yih](https://avatars.discourse-cdn.com/v4/letter/y/3d9bf3/32.png) [@Yih](https://mne.discourse.group/u/Yih)\
**Post date:** [August 8, 2024, 4:54pm UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/1 "2024-08-08T16:54:52Z")

</div>

**❓ If you have a question or issue with MNE-Python, please include the following info:**

- MNE version: 1.7.1
- operating system: Windows 10

Hi,

I am new to MNE-python and I apologise if this is an elementary question. I am trying to apply a stopband over a passband. I want to achieve this:

> Continuous EEG signals were bandpass filtered (standard non-causal two-pass Butterworth filters) between 0.1 and 100 Hz and bandstop filtered (48–52 and 98–102 Hz) to remove line noise at 50 and 100 Hz.

What I planned to do was first do the bandpass filter and then the two bandstop filter. Then I tried to use the following code:

```python
raw_eeg_subj2_bandstop = raw_eeg_subj2.get_data().filter(0.1, 100).filter(52, 48)

```

or tried to create a bandpass filter like:

```python
stopband1 = mne.filter.create_filter(raw_eeg_subj2.get_data(), 512, 52, 48)

```

However, they didn’t work and returned the message:

> Stop bands are not sufficiently separated.

I was wondering what was wrong and hope to ask about how to achieve 0.1-100Hz without 48-52Hz and 98-102Hz.

Thank you very much! 😃

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 9, 2024, 6:11am UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/2 "2024-08-09T06:11:39Z")

</div>

I recommend that you use `raw.filter()` directly, there should be no need for `.get_data()`. I’d also apply the three filters sequentially. Furthermore, since you want a Butterworth filter, you should provide the `method="iir"` argument.

```python
raw_eeg_subj2.filter(0.1, 100, method="iir")

```

These operations work in place, so after applying the first filter, you can apply the notch filters:

```python
raw_eeg_subj2.filter(52, 48)
raw_eeg_subj2.filter(102, 98)

```

These two notch filters use the default FIR method, but of course you can change it with the `method` argument if that’s not what you want. If you still get an error message regarding the stop band, let us know here!

---

<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:** [August 9, 2024, 8:46am UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/3 "2024-08-09T08:46:24Z")

</div>

I’m surprised `raw.filter` let’s you apply a stop band when `raw.notch_filter` exist 🤔

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 9, 2024, 9:16am UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/4 "2024-08-09T09:16:07Z")

</div>

I haven’t looked at the implementation, but `raw.notch_filter()` allows you to specify multiple frequencies to filter (e.g. harmonics), so this is very convenient. I would expect that _both_ methods should work, but `raw.notch_filter()` should probably be preferred in most cases.

---

<div class="post-metadata">

**Author:** ![Yih](https://avatars.discourse-cdn.com/v4/letter/y/3d9bf3/32.png) [@Yih](https://mne.discourse.group/u/Yih)\
**Post date:** [August 10, 2024, 4:36pm UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/5 "2024-08-10T16:36:08Z")

</div>

Hi,

Thank you very much for the answer and for mentioning how to apply a Butterworth filter! I tried both the filter() and notch\_filter(). With the notch\_filter(), it works:

```python
raw_eeg_subj38_01Hz_notch1 = raw_eeg_subj38_01Hz.copy().notch_filter(freqs = 50, notch_widths = 4)
raw_eeg_subj38_01Hz_notch2 = raw_eeg_subj38_01Hz_notch1.copy().notch_filter(freqs = 100, notch_widths = 4)

```

and I got the plot:

 ![pic_notch](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/b/ba5a88d904e46b6699dd27ab1bc2d780d7603132.png)

But with the filter method, I still got the error message like:

> Stop bands are not sufficiently separated.

My code was:

```python
raw_eeg_subj38_01Hz_filter1 = raw_eeg_subj38_01Hz.copy().filter(52, 48)

```

So I can use the notch\_filter() which is so helpful, but I am still a little bit confused about why the filter() failed. 😂

Anyway, thank you very much for the reply!! 😄

---

<div class="post-metadata">

**Author:** ![Yih](https://avatars.discourse-cdn.com/v4/letter/y/3d9bf3/32.png) [@Yih](https://mne.discourse.group/u/Yih)\
**Post date:** [August 10, 2024, 4:40pm UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/6 "2024-08-10T16:40:23Z")

</div>

Hi!

Thank you very much for mentioning the notch\_filter()! It worked very well with my data! 😄

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 11, 2024, 9:58am UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/7 "2024-08-11T09:58:50Z")

</div>

To be honest, I don’t know why `raw.filter()` does not work for a notch filter. In my opinion, it should be possible to use it as well (for example because the documentation includes band stop filters), so it’s maybe because of some parameters not working out (both methods internally dispatch to `mne.filter.filter_data()`). @larsoner do you have an idea and/or an opinion? If there is really a conceptual issue why notch filters cannot be applied with `raw.filter()`, there should at the very least be a note in the docs.

---

<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:** [August 12, 2024, 8:09am UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/8 "2024-08-12T08:09:32Z")

</div>

With the default parameters of `raw.filter`, you get `l_trans_bandwidth` and `h_trans_bandwidth` set to `'auto'` which will yield 2 Hz.

Documentation of `l_trans_bandwidth`:

> min(max(l\_freq \* 0.25, 2), l\_freq)

Thus if you change it, it works.  
The default for `notch_filter` is `trans_bandwidth=1`.

```python
from mne.datasets import sample
from mne.io import read_raw_fif

raw = read_raw_fif(sample.data_path() / "MEG" / "sample" / "sample_audvis_raw.fif")
raw.crop(tmax=30).pick("eeg").load_data()
raw.filter(52, 48, l_trans_bandwidth=1, h_trans_bandwidth=1)
raw.compute_psd().plot()

```

yields:

 ![Screenshot from 2024-08-12 10-07-51](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/3/3ce83b3c8658e53f8a5c2d11da0a42287cf4cd06.png)

Mathieu

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 12, 2024, 8:36am UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/9 "2024-08-12T08:36:06Z")

</div>

Nice, that’s it! Thanks @mscheltienne!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 19, 2024, 8:36am UTC](https://mne.discourse.group/t/failed-to-know-how-to-apply-a-stopband-over-a-passband/9187/10 "2024-08-19T08:36:26Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
