# Re-referencing EEG electrodes in MNE

**URL:** <https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [September 30, 2013, 10:43pm UTC](https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618 "2013-09-30T22:43:13Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [September 30, 2013, 10:43pm UTC](https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618/1 "2013-09-30T22:43:13Z")

</div>

Hello,

I'm using MNE and MNE-Python to analyze EEG data from a 64 channel biosemi  
EEG cap, with no MEG channels, and 2 mastoid EEG channels for reference.  
I'd like to re-reference (offline) my EEG channels to the average of the 2  
mastoids. Is there a way to do this with either MNE command line or MNE  
python? I see that if I wanted to apply an average reference of all EEG  
channels, MNE command line stores this as a source space projection. What  
would this projection file look like if it were just the 2 mastoid  
electrodes?

Thank you and I apologize if this has been addressed in a previous thread.

Regards,  
Alan Leggitt  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20130930/26d80044/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20130930/26d80044/attachment.html)

---

<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:** [October 1, 2013, 7:38am UTC](https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618/2 "2013-10-01T07:38:51Z")

</div>

Hi Alan,

I am not aware of a way to do this in a generic manner with MNE/MNE-Python  
but it should not be hard.

At what stage of the analysis would you apply the new reference? on  
Raw or Evoked?

Maybe you can draft a python function to do this and we can see how to  
merge this to mne-python?

best,  
Alex

---

<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:** [October 1, 2013, 10:02am UTC](https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618/3 "2013-10-01T10:02:23Z")

</div>

Hi Alan,

The linked mastoids reference should not be used for source modeling. In my mind it is part of EEG heritage that should be definitely dropped.  
In general re-referencing operations are not linear projection operations. Average reference is a special case.

You need to write some Python or Matlab code to modify your data if you want to view the EEG in the "linked-ears" format. Again this kind of data should not be used for source modeling purposes.

- Matti

---

<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:** [October 1, 2013, 10:49am UTC](https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618/4 "2013-10-01T10:49:37Z")

</div>

Thanks Matti and Alex for these suggestions, I just wanted to jump in with  
another question on the side if that is all right: Matti, is your comment  
about linked mastoids relevant specifically to physically linked mastoids,  
or also to arithmetically averaged mastoids (that are not physically  
linked)? I know the problems with physically linked mastoid channels, but  
is it also the case that separate mastoids shouldn't be used as a reference  
for source modeling?

Thanks,  
Steve

Stephen Politzer-Ahles  
New York University, Abu Dhabi  
Neuroscience of Language Lab  
[http://www.nyu.edu/projects/politzer-ahles/](http://www.nyu.edu/projects/politzer-ahles/)

---

<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:** [October 1, 2013, 5:27pm UTC](https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618/5 "2013-10-01T17:27:03Z")

</div>

Thanks so much for the feedback. Right now I'm trying to implement this in the raw stage, the rationale being that re-referencing might reduce the number of epochs that are rejected by amplitude threshold. I don't want to do this with an average reference, since I know that I have some noisy channels that I need to exclude.

I was playing around with this yesterday and I came up with a crude solution. I've attached my python code.

Although I eventually want to move to source modeling, right now I'm interested in using MNE to do ERP and spectral analysis, instead of using something Matlab based like EEGLAB. Would you recommend re-referencing in this context?

While I'm on the subject, EEGLAB recommends specifying a reference channel when import eeg data from a .bdf file. Their rationale ...

% WARNING! Biosemi Active II data  
% are recorded reference-free, but LOSE 40 dB of SNR  
% if no reference is used!. If you do not know which  
% channel to use, pick one and then re-reference after  
% the channel locations are read in. {default: none}

Is this also a problem with mne\_edf2fiff ?

Thanks again,  
Alan Leggitt

---

<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:** [October 2, 2013, 7:42am UTC](https://mne.discourse.group/t/re-referencing-eeg-electrodes-in-mne/618/6 "2013-10-02T07:42:09Z")

</div>

Hi Alan,

I moved the discussion to :

> <https://github.com/mne-tools/mne-python/issues/784>
>
> suggested by Alan Leggitt
> 
> \`\`\`
> from matplotlib import mlab
> import mne
> import num…py as np
> 
> def reref(rawFileName,refChannels=\['M1','M2'\]):
> 
> '''
> A function to rereference eeg channels
> 
> Arguments
> ---------
> rawFileName (string) - the full file name of the .fif file containing the raw data
> refChannels (list of strings) - the name (or names) of the reference channels
> 
> Returns
> -------
> raw - the raw object with rereferenced eeg channels
> 
> '''
> 
> # Read the raw .fif file into mne
> raw = mne.fiff.Raw(rawFileName,preload=True)
> 
> # Find the indices to the reference electrodes
> refIdx = mlab.find(np.in1d(raw.ch\_names,refChannels))
> 
> # Get the reference data
> refData = raw.\_data\[refIdx\].mean(0)
> 
> # Get the indices to the eeg channels
> eegIdx = mne.fiff.pick\_types(raw.info,eeg=True,meg=False)
> 
> # Rereference the eeg channels
> raw.\_data\[eegIdx\] -= refData
> 
> return raw
> 
> if \_\_name\_\_ == "\_\_main\_\_":
> rawFileName = '/data/alanData/EEGDATA/EEG\_Data\_PRE/115/115\_EXT-D1\_Pre\_01\_ica.fif'
> raw = reref(rawFileName)
> 
> \`\`\`

let me know if you want to give it a try.

> the raw stage, the rationale being that re-referencing might reduce the  
> number of epochs that are rejected by amplitude threshold. I don't want to  
> do this with an average reference, since I know that I have some noisy  
> channels that I need to exclude.

you should mark your channels as bad first

> I was playing around with this yesterday and I came up with a crude  
> solution. I've attached my python code.
> 
> Although I eventually want to move to source modeling, right now I'm  
> interested in using MNE to do ERP and spectral analysis, instead of using  
> something Matlab based like EEGLAB. Would you recommend re-referencing in  
> this context?

yes if you want to see something that matches the ERP topography

> While I'm on the subject, EEGLAB recommends specifying a reference channel  
> when import eeg data from a .bdf file. Their rationale ...
> 
> % WARNING! Biosemi Active II data  
> % are recorded reference-free, but LOSE 40 dB of SNR  
> % if no reference is used!. If you do not know which  
> % channel to use, pick one and then re-reference after  
> % the channel locations are read in. {default: none}
> 
> Is this also a problem with mne\_edf2fiff ?

I don't know. I guess it's due to limited precision used for storing  
floating point values

hope this helps

Alex
