function main_getAVGTrialFirst()
clear all;
for hz=[10]
    %     %120-9hz
    if hz==9
        EEG = pop_loadset('filename','sbj9_session_A8.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %replace H26 by H25
        %EEG.data(250,:,:) = EEG.data(249,:,:);
        EEG2 = pop_loadset('filename','sbj9_session_B6.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %replace H26 by H25
        %EEG2.data(250,:,:) = EEG2.data(249,:,:);
        EEG3 = pop_loadset('filename','sbj9_session_C10.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %replace H26 by H25
        %EEG3.data(250,:,:) = EEG3.data(249,:,:);
        EEG4 = pop_loadset('filename','sbj9_session_D7.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %specialChannelToPrint = {'E31' 'EXG2' 'F32' 'G28'};
    elseif hz==10
        %120-10hz
        EEG = pop_loadset('filename','sbj9_session_A1_ok.set','filepath','/data/projects/yute/SSVEP/subject9/');
        EEG2 = pop_loadset('filename','sbj9_session_B8_ok.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %replace H26 by H25
        %EEG2.data(250,:,:) = EEG2.data(249,:,:);
        EEG3 = pop_loadset('filename','sbj9_session_C6_ok.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %replace H26 by H25
        %EEG3.data(250,:,:) = EEG3.data(249,:,:);
        EEG4 = pop_loadset('filename','sbj9_session_D4_ok.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %replace H30 by H29
        %EEG4.data(254,:,:) = EEG4.data(253,:,:);
        %specialChannelToPrint = {'E29' 'E8' 'F32' 'G31'};
    elseif hz==11
        %120-11hz
        EEG = pop_loadset('filename','sbj9_session_A6.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG.data(250,:,:) = EEG.data(249,:,:);
        EEG2 = pop_loadset('filename','sbj9_session_B1.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG2.data(250,:,:) = EEG2.data(249,:,:);
        EEG3 = pop_loadset('filename','sbj9_session_C8.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG3.data(250,:,:) = EEG3.data(249,:,:);%H26
        %EEG3.data(211,:,:) = EEG3.data(210,:,:);%G19
        %EEG3.data(205,:,:) = EEG3.data(204,:,:);%G13
        %EEG3.data(202,:,:) = EEG3.data(200,:,:);%G10
        %EEG3.data(201,:,:) = EEG3.data(200,:,:);%G9
        %EEG3.data(198,:,:) = EEG3.data(197,:,:);%G6
        EEG4 = pop_loadset('filename','sbj9_session_D3.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG4.data(254,:,:) = EEG4.data(253,:,:);
        %specialChannelToPrint = {'E29' 'EXG7' 'F32' 'G28'};
    elseif hz==12
        %120-12hz
        EEG = pop_loadset('filename','sbj9_session_A9.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG.data(250,:,:) = EEG.data(249,:,:);%H26
        EEG2 = pop_loadset('filename','sbj9_session_B7.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG2.data(211,:,:) = EEG2.data(210,:,:);%G19
        %EEG2.data(202,:,:) = EEG2.data(200,:,:);%G10
        %EEG2.data(201,:,:) = EEG2.data(200,:,:);%G9
        %EEG2.data(198,:,:) = EEG2.data(197,:,:);%G6
        EEG3 = pop_loadset('filename','sbj9_session_C2.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG3.data(250,:,:) = EEG3.data(249,:,:);
        EEG4 = pop_loadset('filename','sbj9_session_D8.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %specialChannelToPrint = {'E31' 'EXG1' 'F32' 'G28'};
    elseif hz==13
        %120-13hz
        EEG = pop_loadset('filename','sbj9_session_A5.set','filepath','/data/projects/yute/SSVEP/subject9/');
        EEG2 = pop_loadset('filename','sbj9_session_B9.set','filepath','/data/projects/yute/SSVEP/subject9/');
        EEG3 = pop_loadset('filename','sbj9_session_C7.set','filepath','/data/projects/yute/SSVEP/subject9/');
        %EEG3.data(250,:,:) = EEG3.data(249,:,:);%H26
        EEG4 = pop_loadset('filename','sbj9_session_D9.set','filepath','/data/projects/yute/SSVEP/subject9/');
        
    end
    %     %75-9hz
    %     EEG = pop_loadset('filename','sbj9_sessionA10_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %replace H26 by H25
    %     %EEG.data(250,:,:) = EEG.data(249,:,:);
    %     EEG2 = pop_loadset('filename','sbj9_sessionB2.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %replace H26 by H25
    %     EEG2.data(250,:,:) = EEG2.data(249,:,:);
    %     EEG3 = pop_loadset('filename','sbj9_sessionC3_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %replace H26 by H25
    %     EEG3.data(250,:,:) = EEG3.data(249,:,:);
    %     EEG4 = pop_loadset('filename','sbj9_sessionD5.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     specialChannelToPrint = {'E32' 'EXG2' 'F32' 'G28'};
    
    % %75-10hz
    %     EEG = pop_loadset('filename','sbj9_sessionA3_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %replace H26 by H25
    %     %EEG.data(250,:,:) = EEG.data(249,:,:);
    %     EEG2 = pop_loadset('filename','sbj9_sessionB4_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %replace H26 by H25
    %     EEG2.data(250,:,:) = EEG2.data(249,:,:);
    %     EEG3 = pop_loadset('filename','sbj9_sessionC1_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %replace H26 by H25
    %     EEG3.data(250,:,:) = EEG3.data(249,:,:);
    %     EEG4 = pop_loadset('filename','sbj9_sessionD10_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG4.data(146,:,:) = EEG4.data(145,:,:);
    %     specialChannelToPrint = {'E31' 'EXG7' 'F32' 'G28'};
    
    % %75-11hz
    %     EEG = pop_loadset('filename','sbj9_sessionA7.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG.data(250,:,:) = EEG.data(249,:,:);%H26
    %     %EEG.data(250,:,:) = EEG.data(249,:,:);%replace H26 by H25
    %     EEG2 = pop_loadset('filename','sbj9_sessionB5_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG2.data(250,:,:) = EEG2.data(249,:,:);%H26
    %     %EEG2.data(250,:,:) = EEG2.data(249,:,:);%replace H26 by H25
    %     EEG3 = pop_loadset('filename','sbj9_sessionC9_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG3.data(254,:,:) = EEG3.data(253,:,:);%H30
    %     EEG3.data(250,:,:) = EEG3.data(249,:,:);%H26
    %     EEG3.data(211,:,:) = EEG3.data(210,:,:);%G19
    %     EEG3.data(205,:,:) = EEG3.data(204,:,:);%G13
    %     EEG3.data(202,:,:) = EEG3.data(200,:,:);%G10
    %     EEG3.data(201,:,:) = EEG3.data(200,:,:);%G9
    %     EEG3.data(198,:,:) = EEG3.data(197,:,:);%G6
    %     %EEG3.data(250,:,:) = EEG3.data(249,:,:);%replace H26 by H25
    %     EEG4 = pop_loadset('filename','sbj9_sessionD6.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %EEG4.data(146,:,:) = EEG4.data(145,:,:);
    %     specialChannelToPrint = {'E27' 'EXG7' 'F32' 'G28'};
    
    % %75-12hz
    %     EEG = pop_loadset('filename','sbj9_sessionA4_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG.data(250,:,:) = EEG.data(249,:,:);%H26
    %     EEG2 = pop_loadset('filename','sbj9_sessionB10_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    % %     EEG2.data(250,:,:) = EEG2.data(249,:,:);%H26
    %     %EEG2.data(250,:,:) = EEG2.data(249,:,:);%replace H26 by H25
    %     EEG3 = pop_loadset('filename','sbj9_sessionC5.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG3.data(128,:,:) = EEG3.data(127,:,:);%D32
    %     EEG4 = pop_loadset('filename','sbj9_sessionD1.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG4.data(250,:,:) = EEG4.data(249,:,:);%H26
    %     specialChannelToPrint = {'E31' 'EXG7' 'F32' 'G28'};
    
    %75-13hz
    %     EEG = pop_loadset('filename','sbj9_sessionA2_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     %EEG.data(250,:,:) = EEG.data(249,:,:);%H26
    %     EEG2 = pop_loadset('filename','sbj9_sessionB3.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG2.data(250,:,:) = EEG2.data(249,:,:);%H26
    %     %EEG2.data(250,:,:) = EEG2.data(249,:,:);%replace H26 by H25
    %     EEG3 = pop_loadset('filename','sbj9_sessionC4_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG3.data(250,:,:) = EEG3.data(249,:,:);%H26
    %     EEG4 = pop_loadset('filename','sbj9_sessionD2_cleaned.set','filepath','/data/projects/yute/SSVEP/subject9/');
    %     EEG4.data(254,:,:) = EEG4.data(253,:,:);%H30
    %     specialChannelToPrint = {'E32' 'EXG1' 'F32' 'G28'};
    %% Average over sessions and trials
    %  @output: [nbchan*length_per_trials]
    specialChannelToPrint = {'E31' 'E7' 'F32' 'G1'};
    referenceChannel={'EXG4'};
    %rank: face->ear->occipital->neck
    specialChannelToPrintIndex =1;
    maxSNRChannelOverArea = ''; %be used in the end
    SNR_hz_we_want = hz;
    resolution = 4;% 4 = 0.25hz
    freqFrom = 8;
    freqTo = 15;
    
    trialTemp =  zeros([(EEG.trials+EEG2.trials+EEG3.trials+EEG4.trials) length(EEG.data(1,:,1))]);%initial the data_temp
    trialTempCounter = 1;
    AVG.data = zeros([EEG.nbchan length(EEG.data(1,:,1))]);
    channelArea = {'A1','A2','A3','A4','A5','A6','A7','A8','A9','A10','A11','A12','A13','A14','A15','A16','A17','A18','A19','A20','A21','A22','A23','A24','A25','A26','A27','A28','A29','A30','A31','A32','B1','B2','B3','B4','B5','B6','B7','B8','B9','B10','B11','B12','B13','B14','B15','B16','B17','B18','B19','B20','B21','B22','B23','B24','B25','B26','B27','B28','B29','B30','B31','B32','C1','C2','C3','C4','C5','C6','C7','C8','C9','C10','C11','C12','C13','C14','C15','C16','C17','C18','C19','C20','C21','C22','C23','C24','C25','C26','C27','C28','C29','C30','C31','C32','D1','D2','D3','D4','D5','D6','D7','D8','D9','D10','D11','D12','D13','D14','D15','D16','D17','D18','D19','D20','D21','D22','D23','D24','D25','D26','D27','D28','D29','D30','D31','D32','E1','E2','E3','E4','E5','E6','E7','E8','E9','E10','E11','E12','E13','E14','E15','E16','E17','E18','E19','E20','E21','E22','E23','E24','E25','E26','E27','E28','E29','E30','E31','E32','F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','F13','F14','F15','F16','F17','F18','F19','F20','F21','F22','F23','F24','F25','F26','F27','F28','F29','F30','F31','F32','G1','G2','G3','G4','G5','G6','G7','G8','G9','G10','G11','G12','G13','G14','G15','G16','G17','G18','G19','G20','G21','G22','G23','G24','G25','G26','G27','G28','G29','G30','G31','G32','H1','H2','H3','H4','H5','H6','H7','H8','H9','H10','H11','H12','H13','H14','H15','H16','H17','H18','H19','H20','H21','H22','H23','H24','H25','H26','H27','H28','H29','H30','H31','H32','EXG1','EXG2','EXG3','EXG4','EXG5','EXG6','EXG7','EXG8'};
    % if(EEG.nbchan ~= length(channelArea))
    %     fprintf('**************************\n');
    %     fprintf('EEG.nbchan ~= channelArea   (do we have EXG8?)\n');
    %     fprintf('**************************\n');
    %     return;
    % end
    for i=1:EEG.nbchan
        for trial=1:EEG.trials
            trialTemp(trialTempCounter,:) = EEG.data(i,:,trial);%sum each epoche
            trialTempCounter = trialTempCounter+1;
        end
        for trial=1:EEG2.trials
            trialTemp(trialTempCounter,:) = EEG2.data(i,:,trial);%sum each epoche
            trialTempCounter = trialTempCounter+1;
        end
        for trial=1:EEG3.trials
            trialTemp(trialTempCounter,:) = EEG3.data(i,:,trial);%sum each epoche
            trialTempCounter = trialTempCounter+1;
        end
        for trial=1:EEG4.trials
            trialTemp(trialTempCounter,:) = EEG4.data(i,:,trial);%sum each epoche
            trialTempCounter = trialTempCounter+1;
        end
        trialTempCounter=1;
        AVG.data(i,:) = mean(trialTemp,1);
        AVG.channelName(i) = channelArea(i);
    end
    AVG.specialChannelToPrint=specialChannelToPrint;
    AVG.referenceChannel = referenceChannel;
    AVG.chanlocs = EEG.chanlocs;
    
    %%
    
    x_axis = (freqFrom*resolution+1:freqTo*resolution+1);
    %check error
    if(SNR_hz_we_want<freqFrom || SNR_hz_we_want>freqTo)
        fprintf('*****Error, SNR_hz_we_want<freqFrom || SNR_hz_we_want>freqTo****\r\r');
        return;
    end;
    SNR_hz_we_want = SNR_hz_we_want*resolution+1;
    
    for channel=1:EEG.nbchan
        if(strmatch(referenceChannel,AVG.channelName(channel)))
            %fprintf('match');
            continue;
        end
        %     fft_result(channel,:) = abs(fft(AVG.data(channel,:)));
        %     %plot SNR= SNR_hz_we_want/mean(freqFrom->freqTo, excep SNR_hz_we_want)
        %     snr_sum =0;
        % %     snr_sum = sum(fft_result(channel,x_axis));
        % %     snr_result(channel,:) = fft_result(channel,x_axis)/snr_sum*100;
        %     snr_sum = sum(fft_result(channel,x_axis))-fft_result(channel,x_axis(SNR_hz_we_want-(freqFrom*resolution)));
        %     snr_result(channel,:) = fft_result(channel,x_axis)/(snr_sum);%snr_result(channel,:) = fft_result(channel,x_axis)/(snr_sum/(length(x_axis)-1));
        snr_result(channel,:) = SNR_multiFreq(AVG.data(channel,:),8,15,4);
        
        max_SNR_for_each_channel(channel) = snr_result(channel,SNR_hz_we_want-(freqFrom*resolution));
    end
    AVG.max_SNR_for_each_channel = max_SNR_for_each_channel;
    %main_printSNR_Decend(max_SNR_for_each_channel,AVG);
    
    figure();
    %for(i=1:4)
    figure_SNR=plot((x_axis-1)/resolution,snr_result(strmatch(specialChannelToPrint(1),AVG.channelName),:),'g',(x_axis-1)/resolution,snr_result(strmatch(specialChannelToPrint(2),AVG.channelName),:),'k',(x_axis-1)/resolution,snr_result(strmatch(specialChannelToPrint(3),AVG.channelName),:),'r',(x_axis-1)/resolution,snr_result(strmatch(specialChannelToPrint(4),AVG.channelName),:),'b');
    %hold on;
    %end
    title([EEG.setname, ' ',num2str((SNR_hz_we_want-1)/resolution),'Hz',' Ch= ' specialChannelToPrint{1},' ', specialChannelToPrint{2},' ', specialChannelToPrint{3},' ', specialChannelToPrint{4}]);%title(['SNR, Ch=',specialChannelToPrint]);
    legend_SNR=legend('face area','behind-ear area','occipital area','neck area');
    xlabel('Hz','fontsize',12);
    ylabel('SNR','fontsize',12);
    set(figure_SNR,'LineWidth',2);
    set(legend_SNR,'FontSize',12);
    set(gca,'FontSize',12)
    box off
    %ylim([0 35]);
    % code below is for topoplot()
    figure();
    topoplot(max_SNR_for_each_channel,EEG.chanlocs);%'/data/projects/yute/SSVEP/location_test.loc','electrodes','on');
    title([EEG.setname,' ',num2str((SNR_hz_we_want-1)/resolution),'Hz', ' Ch= ' specialChannelToPrint{1},' ', specialChannelToPrint{2},' ', specialChannelToPrint{3},' ', specialChannelToPrint{4}]);%title(strcat(EEG.setname,',Ch=',specialChannelToPrint));%title([EEG.setname ',Ch=',specialChannelToPrint]);
    colorbar();
    
    result=[snr_result(strmatch(specialChannelToPrint(1),AVG.channelName),:) ;
        snr_result(strmatch(specialChannelToPrint(2),AVG.channelName),:);
        snr_result(strmatch(specialChannelToPrint(3),AVG.channelName),:);
        snr_result(strmatch(specialChannelToPrint(4),AVG.channelName),:)];
    save (['sbj9_120_' num2str(hz)]);
    %save sbj9_75_13 AVG;
end
end