import numpy as np
import matplotlib.pyplot as plt
import spkit as sp
from spkit.data import load_data
sp.__version__
'0.0.9.3'
X,ch_names = load_data.eegSample()
fs = 128
Xf = sp.filter_X(X,band=[0.5], btype='highpass',fs=fs,verbose=0).T
Xf.shape
(2048, 14)
t = np.arange(Xf.shape[0])/fs
plt.figure(figsize=(12,5))
plt.plot(t,Xf+np.arange(-7,7)*200)
plt.xlim([t[0],t[-1]])
plt.xlabel('time (sec)')
plt.yticks(np.arange(-7,7)*200,ch_names)
plt.grid()
plt.title('Xf: 14 channel - EEG Signal (filtered)')
plt.show()
XR = sp.eeg.ICA_filtering(Xf.copy(),verbose=1,kur_thr=2,corr_thr=0.8,winsize=128)
XR.shape
ICA Artifact Removal : extended-infomax 100.05%|##################################################|2112\2113| Done..
(2048, 14)
plt.figure(figsize=(12,5))
plt.plot(t,XR+np.arange(-7,7)*200)
plt.xlim([t[0],t[-1]])
plt.xlabel('time (sec)')
plt.yticks(np.arange(-7,7)*200,ch_names)
plt.grid()
plt.title('XR: Corrected Signal')
plt.show()
plt.figure(figsize=(12,5))
plt.plot(t,(Xf-XR)+np.arange(-7,7)*200)
plt.xlim([t[0],t[-1]])
plt.xlabel('time (sec)')
plt.yticks(np.arange(-7,7)*200,ch_names)
plt.grid()
plt.title('Xf - XR: Difference (removed signal)')
plt.show()
Xf1 = Xf[128*10:128*14].copy()
t = np.arange(Xf1.shape[0])/fs
plt.figure(figsize=(12,5))
plt.plot(t,Xf1+np.arange(-7,7)*200)
plt.xlim([t[0],t[-1]])
plt.xlabel('time (sec)')
plt.yticks(np.arange(-7,7)*200,ch_names)
plt.grid()
plt.title('Xf: 14 channel - EEG Signal (filtered)')
plt.show()
XR1 = sp.eeg.ICA_filtering(Xf1.copy(),verbose=1,kur_thr=2,corr_thr=0.8,winsize=128*2)
XR1.shape
ICA Artifact Removal : extended-infomax 100.16%|##################################################|640\641| Done..
(512, 14)
plt.figure(figsize=(15,5))
plt.subplot(121)
plt.plot(t,XR1+np.arange(-7,7)*200)
plt.xlim([t[0],t[-1]])
plt.xlabel('time (sec)')
plt.yticks(np.arange(-7,7)*200,ch_names)
plt.grid()
plt.title('XR: Corrected Signal')
plt.subplot(122)
plt.plot(t,(Xf1-XR1)+np.arange(-7,7)*200)
plt.xlim([t[0],t[-1]])
plt.xlabel('time (sec)')
plt.yticks(np.arange(-7,7)*200,ch_names)
plt.grid()
plt.title('Xf - XR: Difference (removed signal)')
plt.show()
help(sp.eeg.ICA_filtering)
Help on function ICA_filtering in module eeg.artifact_correction: ICA_filtering(X, winsize=128, ICA_method='extended-infomax', kur_thr=2, corr_thr=0.8, AF_ch_index=[0, 13], F_ch_index=[1, 2, 11, 12], verbose=True, window=['hamming', True], hopesize=None, winMeth='custom') input ------ X: input signal (n,ch) with n samples and ch channels winsize: window size to process, if None, entire signal is used at once ICAMed = ['fastICA','infomax','extended-infomax','picard'] (1) Kurtosis based artifacts - mostly for motion artifacts ------------------------------------------------------ kur_thr: (default 2) threshold on kurtosis of IC commponents to remove, higher it is, more peaky component is selected : +ve int value (2) Correlation Based Index (CBI) for eye movement artifacts -------------------------------------------------------- for applying CBI method, index of prefrontal (AF - First Layer of electrodes towards frontal lobe) and frontal lobe (F - second layer of electrodes)channels needs to be provided. For case of 14-channels Emotiv Epoc ch_names = ['AF3','F7','F3','FC5','T7','P7','O1','O2','P8','T8','FC6','F4','F8','AF4'] PreProntal Channels =['AF3','AF4'], Fronatal Channels = ['F7','F3','F4','F8'] AF_ch_index =[0,13] : (AF - First Layer of electrodes towards frontal lobe) F_ch_index =[1,2,11,12] : (F - second layer of electrodes) if AF_ch_index or F_ch_index is None, CBI is not applied (3) Correlation of any independent component with many EEG channels --------------------------------------------------------- If any indepentdent component is correlated fo corr_thr% (80%) of elecctrodes, is considered to be artifactual -- Similar like CBI, except, not comparing fronatal and prefrontal but all corr_thr: (deafult 0.8) threshold to consider correlation, higher the value less IC are removed and vise-versa : float [0-1], : if None, this is not applied