|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function [art, horiz, vert, blink, disc,... |
|
|
soglia_DV, diff_var, soglia_K, med2_K, meanK, soglia_SED, med2_SED, SED, soglia_SAD, med2_SAD, SAD, ... |
|
|
soglia_GDSF, med2_GDSF, GDSF, soglia_V, med2_V, nuovaV, soglia_D, maxdin]=ADJUST (EEG,out) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if length(size(EEG.data))==3 |
|
|
|
|
|
num_epoch=size(EEG.data,3); |
|
|
|
|
|
else |
|
|
|
|
|
num_epoch=0; |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
if isempty(EEG.icaact) |
|
|
disp('EEG.icaact not present. Recomputed from data.'); |
|
|
if length(size(EEG.data))==3 |
|
|
|
|
|
|
|
|
EEG.icaact = reshape(EEG.icaweights*EEG.icasphere*reshape(EEG.data,[size(EEG.data,1)... |
|
|
size(EEG.data,2)*size(EEG.data,3)]),[size(EEG.data,1) size(EEG.data,2) size(EEG.data,3)]); |
|
|
else EEG.icaact = EEG.icaweights*EEG.icasphere*EEG.data; |
|
|
end |
|
|
end |
|
|
|
|
|
topografie=EEG.icawinv'; %computes IC topographies |
|
|
|
|
|
% Topographies and time courses normalization |
|
|
% |
|
|
% disp(' '); |
|
|
% disp('Normalizing topographies...') |
|
|
% disp('Scaling time courses...') |
|
|
|
|
|
for i=1:size(EEG.icawinv,2) % number of ICs |
|
|
|
|
|
ScalingFactor=norm(topografie(i,:)); |
|
|
|
|
|
topografie(i,:)=topografie(i,:)/ScalingFactor; |
|
|
|
|
|
if length(size(EEG.data))==3 |
|
|
EEG.icaact(i,:,:)=ScalingFactor*EEG.icaact(i,:,:); |
|
|
else |
|
|
EEG.icaact(i,:)=ScalingFactor*EEG.icaact(i,:); |
|
|
end |
|
|
|
|
|
end |
|
|
% |
|
|
% disp('Done.') |
|
|
% disp(' ') |
|
|
|
|
|
% Variables memorizing artifacted ICs indexes |
|
|
|
|
|
blink=[]; |
|
|
|
|
|
horiz=[]; |
|
|
|
|
|
vert=[]; |
|
|
|
|
|
disc=[]; |
|
|
|
|
|
%% Check EEG channel position information |
|
|
nopos_channels=[]; |
|
|
for el=1:length(EEG.chanlocs) |
|
|
if(any(isempty(EEG.chanlocs(1,el).X)&isempty(EEG.chanlocs(1,el).Y)&isempty(EEG.chanlocs(1,el).Z)&isempty(EEG.chanlocs(1,el).theta)&isempty(EEG.chanlocs(1,el).radius))) |
|
|
nopos_channels=[nopos_channels el]; |
|
|
end; |
|
|
end |
|
|
|
|
|
if ~isempty(nopos_channels) |
|
|
warning(['Channels ' num2str(nopos_channels) ' have incomplete location information. They will NOT be used to compute ADJUST spatial features']); |
|
|
disp(' '); |
|
|
end; |
|
|
|
|
|
pos_channels=setdiff(1:length(EEG.chanlocs),nopos_channels); |
|
|
|
|
|
%% Feature extraction |
|
|
|
|
|
disp(' ') |
|
|
disp('Features Extraction:') |
|
|
|
|
|
%GDSF - General Discontinuity Spatial Feature |
|
|
|
|
|
disp('GDSF - General Discontinuity Spatial Feature...') |
|
|
|
|
|
GDSF = compute_GD_feat(topografie,EEG.chanlocs(1,pos_channels),size(EEG.icawinv,2)); |
|
|
|
|
|
|
|
|
%SED - Spatial Eye Difference |
|
|
|
|
|
disp('SED - Spatial Eye Difference...') |
|
|
|
|
|
[SED,medie_left,medie_right]=computeSED_NOnorm(topografie,EEG.chanlocs(1,pos_channels),size(EEG.icawinv,2)); |
|
|
|
|
|
|
|
|
%SAD - Spatial Average Difference |
|
|
|
|
|
disp('SAD - Spatial Average Difference...') |
|
|
|
|
|
[SAD,var_front,var_back,mean_front,mean_back]=computeSAD(topografie,EEG.chanlocs(1,pos_channels),size(EEG.icawinv,2)); |
|
|
|
|
|
|
|
|
%SVD - Spatial Variance Difference between front zone and back zone |
|
|
|
|
|
diff_var=var_front-var_back; |
|
|
|
|
|
%epoch dynamic range, variance and kurtosis |
|
|
|
|
|
K=zeros(num_epoch,size(EEG.icawinv,2)); %kurtosis |
|
|
Kloc=K; |
|
|
|
|
|
Vmax=zeros(num_epoch,size(EEG.icawinv,2)); %variance |
|
|
|
|
|
% disp('Computing variance and kurtosis of all epochs...') |
|
|
|
|
|
for i=1:size(EEG.icawinv,2) % number of ICs |
|
|
|
|
|
for j=1:num_epoch |
|
|
Vmax(j,i)=var(EEG.icaact(i,:,j)); |
|
|
% Kloc(j,i)=kurtosis(EEG.icaact(i,:,j)); |
|
|
K(j,i)=kurt(EEG.icaact(i,:,j)); |
|
|
end |
|
|
end |
|
|
|
|
|
% check that kurt and kurtosis give the same values: |
|
|
% [a,b]=max(abs(Kloc(:)-K(:))) |
|
|
|
|
|
%TK - Temporal Kurtosis |
|
|
|
|
|
disp('Temporal Kurtosis...') |
|
|
|
|
|
meanK=zeros(1,size(EEG.icawinv,2)); |
|
|
|
|
|
for i=1:size(EEG.icawinv,2) |
|
|
if num_epoch>100 |
|
|
meanK(1,i)=trim_and_mean(K(:,i)); |
|
|
else meanK(1,i)=mean(K(:,i)); |
|
|
end |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
%MEV - Maximum Epoch Variance |
|
|
|
|
|
disp('Maximum epoch variance...') |
|
|
|
|
|
maxvar=zeros(1,size(EEG.icawinv,2)); |
|
|
meanvar=zeros(1,size(EEG.icawinv,2)); |
|
|
|
|
|
|
|
|
for i=1:size(EEG.icawinv,2) |
|
|
if num_epoch>100 |
|
|
maxvar(1,i)=trim_and_max(Vmax(:,i)'); |
|
|
meanvar(1,i)=trim_and_mean(Vmax(:,i)'); |
|
|
else |
|
|
maxvar(1,i)=max(Vmax(:,i)); |
|
|
meanvar(1,i)=mean(Vmax(:,i)); |
|
|
end |
|
|
end |
|
|
|
|
|
% MEV in reviewed formulation: |
|
|
|
|
|
nuovaV=maxvar./meanvar; |
|
|
|
|
|
|
|
|
|
|
|
%% Thresholds computation |
|
|
|
|
|
disp('Computing EM thresholds...') |
|
|
|
|
|
% soglia_K=EM(meanK); |
|
|
% |
|
|
% soglia_SED=EM(SED); |
|
|
% |
|
|
% soglia_SAD=EM(SAD); |
|
|
% |
|
|
% soglia_GDSF=EM(GDSF); |
|
|
% |
|
|
% soglia_V=EM(nuovaV); |
|
|
[soglia_K,med1_K,med2_K]=EM(meanK); |
|
|
|
|
|
[soglia_SED,med1_SED,med2_SED]=EM(SED); |
|
|
|
|
|
[soglia_SAD,med1_SAD,med2_SAD]=EM(SAD); |
|
|
|
|
|
[soglia_GDSF,med1_GDSF,med2_GDSF]=EM(GDSF); |
|
|
|
|
|
[soglia_V,med1_V,med2_V]=EM(nuovaV); |
|
|
|
|
|
%% Output file header |
|
|
|
|
|
% ---------------------------------------------------- |
|
|
% | Opens report file and writes header | |
|
|
% ---------------------------------------------------- |
|
|
|
|
|
file=fopen(out,'w'); |
|
|
|
|
|
fprintf(file,'ADJUST\n'); |
|
|
|
|
|
fprintf(file,'Automatic EEG artifacts Detector with Joint Use of Spatial and Temporal features\n\n'); |
|
|
|
|
|
fprintf(file,'Andrea Mognon and Marco Buiatti (2009-2014)\n\n'); |
|
|
|
|
|
fprintf(file,['Analyzed dataset: ' EEG.filename '\n']); |
|
|
|
|
|
fprintf(file,['Analysis date: ' date '\n']); |
|
|
|
|
|
fprintf(file,'Analysis carried out on the |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disp(' '); |
|
|
disp('Artifact Identification:'); |
|
|
disp('Horizontal Eye Movements...') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(file,'> HEM - Horizontal movements\n\n'); |
|
|
|
|
|
fprintf(file,'Classification based on features:\n'); |
|
|
|
|
|
fprintf(file,'SED - Spatial eye difference (threshold=%f)\n',soglia_SED); |
|
|
|
|
|
fprintf(file,'MEV - Maximum epoch variance (threshold=%f)\n\n',soglia_V); |
|
|
|
|
|
fprintf(file,'ICs with Horizontal eye movements:\n'); |
|
|
|
|
|
horiz=intersect(intersect(find(SED>=soglia_SED),find(medie_left.*medie_right<0)),... |
|
|
(find(nuovaV>=soglia_V))); |
|
|
|
|
|
hor_bool=1; |
|
|
|
|
|
if isempty(horiz) |
|
|
|
|
|
fprintf(file,'/ \n'); |
|
|
|
|
|
hor_bool=0; |
|
|
|
|
|
else |
|
|
|
|
|
fprintf(file,[num2str(horiz) '\n']); |
|
|
fprintf(file,'\n'); |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disp('Vertical Eye Movements...') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(file,'>> VEM - Vertical movements\n\n'); |
|
|
|
|
|
fprintf(file,'Classification based on features:\n'); |
|
|
|
|
|
fprintf(file,'SAD - Spatial average difference (threshold=%f)\n',soglia_SAD); |
|
|
|
|
|
fprintf(file,'MEV - Maximum epoch variance (threshold=%f)\n\n',soglia_V); |
|
|
|
|
|
fprintf(file,'ICs with Vertical eye movements:\n'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vert=intersect(intersect(find(SAD>=soglia_SAD),find(medie_left.*medie_right>0)),... |
|
|
intersect(find(diff_var>0),find(nuovaV>=soglia_V))); |
|
|
|
|
|
|
|
|
|
|
|
ver_bool=1; |
|
|
|
|
|
if isempty(vert) |
|
|
|
|
|
fprintf(file,'/ \n'); |
|
|
|
|
|
ver_bool=0; |
|
|
else |
|
|
|
|
|
fprintf(file,[num2str(vert) '\n']); |
|
|
fprintf(file,'\n'); |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disp('Eye Blinks...') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(file,'>>> EB - Blinks\n\n'); |
|
|
|
|
|
fprintf(file,'Classification based on features:\n'); |
|
|
|
|
|
fprintf(file,'SAD (threshold=%f)\n',soglia_SAD); |
|
|
|
|
|
fprintf(file,'TK - Temporal kurtosis (threshold=%f)\n\n',soglia_K); |
|
|
|
|
|
fprintf(file,'ICs with Blinks:\n'); |
|
|
|
|
|
|
|
|
|
|
|
blink=intersect ( intersect( find(SAD>=soglia_SAD),find(medie_left.*medie_right>0) ) ,... |
|
|
intersect ( find(meanK>=soglia_K),find(diff_var>0) )); |
|
|
|
|
|
|
|
|
|
|
|
bl_bool=1; |
|
|
|
|
|
if isempty(blink) |
|
|
|
|
|
fprintf(file,'/ \n'); |
|
|
|
|
|
bl_bool=0; |
|
|
else |
|
|
|
|
|
fprintf(file,[num2str(blink) '\n']); |
|
|
fprintf(file,'\n'); |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disp('Generic Discontinuities...') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(file,'>>>> GD - Discontinuities\n'); |
|
|
|
|
|
fprintf(file,'Classification based on features:\n'); |
|
|
|
|
|
fprintf(file,'GDSF - Generic Discontinuities Spatial Feature (threshold=%f)\n',soglia_GDSF); |
|
|
|
|
|
fprintf(file,'MEV - Maximum epoch variance (threshold=%f)\n\n',soglia_V); |
|
|
|
|
|
fprintf(file,'ICs with Generic Discontinuities:\n'); |
|
|
|
|
|
|
|
|
disc=intersect(find(GDSF>=soglia_GDSF),find(nuovaV>=soglia_V)); |
|
|
|
|
|
dsc_bool=1; |
|
|
|
|
|
if isempty(disc) |
|
|
|
|
|
fprintf(file,'/ \n'); |
|
|
|
|
|
dsc_bool=0; |
|
|
else |
|
|
|
|
|
fprintf(file,[num2str(disc) '\n']); |
|
|
fprintf(file,'\n'); |
|
|
end |
|
|
|
|
|
aic=unique([blink disc horiz vert]); |
|
|
|
|
|
fprintf(file,'Artifacted ICs (total):\n'); |
|
|
fprintf(file,[num2str(aic) '\n']); |
|
|
fprintf(file,'\n'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disp(' ') |
|
|
disp(['Results in <' out '>.']) |
|
|
|
|
|
|
|
|
fclose(file); |
|
|
|
|
|
|
|
|
art = nonzeros( union (union(blink,horiz) , union(vert,disc)) )'; %artifact ICs |
|
|
|
|
|
% these three are old outputs which are no more necessary in latest ADJUST version. |
|
|
soglia_D=0; |
|
|
soglia_DV=0; |
|
|
maxdin=zeros(1,size(EEG.icawinv,2)); |
|
|
|
|
|
return |
|
|
|
|
|
%% The following sections have been moved to interface_ADJ in order to manage |
|
|
%% continuous data |
|
|
|
|
|
% |
|
|
% %% Saving artifacted ICs for further analysis |
|
|
% |
|
|
% nome=['List_' EEG.setname '.mat']; |
|
|
% |
|
|
% save (nome, 'blink', 'horiz', 'vert', 'disc'); |
|
|
% |
|
|
% disp(' ') |
|
|
% disp(['Artifact ICs list saved in ' nome]); |
|
|
% |
|
|
% |
|
|
% %% IC show & remove |
|
|
% % show all ICs; detected ICs are highlighted in red color. Based on |
|
|
% % pop_selectcomps. |
|
|
% |
|
|
% art = nonzeros( union (union(blink,horiz) , union(vert,disc)) )'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|