Skip to content

Instantly share code, notes, and snippets.

@aweinstein
Created October 10, 2017 14:37
Show Gist options
  • Save aweinstein/70803335da5414257a49ab1e54042eb7 to your computer and use it in GitHub Desktop.
Save aweinstein/70803335da5414257a49ab1e54042eb7 to your computer and use it in GitHub Desktop.
Test all the combinations of dB and estimate when plotting the PSD of raw object.
from __future__ import print_function, division
import numpy as np
import matplotlib.pyplot as plt
from itertools import product
import mne
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
def scipy_ex():
"""Data from scipy.signals.welch doc example.
We change the sampling frequency, frequency and amplitude to make it
EEG-like.
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.welch.html
"""
fs = 100
tmax = 60
amp = 1e-6 * np.sqrt(2)
freq = 10.
noise_power = 0.0000001e-6 * fs / 2
time = np.arange(tmax * fs) / fs
x = amp * np.sin(2*np.pi*freq*time)
x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
return fs, time, x
if __name__ == '__main__':
fs, t, x = scipy_ex()
ch_types = ['eeg']
ch_names = ['ch1']
info = mne.create_info(ch_names=ch_names, sfreq=fs, ch_types=ch_types)
raw = mne.io.RawArray(np.atleast_2d(x), info)
plt.close('all')
# | dB | estimate | plot | units |
# |-------+-------------+------+-----------------|
# | True | 'power' | PSD | u**2/Hz (dB) |
# | False | 'amplitude' | ASD | u/sqrt{Hz} |
# | True | 'auto' | PSD | u**2/Hz (dB) |
# | False | 'power' | PSD | u**2/Hz |
# | True | 'amplitude' | ASD | u/sqrt{Hz} (dB) |
# | False | 'auto' | ASD | u/sqrt{Hz} |
for dB, estimate in product((True, False), ('power', 'amplitude', 'auto')):
msg = 'db: %s, estimate: %s' % (dB, estimate)
print(msg)
fig = raw.plot_psd(n_fft=1024, n_overlap=512, dB=dB, estimate=estimate)
fig.gca().set_title(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment