# I want to remove AC noise

**URL:** <https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561>\
**Category:** Support & Discussions\
**Tags:** eeg\
**Created:** [September 9, 2022, 11:25am UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561 "2022-09-09T11:25:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![OnoreCoffee](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/onorecoffee/32/1887_2.png) [@OnoreCoffee](https://mne.discourse.group/u/OnoreCoffee)\
**Post date:** [September 9, 2022, 11:25am UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/1 "2022-09-09T11:25:15Z")

</div>

MNE version: 1.0.3  
OS: macOS Monterey

Hello.

I am trying to measure event-related potentials.  
Preliminary experiments have shown that there is too much AC noise, so I am thinking of removing it.  
What code should I write to remove it?

Thank you in advance for your help.

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [September 9, 2022, 12:17pm UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/2 "2022-09-09T12:17:31Z")

</div>

what is AC noise ? you mean line noise?

can you show the PSD of your data?

Alex

---

<div class="post-metadata">

**Author:** ![OnoreCoffee](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/onorecoffee/32/1887_2.png) [@OnoreCoffee](https://mne.discourse.group/u/OnoreCoffee)\
**Post date:** [September 9, 2022, 2:59pm UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/3 "2022-09-09T14:59:52Z")

</div>

Thank you for your reply.

It is about the line noise. I wrote AC noise because I think it is caused by AC power supply.  
I am wondering how to write the code to remove this in MNEPython.

Sorry for being a beginner.  
What do you mean by PSD?

Sorry for the lack of explanation.  
Thank you in advance.

---

<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:** [September 9, 2022, 4:24pm UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/4 "2022-09-09T16:24:10Z")

</div>

PSD = Power Spectral Density. Let’s start by confirming that your data is contaminated by powerline noise, 50 Hz in Europe, 60 Hz in the US (and I don’t know elsewhere).

Could you load your data into a `Raw` object (see the `Loading Data` section of the first tutorial: [Overview of MEG/EEG analysis with MNE-Python — MNE 1.1.1 documentation](https://mne.tools/stable/auto_tutorials/intro/10_overview.html#loading-data)) and then use the [`plot_psd`](https://mne.tools/stable/generated/mne.io.Raw.html#mne.io.Raw.plot_psd) method to display the PSD?

Link: [mne.io.Raw — MNE 1.1.1 documentation](https://mne.tools/stable/generated/mne.io.Raw.html#mne.io.Raw.plot_psd)  
Code snippet:

```python
from mne.io import read_raw

raw = read_raw(fname, preload=True) # where fname is the path to your compatible file
raw.plot_psd()

```

---

<div class="post-metadata">

**Author:** ![OnoreCoffee](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/onorecoffee/32/1887_2.png) [@OnoreCoffee](https://mne.discourse.group/u/OnoreCoffee)\
**Post date:** [September 11, 2022, 6:33am UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/5 "2022-09-11T06:33:33Z")

</div>

Thank you for your reply.  
Sorry for the delay in responding.

In my area the power supply noise is considered 50Hz.

I ran it on the raw data and the following figure was output.

 ![2022-09-11 15.31.26](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/e/e495ccb7815c5b6e6ed31c9b0c559acd116d2208.png)

---

<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:** [September 11, 2022, 10:05am UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/6 "2022-09-11T10:05:06Z")

</div>

As you can see, there is no powerline noise. You do not have a peak at 50 Hz or 60 Hz.  
Also, the dataset seems to be bandpass filtered between 1 and 20 Hz. The peak at 20 Hz looks weird to me.

Could you do the same plot on the `raw` data **before** BP filter?

---

<div class="post-metadata">

**Author:** ![OnoreCoffee](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/onorecoffee/32/1887_2.png) [@OnoreCoffee](https://mne.discourse.group/u/OnoreCoffee)\
**Post date:** [September 11, 2022, 10:30am UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/7 "2022-09-11T10:30:31Z")

</div>

Thank you for your reply.

As you can see, the data was after filtering.  
Here are the results plotted with the data before filtering.

 ![2022-09-11 19.25.00](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/e/e6e2d777cbf064541f3a480346af525965174bd5.png)

---

<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:** [September 11, 2022, 10:52am UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/8 "2022-09-11T10:52:34Z")

</div>

I have no idea what can cause noise at 20 Hz. If someone know, I’m very curious.  
Anyway, you can apply a notch for 20 Hz and the harmonics. This way, you will also be able to increase your BP upper limit to more than 20 Hz if you want:

```python
import numpy as np

raw = [...] # load data
raw.notch_filter(np.arange(20, 120, 20))

```

---

<div class="post-metadata">

**Author:** ![OnoreCoffee](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/onorecoffee/32/1887_2.png) [@OnoreCoffee](https://mne.discourse.group/u/OnoreCoffee)\
**Post date:** [September 11, 2022, 12:09pm UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/9 "2022-09-11T12:09:15Z")

</div>

Thank you for your reply.

Filtering at every 20 Hz resulted in the figure.  
I think I have lowered it more than necessary, but is it OK?

 ![2022-09-11 21.00.13](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/6/6edc7e48a1d82709b464af39dca603be28fa7764.png)

---

<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:** [September 11, 2022, 1:28pm UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/10 "2022-09-11T13:28:50Z")

</div>

That’s what a filter does, it looks OK. I prefer to avoid using `notch` filters, but I don’t see another solution in your case.  
Alternatively, you should figure out where this noise is coming from, remove the source, and record clean data.

---

<div class="post-metadata">

**Author:** ![OnoreCoffee](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/onorecoffee/32/1887_2.png) [@OnoreCoffee](https://mne.discourse.group/u/OnoreCoffee)\
**Post date:** [September 11, 2022, 2:27pm UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/11 "2022-09-11T14:27:33Z")

</div>

I understand.  
Thank you for your kind .

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [September 14, 2022, 7:40pm UTC](https://mne.discourse.group/t/i-want-to-remove-ac-noise/5561/12 "2022-09-14T19:40:00Z")

</div>

> [@mscheltienne](#):
>
> I have no idea what can cause noise at 20 Hz. If someone know, I’m very curious.

One thing to check is if the sampling frequency was somehow mis-specified, so that interference at 50 Hz was showing up as if it were 20 Hz. I don’t know _how_ this would have happened though so it’s a wild guess.

Another possibility is that it’s a coming from equipment in an adjacent lab in the same building. I’ve seen this once, it was coming through on an audio recording in a sound-attenuated booth. It was a timing signal for some custom physics equipment, and it was exactly 1kHz. (at 20 Hz this is almost surely not a timing signal 🙂 but may still be caused by nearby equipment)
