% *****************************************************************
% 
%
%
% 
%
% *****************************************************************

%%
clear;
clc;

addpath .\UnderConfig

triLen = 10;

load('.\NN_dssall03_27_f6c6');
E1yb = yb;

chl = PKUtopoChNam('l');
chr = PKUtopoChNam('r');
close

%%
E1yb = E1yb(2*fs+[1:triLen*fs],:,:,:);
f=1:fs*triLen;f=f-1;f=f/triLen;

topoo    = [2*(fft(E1yb))/size(E1yb,1)];
E1spec   = abs(topoo).^2;
E1topoCh = (E1spec(:,2:3:end,:,:)+E1spec(:,3:3:end,:,:))/2;
E1topo   = squeeze(mean(E1topoCh,2));
%%

E1topom = double(mean(E1topo,3));
bloGros = [1 2 3 4]';

DIMs = [triLen/2+1 triLen*2/2+1 triLen*3/2+1 triLen*4/2+1 triLen*5/2+1];
clear pps fvalue
for dm = 1:size(DIMs,2)
    DIM = DIMs(dm);
    for blo = 1:size(bloGros,1)
        ss  = E1topom(DIM,blo)/mean(E1topom(DIM+[-1 1],blo));
        p   = fcdf(ss,2*16,4*16,'upper');
        pps(blo,dm) = p;
        fvalue(blo,dm) = ss;
    end
end
for blo = 1:size(pps,1)
    [h, crit_p, pps(blo,:)]=fdr_bh(pps(blo,:));
end

clear E1_txtv
E1_txtv{1,1} = 'same_alter';
E1_txtv{2,1} = 'diff_alter';
E1_txtv{3,1} = 'same_rando';
E1_txtv{4,1} = 'diff_rando';

for dm = 1:size(pps,2)
    for blo = 1:size(pps,1)
        E1_txtv{blo,dm+1}=[num2str(pps(blo,dm)) '/F:' num2str(fvalue(blo,dm))];
    end
end
E1_txtv

%%
E1topom = double(mean(E1topo,3));

DIMs = [triLen+1 triLen*2+1];
bloGros = [1 2 1 3 1 2;3 4 2 4 4 3]';
clear pps fvalue
for dm = 1:size(DIMs,2)
    DIM = DIMs(dm);
    for blo = 1:size(bloGros,1)
        ss = E1topom(DIM,bloGros(blo,1))/E1topom(DIM,bloGros(blo,2));
        [p] = fcdf(ss,2*16,2*16);
        if p < 0.5,p = p*2;else p = (1-p)*2;end
        pps(blo,dm) = p;
        fvalue(blo,dm) = ss;
    end
end
for dm = 1:size(pps,2)
    [h, crit_p, pps(:,dm)] = fdr_bh(pps(:,dm));
end
ppd = 10*log10(fvalue);

clear E1C1Com_txtv
E1C1Com_txtv{1,1} = 'same_alter:same_rando';
E1C1Com_txtv{2,1} = 'diff_alter:diff_rando';
E1C1Com_txtv{3,1} = 'same_alter:diff_alter';
E1C1Com_txtv{4,1} = 'same_rando:diff_rando';
E1C1Com_txtv{5,1} = 'same_alter:diff_rando';
E1C1Com_txtv{6,1} = 'diff_alter:same_rando';

for dm = 1:size(pps,2)
    for blo = 1:size(pps,1)
        E1C1Com_txtv{blo,dm+1}=[num2str(pps(blo,dm)) '/F:' num2str(fvalue(blo,dm))  '(db:' num2str(ppd(blo,dm)) ')'];
    end
end
E1C1Com_txtv

