%% Step2: PostProcessing data for CORRECT trials only: ERP analysis; Time-Frequncy analaysis, ITPC analysis % Import preprocessed data. This analysis will look results for correct trials of Congruent % and Incongruent trials 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\CORRECT\'; % 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]) %%%%%%%%%%%%%%%%%%%%%% get Data only for CORRECT trials no Error trials 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; % %%%%%%%% Look for Congruent and Incongruent individually for Correct Response for ii = 1:size(EEG.epoch,2) RESP_VECTOR2(ii,1)= EEG.epoch(ii).TYPE; RESP_VECTOR2(ii,2)= EEG.epoch(ii).RESP; RESP_VECTOR2(ii,3)= EEG.epoch(ii).RT./ (1000/EEG.srate); end RESP_VECTOR3 = RESP_VECTOR2; %%% look for the NaN and delete that row RESP_VECTOR3(isnan(RESP_VECTOR3(:,2)),:) = []; %%%%% look for Error only and delete it ErrId = find (RESP_VECTOR3(:,2)==103 | RESP_VECTOR3(:,2)==104); RESP_VECTOR3(ErrId,2) = NaN; RESP_VECTOR3(isnan(RESP_VECTOR3(:,2)),:) = []; RESP_VECTOR3(:,2) = 1; %%%% now look for Cong and Incong in Only CORRECT trials RESP_VECTOR_STIM = RESP_VECTOR3; %%%% for Congruent correct trials RESP_VECTOR_STIM(RESP_VECTOR_STIM(:,1)>110 & RESP_VECTOR_STIM(:,1)<115) = 1; RESP_VECTOR_STIM(RESP_VECTOR_STIM(:,1)>210 & RESP_VECTOR_STIM(:,1)<215) = 1; %%%% for Inongruent correct trials RESP_VECTOR_STIM(RESP_VECTOR_STIM(:,1)>120 & RESP_VECTOR_STIM(:,1)<125) = 2; RESP_VECTOR_STIM(RESP_VECTOR_STIM(:,1)>220 & RESP_VECTOR_STIM(:,1)<225) = 2; %%%% creat VECTOR_resp for Cong and Incong VECTOR_respCI = RESP_VECTOR_STIM; VECTOR_respCI(:,2) = []; % 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_RESP{1}=1; % Congruent...Correct RESP X_RESP{2}=2; % Incongruent..Correct 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); % [36,21,7,22]; % FCz, Cz, C3, C4 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) = mean(temp_POWER{2}(:,VECTOR_respCI==X_RESP{condi}),2); % ------------------- ITPC(chani,fi,:,condi) = abs(mean(exp(1i*( angle(EEG_multi_conv{2}(t1:t2,VECTOR_respCI==X_RESP{condi})) )),2)); end % dB correct power by base for condi=1:2 POWER(chani,fi,:,condi) = 10*( log10(temp_POWER_avg(:,condi)) - log10(repmat(BASE,size(temp_POWER_avg(:,condi),1),1)) ); 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 % ---------- % ---------- % ---------- 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 ------------> BASE2=squeeze( mean(EEG.resp(:,b1:b2,:),2) ); for chani=1:dims(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,2)=mean(EEG.resp(1:60,t1:t2, VECTOR_respCI==X_RESP{condi} ),3); end %%%%% save results save([savedir,num2str(subj),'_Session_',num2str(session),'_PDDys_CC_ALL_GOODS.mat'],'ERPs','VECTOR','VECTOR_resp','VECTOR_respCI','POWER','ITPC','Baselines'); clearvars -except datalocation ONOFF PDsx CTLsx session subj num_cc txt_cc raw_cc savedir end end end end