File size: 5,170 Bytes
03a0748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
%% EEG GO during Response task: TASK Post-processing: for Time-Frequency ananlysis
%%% DATA was Segmented from the GO
% Get the directories for pre-processed EEG file folder
clear; clc; close all;

cwd = pwd; 
addpath(cwd);

datalocation = [cwd '\ALL_DATA\ALL_Processed_Data\'];   % Data are here

savelocation = [cwd '\ALL_DATA\ALL_Processed_Data\'];   % Data save here

cd(datalocation);



[~, ~, SubjInfo] = xlsread([ cwd '\Patients_MS.xlsx'],'AllPedaling');





%%

for nn = 1: 2%size(SubjInfo,1)

        

    type = SubjInfo{nn,1};

    subj = SubjInfo{nn, 2};

    

    disp([type,num2str(subj),'_Ped_Processed.mat']);    

    load([type,num2str(subj),'_Ped_Processed.mat']);

    

    %Get Subj Info and remove the bad ICAs on the basis of VEOG correlation

    %Bad ICAS 1,2,3 are ADJUST, VEOG correlation, and Gaussian template.

    %ADJUST is a stand-alone program that isn't very good.
    %VEOG correlation is the most reliable and the gaussian template can be useful.  
    %APPLE VEOG corr is RESP about 85% of teh time.

   % % Get the data here and remove bad ICAs: VEOG and TEMPLATE
    temp1a = cell2mat(bad_ICAs(2));   % 2 = bad_VEOG_ICAs 
    temp1b = cell2mat(bad_ICAs(3));   % 3 = Bad TEMPLATE ICAs 
        temp1 = [temp1a temp1b];   
    tpidx = find (temp1==0);
    temp1(tpidx)=[];
    
    if isempty(temp1)
        temp1 = 'NaN';
    end

    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 temp1a temp1b temp1 tpidx;

    %%% 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 bad_chans bad_epochs bad_ICAs
                
     
    %% ###################################   TIME FREQUENCY ANALYSIS %% ########################################################
    
    % Set Times
    tx=EEG.times;
    b1=find(tx==-300);  b2=find(tx==-100);  %% Baseline
    t1=find(tx==-500);  t2=find(tx==2000);
    tx2disp=-500:2:2000;
    
    %%%%%%%%%%%%%%%%%%% Setup Wavelet Params %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    min_freq = 1.0233;
    max_freq = 50;
    num_freqs=50;
  
    %%%%% logspace
    frex = logspace(log10(min_freq),log10(max_freq),num_freqs); 
    s=logspace(log10(3),log10(10),num_freqs)./(2*pi*frex);
    
    %%%% timelength of the wavelet
    t=-2:1/EEG.srate:2;
    
    % Definte Convolution Parameters
    n_wavelet = length(t); half_of_wavelet_size = (n_wavelet-1)/2; clear dims
    % -------- GO
    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}));
    
    %%% decide channels
    channels = EEG.nbchan;
    for  chani = 1:channels
        % get FFT of data
        EEG_fft{1} = fft(reshape(EEG.data   (chani,:,:),1,n_data{1}),n_conv_pow2{1});
       
        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} );
           
                % convolution for 1 data set
                for convo=1
                    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 {1}
                 BASE = mean(mean(abs( EEG_multi_conv{1}(b1:b2,:)).^2));
                 
                 temp_POWER_avg(:,1) = mean(temp_POWER{1}(:,:),2); % GO
                
                % dB correct power by base
                % WARNING CUE; GO/GO Cue; RESPONSE
                  POWER(chani,fi,:,1) = 10*( log10(temp_POWER_avg(:,1)) - log10(repmat(BASE,size(temp_POWER_avg(:,1),1),1)) ); % GO
                 clear temp* EEG_multi_conv wavelet BASE1 BASE2;
        end
        clear *_fft;
    end
    
    % plot figure
%     ChanCz = find(strcmpi('Cz',{EEG.chanlocs.labels}));
%     yfreq = 50;
%     
%     imagesc(tx2disp,[], squeeze( POWER(ChanCz,:,:,1))); axis xy; hold on; plot([0 0],[1 yfreq],'k:');
%     set(gca,'xlim',[tx2disp(1),tx2disp(end)],'ylim',[1 50],'clim',[-4 4],'YTick',1:4:length(frex),'YTickLabel',round(frex(1:4:end)));  title('Cz: POWER');
%     suptitle([type,num2str(subj)]);
%     saveas(gcf, [savelocation, type,num2str(subj),'_ANALYZED.png']);
        
    %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SAVE DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    chanlocs = EEG.chanlocs;
    MainOuts.POWER = POWER; 
    
    save([savelocation, type,num2str(subj),'_ANALYZED.mat'],'MainOuts','chanlocs' );
     
   %%  Clear everything except some variables to run loop
   close all;
   clearvars -except datalocation savelocation SubjInfo
   
end