File size: 12,609 Bytes
d7989aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
%% Step2: PostProcessing data: ERP analysis; Time-Frequncy analaysis, ITPC analysis
% Import preprocessed data 

clear; clc; close all;
datalocation='D:\Project_EEG_CC\CC_Results_step1\';              % Data are here

savedir = 'D:\Project_EEG_CC\CC_PD_Figures_Manuscript\CC_Manuscript\Manuscript_Scripts_PREDICT\Data\'; % save data here

cd(savedir);



load('D:\Project_EEG_CC\mFiles\ONOFF.mat','ONOFF')

load('D:\Project_EEG_CC\mFiles\BV_Chanlocs_60.mat');



[num_cc,txt_cc,raw_cc]=xlsread('D:\Project_EEG_CC\CC_ICAs.xlsx');



% subjects

PDsx=[801:811,813:823,824:829];  % 824 S2 CC is bad (mange in Step 3)

CTLsx=[8010,8070,8060,890:914];  % 911 S1 CC is bad (mange in Step 3)

%%%%%%%%% or run 824 afterwards since session 2 is bad OR use sessiosn 1

%%%%%%%%% for both OFF and ON



%% ##########################

for subj=[CTLsx(end:-1:1),PDsx(end:-1:1)]

    for session=1:2

        if (subj>850 && session==1) || subj<850  % If not ctl, do session 2

            if 1 % exist([num2str(subj),'_Session_',num2str(session),'_PDDys_CC.mat'])~=2;

                         

                % ----------------   GET PD and Control DATA ----------------   ----------------  ----------------

                         

                % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&                   % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 

                disp([num2str(subj),'_Session_',num2str(session),'_PDDys_CC.mat'])

                load([num2str(subj),'_Session_',num2str(session),'_PDDys_CC.mat'],'EEG','bad_chans','bad_epochs','bad_ICAs');

                

                % Get Subj Info

                temp1=cell2mat(raw_cc(find(num_cc(:,1)==subj),session+1));

                if isnumeric(temp1)

                    bad_ICAs_To_Remove=temp1;

                elseif strmatch('NaN',temp1)

                    bad_ICAs_To_Remove=NaN;

                else

                    bad_ICAs_To_Remove=str2num(temp1);

                end

                clear temp1;

                

                % Remove the (presumptive) bad ICAs:

                if ~(isnan(bad_ICAs_To_Remove))

                    EEG = pop_subcomp( EEG, bad_ICAs_To_Remove, 0);

                end

                clear bad_ICAs_To_Remove;

                

                % % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& GET Epochs  % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 



                CONGRU=[111,112,113,114,211,212,213,214];

                INCONGRU=[121,122,123,124,221,222,223,224];

                CORRECT=[101,102];

                ERROR=[103,104];

                REW=8;

                PUN=9;



                % Get the good info out of the epochs

                for aai=1:size(EEG.epoch,2)

                    EEG.epoch(aai).TYPE=NaN; EEG.epoch(aai).RESP=NaN; EEG.epoch(aai).RT=NaN;

                    RESP_VECTOR(aai,1:2)=NaN;

                    for bbi=1:size(EEG.epoch(aai).eventlatency,2)

                        % Get STIMTYPE

                        if EEG.epoch(aai).eventlatency{bbi}==0 % If this bi is the event

                            % Get StimType

                            FullName=EEG.epoch(aai).eventtype{bbi};

                            % IF TRN CUE

                            if any(str2num(FullName(2:end))==[CONGRU,INCONGRU])

                                EEG.epoch(aai).TYPE=str2num(FullName(2:end)) ;

                                if any(str2num(FullName(2:end))==CONGRU)

                                    VECTOR(aai)=5;

                                elseif any(str2num(FullName(2:end))==INCONGRU)

                                    VECTOR(aai)=6;

                                end

                                % If anything is next

                                if  size(EEG.epoch(aai).eventlatency,2)>=bbi

                                    % If RESP

                                    tempName=EEG.epoch(aai).eventtype{bbi+1};

                                    if any(str2num(tempName(2:end))==[CORRECT,ERROR])

                                        EEG.epoch(aai).RESP=str2num(tempName(2:end)) ;

                                        EEG.epoch(aai).RT=EEG.epoch(aai).eventlatency{bbi+1};

                                        RESP_VECTOR(aai,1)=str2num(tempName(2:end));

                                        RESP_VECTOR(aai,2)=EEG.epoch(aai).eventlatency{bbi+1};

                                    end

                                end

                            else

                                EEG.epoch(aai).TYPE=str2num(FullName(2:end)) ;

                                VECTOR(aai)=str2num(FullName(2:end));

                            end

                            clear FullName tempName

                        end

                    end

                end

                

                % Aggregate accelerometer data

                EEG.X=EEG.X-repmat(mean(EEG.X),3250,1);

                EEG.Y=EEG.Y-repmat(mean(EEG.Y),3250,1);

                EEG.Z=EEG.Z-repmat(mean(EEG.Z),3250,1);

                % Add to EEG.data as 61st channel - but not the rejected trials

                if subj==824 && session==2, clear bad_epochs; bad_epochs{1}=zeros(1,size(EEG.data,3)); end  % B/c 824 S2 is bad - hack this

                EEG.data(61,:,:)=(EEG.X(:,bad_epochs{1}~=1).^2)+(EEG.Y(:,bad_epochs{1}~=1).^2)+(EEG.Z(:,bad_epochs{1}~=1).^2);

                dims=size(EEG.data);

                

                % Lock to Response, Stim, and Cue

                respct=1;

                for ai=1:size(EEG.epoch,2)

                    if any(RESP_VECTOR(ai,1)==[CORRECT,ERROR])

                        Cue_to_Resp=RESP_VECTOR(ai,2) ./ (1000/EEG.srate);

                        if isnan(Cue_to_Resp), Cue_to_Resp=1; end

                        EEG.resp(:,:,respct)=[squeeze(EEG.data(:,Cue_to_Resp:end,ai)),zeros(dims(1),(Cue_to_Resp-1))];

                        if any(RESP_VECTOR(ai,1)==CORRECT)

                            VECTOR_resp(respct,1)=1; VECTOR_resp(respct,2)=Cue_to_Resp;

                        elseif any(RESP_VECTOR(ai,1)==ERROR)

                            VECTOR_resp(respct,1)=2; VECTOR_resp(respct,2)=Cue_to_Resp;

                        end

                        respct=respct+1;

                        clear Cue_to_Resp;

                    end

                end

                clear RESP_VECTOR;

                                

                % % Set Times

                tx=-1500:2:4998;

                b1=find(tx==-300);  b2=find(tx==-200);  %% original

                t1=find(tx==-500);  t2=find(tx==1000);

                tx2disp=-500:2:1000;

                

                % ------------------------ Get the goods

                X_CUE{1}=5;  % CONGRU

                X_CUE{2}=6;  % INCONGRU

                X_RESP{1}=1; % CORRECT RESP

                X_RESP{2}=2; % ERROR RESP

                                

                % ---------- % ---------- % ----------

                % ---------- TF stuff

                % ---------- % ---------- % ----------

                

                % Setup Wavelet Params

                num_freqs=50;

                frex=logspace(.01,1.7,num_freqs);

                s=logspace(log10(3),log10(10),num_freqs)./(2*pi*frex);

                t=-2:1/EEG.srate:2;

                

                % Definte Convolution Parameters

                n_wavelet = length(t);

                half_of_wavelet_size = (n_wavelet-1)/2; clear dims

                % -------- cue/fb

                dims{1} = size(EEG.data); n_data{1} = dims{1}(2)*dims{1}(3); n_convolution{1} = n_wavelet+n_data{1}-1;   n_conv_pow2{1} = pow2(nextpow2(n_convolution{1}));

                % -------- resp

                dims{2} = size(EEG.resp); n_data{2} = dims{2}(2)*dims{2}(3); n_convolution{2} = n_wavelet+n_data{2}-1;   n_conv_pow2{2} = pow2(nextpow2(n_convolution{2}));

                                

                CHANS= (1:60);

                for chani=1:60 

                    

                    % get FFT of data

                    EEG_fft{1} = fft(reshape(EEG.data(CHANS(chani),:,:),1,n_data{1}),n_conv_pow2{1});

                    EEG_fft{2} = fft(reshape(EEG.resp(CHANS(chani),:,:),1,n_data{2}),n_conv_pow2{2});

                    

                    for fi=1:num_freqs

                        

                        wavelet{1} = fft( exp(2*1i*pi*frex(fi).*t) .* exp(-t.^2./(2*(s(fi)^2))) , n_conv_pow2{1} );   

                        wavelet{2} = fft( exp(2*1i*pi*frex(fi).*t) .* exp(-t.^2./(2*(s(fi)^2))) , n_conv_pow2{2} );   

                         

                        % convolution

                        for convo=1:2

                            EEG_conv = ifft(wavelet{convo}.*EEG_fft{convo});

                            EEG_conv = EEG_conv(1:n_convolution{convo});

                            EEG_conv = EEG_conv(half_of_wavelet_size+1:end-half_of_wavelet_size);

                            EEG_multi_conv{convo} = reshape(EEG_conv,dims{convo}(2),dims{convo}(3)); clear EEG_conv;

                            temp_POWER{convo} = abs(EEG_multi_conv{convo}(t1:t2,:)).^2;

                        end



                        % Baseline from pre-cue  {1}

                        BASE = mean(mean(abs( EEG_multi_conv{1}(b1:b2,VECTOR<7)).^2));

                                           

                        % Average FIRST 

                        for condi=1:2

                            temp_POWER_avg(:,condi,1) = mean(temp_POWER{1}(:,VECTOR==X_CUE{condi}),2); 

                            temp_POWER_avg(:,condi,2) = mean(temp_POWER{2}(:,VECTOR_resp==X_RESP{condi}),2);

                            % -------------------

                            ITPC(chani,fi,:,condi,1) = abs(mean(exp(1i*(  angle(EEG_multi_conv{1}(t1:t2,VECTOR==X_CUE{condi}))  )),2));

                            ITPC(chani,fi,:,condi,2) = abs(mean(exp(1i*(  angle(EEG_multi_conv{2}(t1:t2,VECTOR_resp==X_RESP{condi}))  )),2));

                        end

                         

                        % dB correct power by base

                        for condi=1:2

                            for event=1:2

                                POWER(chani,fi,:,condi,event) = 10*( log10(temp_POWER_avg(:,condi,event))      - log10(repmat(BASE,size(temp_POWER_avg(:,condi,event),1),1)) );

                            end

                        end

                                                                   

                        % Actually, save these for later

                        Baselines(chani,fi,1)=BASE;

                                            

                        clear temp* EEG_multi_conv wavelet BASE PE;

                        

                    end

                    clear *_fft;

                end

                

                % % ---------- % ---------- % ----------

                % ---------- ERP stuff

                % ---------- % ---------- % ----------  

                

                % Filter for ERPs

                dims=size(EEG.data); EEG.data=eegfilt(EEG.data,500,[],20); EEG.data=reshape(EEG.data,dims(1),dims(2),dims(3));

                dims=size(EEG.resp); EEG.resp=eegfilt(EEG.resp,500,[],20); EEG.resp=reshape(EEG.resp,dims(1),dims(2),dims(3));



                % Basecor your ERPs here so they are pretty ------------>

                % You can also remove this if you want...

                BASE1=squeeze(  mean(EEG.data(:,b1:b2,:),2)  );

                BASE2=squeeze(  mean(EEG.resp(:,b1:b2,:),2)  );

                for chani=1:dims(1)

                    EEG.data(chani,:,:)=squeeze(EEG.data(chani,:,:))-repmat( BASE1(chani,:),dims(2),1 );

                    EEG.resp(chani,:,:)=squeeze(EEG.resp(chani,:,:))-repmat( BASE2(chani,:),dims(2),1 );

                end

               

                for condi=1:2

                    % Mean for ERPs

                    ERPs(1:60,:,condi,1)=mean(EEG.data(1:60,t1:t2, VECTOR==X_CUE{condi} ),3);

                    ERPs(1:60,:,condi,2)=mean(EEG.resp(1:60,t1:t2, VECTOR_resp==X_RESP{condi} ),3);

                end

            % %     

                %%%%% save results                                     

                save([savedir,num2str(subj),'_Session_',num2str(session),'_PDDys_CC_ALL_GOODS.mat'],'ERPs','VECTOR','VECTOR_resp','POWER','ITPC','Baselines');

                

                clearvars -except datalocation ONOFF PDsx CTLsx session subj num_cc txt_cc raw_cc savedir

                

            end

        end

    end

end