-
Notifications
You must be signed in to change notification settings - Fork 1
/
Source_Power_Analysis.m
113 lines (92 loc) · 3.64 KB
/
Source_Power_Analysis.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function Source_Alpha =Source_Power_Analysis(subID)
% compute 9-15 Hz alpha source with DICS
% Input: subID---subject ID
% Output: Source_Alpha---alpha source for BCI down condition
% Fieldtrip path
FTpath = '/home/hjiang/Toolbox/fieldtrip-20180805/fieldtrip-20180805/';
% sample data folder
Datapath = '/home/hjiang/Project_BCIMeditation/Data_Github/SampleData';
% Load the data
load(fullfile(Datapath,strcat(subID,'BCI_UD')))
% Alpha source
Source_Alpha = zeros(7452,1);
%% get data before the hitting the target
Fs = BCI_UD.fsample;
TaskData = cell(1,length(BCI_UD.trial));
TimeSeri = cell(1,length(BCI_UD.trial));
for i = 1:length(BCI_UD.trial)
TaskData{i} = BCI_UD.trial{1,i}(:,floor((BCI_UD.trialinfo(i).t_result+1)*Fs):floor((BCI_UD.trialinfo(i).t_result+2)*Fs));
TimeInd = 0:1/Fs:(size(TaskData{i},2)-1)/Fs;
TimeSeri{i} = TimeInd;
end
BCI_UD_Feed = BCI_UD;
BCI_UD_Feed.trial = TaskData;
BCI_UD_Feed.time = TimeSeri;
BCI_UD_Feed.label = cellfun(@(x) upper(x), BCI_UD_Feed.label,'uniformoutput',0);
BCI_UD_Feed.elec.label = cellfun(@(x) upper(x), BCI_UD_Feed.elec.label,'uniformoutput',0);
%%%%%%%%% select only down trials %%%%%%%%%%
cfg = [];
cfg.trials = BCI_UD.DInds; % only select down trial
BCI_UD_Feed = ft_selectdata(cfg,BCI_UD_Feed);
%% template grid
templatedir = fullfile(FTpath,'template') ;
% standard bem model
% http://www.fieldtriptoolbox.org/template/headmodel/
load(fullfile(templatedir,'headmodel/standard_bem'))
% template atlas
% atlas = ft_read_atlas(fullfile(templatedir,'atlas/aal/ROI_MNI_V4.nii'));
% get elec info;
elecs_EEG = BCI_UD_Feed.elec;
% elecs.label = cellfun(@(x) upper(x),elecs.label,'uniformoutput',0);
elecs = ft_read_sens(fullfile(templatedir,'electrode/standard_1020.elc'));
elecs.label = cellfun(@(x) upper(x),elecs.label,'uniformoutput',0);
[s1,s2]=match_str(elecs_EEG.label,elecs.label);
% common grid/filter
cfg = [];
cfg.elec = elecs;
cfg.vol = vol;
cfg.reducerank = 3; % default is 3 for EEG, 2 for MEG
cfg.grid.resolution = 0.8; % use a 3-D grid with a cm resolution
cfg.grid.unit = 'cm';
cfg.grid.tight = 'yes';
% cfg.normalize = 'yes';
cfg.channel = elecs.label(s2);
[grid] = ft_prepare_leadfield(cfg);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% 9-15 Hz Alpha source %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
lambda = '10%';
band = 'Alpha';
foi = 12;
tapsmofrq = 3;
taper = 'dpss'; % 'dpss'/ 'hanning'
%%%%%%%%% Feedback %%%%%%%%%%%%%%%%%%
% Freqanalysis for beamformer
cfg = [];
cfg.method = 'mtmfft';
cfg.taper = taper;
cfg.channel = elecs.label(s2);
cfg.output = 'powandcsd';
cfg.keeptrials = 'yes';
cfg.foi = foi;
cfg.tapsmofrq = tapsmofrq;
Feed_powcsd = ft_freqanalysis(cfg, BCI_UD_Feed);
% beamform common filter
cfg = [];
cfg.method = 'dics';
cfg.elec = elecs;
cfg.channel = Feed_powcsd.label;
cfg.frequency = Feed_powcsd.freq;
% cfg.keeptrials = 'yes';
% cfg.rawtrial = 'yes';
cfg.grid = grid;
cfg.vol = vol;
cfg.senstype = 'EEG'; % Remember this must be specified as either EEG, or MEG
cfg.dics.keepfilter = 'yes';
cfg.dics.lambda = lambda;
cfg.dics.projectnoise = 'yes';
cfg.dics.fixedori = 'yes';
Feed_source = ft_sourceanalysis(cfg, Feed_powcsd);
Alpha = Feed_source.avg.pow;
Source_Alpha = Alpha;
end