|
|
|
|
| clear; clc
|
| 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\PostError\'; % 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');
|
|
|
| PDsx=[801:811,813:823,825: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= 801%[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;
|
|
|
| % It will added 3 column at the EEG.epochs with TYPE, RESP and RT...this code will find STIM CONG and INCONG and Response and latency of Response
|
|
|
| % &&&&&&&&& Get the good info out of the epochs &&&&&&&&&&
|
| for aai=1:size(EEG.epoch,2) % look for total size of epoch..how many epochs are there
|
| EEG.epoch(aai).TYPE=NaN; EEG.epoch(aai).RESP=NaN; EEG.epoch(aai).RT=NaN; % add TYPE; RESP and RT column at the end of epoch
|
| RESP_VECTOR(aai,1:2)=NaN; % create variable "RESP_VECTOR" for all epochs for Correct and Error resp
|
| for bbi=1:size(EEG.epoch(aai).eventlatency,2) % look the size of each epoch via eventlatency..
|
| %%% Get STIMTYPE
|
| if EEG.epoch(aai).eventlatency{bbi}==0 % If this bi is the event % if the eventlatency shows "0"in each epoch..means data were epchoed ("0") at stim...then
|
| %%% Get StimType
|
| FullName=EEG.epoch(aai).eventtype{bbi}; % look the name for the eventtype for each event/stimuli for each epoch
|
| %%% IF TRN CUE (Training Cue) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%looking for the TRAINING CUE
|
| if any(str2num(FullName(2:end))==[CONGRU,INCONGRU]) % if any Fullname of stim match to Cong and Incong stim..then
|
| EEG.epoch(aai).TYPE=str2num(FullName(2:end)) ; % mention the full name in TYPE coulmn...ELSE..send to them in VECTOR variable
|
| if any(str2num(FullName(2:end))==CONGRU) % if any Fullname stim match to Cong then add 5 in Vector
|
| VECTOR(aai)=55; % CONGRUENT
|
| elseif any(str2num(FullName(2:end))==INCONGRU) % if any Fullname stim match to Incong then add 6 in Vector
|
| VECTOR(aai)=66; % INCONGRUENT
|
| end
|
| % %%% If anything is next
|
| if size(EEG.epoch(aai).eventlatency,2)>= bbi % if the size of eventtype is greater or equal to size of latency for each epoch then
|
| %%% If RESP &&& Simplely look for RESP after the STIM that's why eventtype+1..if there is resp after stim
|
| 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
|
|
|
|
|
|
|
|
|
| for aai=1:size(EEG.epoch,2)
|
| EEG.epoch(aai).resp_num = 0;
|
| EEG.epoch(aai).VECTOR = VECTOR(aai);
|
| if any(EEG.epoch(aai).RESP==CORRECT)
|
| EEG.epoch(aai).resp_num = 1;
|
| elseif any(EEG.epoch(aai).RESP==ERROR)
|
| EEG.epoch(aai).resp_num = 2;
|
| end
|
| EEG.epoch(aai).VECTOR2= EEG.epoch(aai).VECTOR;
|
| end
|
|
|
|
|
|
|
| for aai=1:length(EEG.epoch)
|
|
|
| if any(EEG.epoch(aai).resp_num == 1)
|
| if EEG.epoch(aai).VECTOR2 == 55
|
| EEG.epoch(aai).VECTOR2 = 5;
|
| elseif EEG.epoch(aai).VECTOR2 == 66
|
| EEG.epoch(aai).VECTOR2 = 5;
|
| end
|
| end
|
|
|
| if any(EEG.epoch(aai).resp_num == 2) && any(EEG.epoch(aai+1).resp_num == 1)
|
| if EEG.epoch(aai+1).VECTOR2 == 55
|
| EEG.epoch(aai+1).VECTOR2 = 6;
|
| elseif EEG.epoch(aai+1).VECTOR2 == 66
|
| EEG.epoch(aai+1).VECTOR2 = 6;
|
| end
|
| end
|
|
|
| end
|
|
|
| VECTOR_PE= [EEG.epoch.VECTOR2];
|
|
|
|
|
| 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);
|
|
|
| if subj==824 && session==2, clear bad_epochs; bad_epochs{1}=zeros(1,size(EEG.data,3)); end
|
| 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);
|
|
|
|
|
|
|
|
|
| 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;
|
|
|
|
|
| RespID = zeros(length(VECTOR_resp),3);
|
| RespID(:,1) = VECTOR_resp(:,1);
|
| RespID(:,2) = VECTOR_resp(:,2).*2;
|
|
|
|
|
| for rpi=1:length(RespID(:,1))-1
|
| if any(RespID(rpi,1)==1) && any(RespID(rpi+1,1)==1)
|
| RespID(rpi+1,3)= 1;
|
| elseif any(RespID(rpi,1)==2) && any(RespID(rpi+1,1)==1)
|
| RespID(rpi+1,3)= 2;
|
| end
|
| end
|
|
|
| VECTOR_respPE = [RespID(:,3) RespID(:,2)];
|
|
|
|
|
|
|
| tx=-1500:2:4998;
|
| b1=find(tx==-500); b2=find(tx==-200);
|
| t1=find(tx==-500); t2=find(tx==1000);
|
| tx2disp=-500:2:1000;
|
|
|
|
|
| X_CUE{1}=5;
|
| X_CUE{2}=6;
|
| X_RESP{1}=1;
|
| X_RESP{2}=2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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;
|
|
|
|
|
| n_wavelet = length(t);
|
| half_of_wavelet_size = (n_wavelet-1)/2; clear dims
|
|
|
| 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}));
|
|
|
| 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
|
|
|
|
|
| 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} );
|
|
|
|
|
| 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
|
|
|
|
|
| BASE = mean(mean(abs( EEG_multi_conv{1}(b1:b2,VECTOR_PE<7)).^2));
|
|
|
|
|
| for condi=1:2
|
| temp_POWER_avg(:,condi,1) = mean(temp_POWER{1}(:,VECTOR_PE==X_CUE{condi}),2);
|
| temp_POWER_avg(:,condi,2) = mean(temp_POWER{2}(:,VECTOR_respPE==X_RESP{condi}),2);
|
|
|
| ITPC(chani,fi,:,condi,1) = abs(mean(exp(1i*( angle(EEG_multi_conv{1}(t1:t2,VECTOR_PE==X_CUE{condi})) )),2));
|
| ITPC(chani,fi,:,condi,2) = abs(mean(exp(1i*( angle(EEG_multi_conv{2}(t1:t2,VECTOR_respPE==X_RESP{condi})) )),2));
|
|
|
| end
|
|
|
|
|
| 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
|
|
|
|
|
| Baselines(chani,fi,1)=BASE;
|
|
|
| clear temp* EEG_multi_conv wavelet BASE PE;
|
|
|
| end
|
| clear *_fft;
|
| end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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));
|
|
|
|
|
| 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
|
|
|
| ERPs(1:60,:,condi,1)=mean(EEG.data(1:60,t1:t2, VECTOR_PE==X_CUE{condi} ),3);
|
| ERPs(1:60,:,condi,2)=mean(EEG.resp(1:60,t1:t2, VECTOR_respPE==X_RESP{condi} ),3);
|
|
|
| end
|
|
|
| save([savedir,num2str(subj),'_Session_',num2str(session),'_PDDys_CC_ALL_GOODS.mat'],...
|
| 'ERPs','VECTOR','VECTOR_PE','VECTOR_resp','VECTOR_respPE','POWER','ITPC','Baselines');
|
|
|
| clearvars -except datalocation ONOFF PDsx CTLsx session subj num_cc txt_cc raw_cc savedir
|
|
|
| end
|
| end
|
| end
|
| end
|
|
|