function [sigIndex,noise,noise1,noise2,noise3,CORS1,CORS2,CORS3,FDS,CDS,EPYS,FREPS]=removeNoiseComponent(Comp_Num,Dim,ICA_Prefix,T_Threshold,outDir,extPara)



maskFile=fullfile(nic_rsdd_misc('WHEREIS'),'data','mask3.mat');
load(maskFile);

pathdir=outDir;

key='tmap';
key_tc='mean';

 componentFiles=dir(fullfile(pathdir,key_tc,[ICA_Prefix,'_',key_tc,'_component_*']));
 if(length(componentFiles)>0)
     delete(fullfile(pathdir,key_tc,[ICA_Prefix,'_',key_tc,'_component_*']));
 end
 
[data1,head]=nic_ReadNiftiImage(fullfile(pathdir,key_tc,[ICA_Prefix,'_',key_tc,'_all_timecourses_ica_.img']));


ccsf=reshape(ccsf,61,73,61);
cgrey=reshape(cgrey,61,73,61);
cwhite=reshape(cwhite,61,73,61);


cgrey=reshape(cgrey,[],1);
ccsf=reshape(ccsf,[],1);
cwhite=reshape(cwhite,[],1);

CDS=[];
EPYS=[];
FDS=[];
FREPS=[];
NOISEI=[];
CORS1=[];
CORS2=[];
CORS3=[];
for i=1:Comp_Num
    str=fullfile(pathdir,key,sprintf([ICA_Prefix,'_',key,'__component_ica_%03d.img'],i));
    data=nic_ReadNiftiImage(str);
%     data=data(:,:,14:48);%-36mm~72mm
    data=reshape(data,[],1);
    dd=myZscore(data);
    dd(find(dd<T_Threshold))=0;
    cor1=corr(ccsf,dd);
    cor2=corr(cgrey,dd);    
    cor3=corr(cwhite,dd);   
    CORS1=[CORS1 cor1];
    CORS2=[CORS2 cor2];
    CORS3=[CORS3 cor3];
    if(cor1>=cor2 | cor3>=cor2)
        fprintf(1,'i:%d,  noise\n',i);
        NOISEI=[NOISEI i];
    end
    dd1=reshape(dd,Dim);
%     dd1=dd(find(dd>0));
    dd1(find(dd1>0))=1;
    
    BW=bwlabeln(dd1,26);
    len=max(unique(BW));
    for j=1:len
        ss=length(find(BW==j));
        if(ss<=27)
            BW(find(BW==j))=0;
        end
    end
    CD=length(find(BW>0))/length(find(dd1>0));
    
    [edges optD]=getOptimalBinSize(dd);
    histd=hist(dd,edges);
    histd=histd/(prod(Dim));
    ss=0;
    for(j=1:length(histd))
        ss=ss+histd(j)*logg(histd(j));
    end

    enpy=abs(log(-ss));
%     FD=CD/(enpy+eps);
    
    CDS=[CDS CD];
    EPYS=[EPYS enpy];
    
    
    a=data1(:,i);
    tem = fft(a');
    l = size(a,1);
    sigft= [tem.*conj(tem)/l]';
    sigft=sigft(1:l/2);
    tr=2;
    
    sumfreq = sum(sigft);
    szsigft = length(sigft);
    neqfreq = 1/(2*tr);

    freqind5 = round(0.01/(neqfreq/szsigft));
    freqind6 = round(0.08/(neqfreq/szsigft));
    freq6 = sum(sigft(freqind5:freqind6));
    freq= freq6/sumfreq;
    FREPS=[FREPS, freq];
    
end

noise1=NOISEI;
EPYS=Scale(EPYS,0,1);
CDS=Scale(CDS,0,1);

FDS=CDS./(EPYS);
    
FDS(find(FDS==inf))=50;

mfd=median(CDS)/(median(EPYS));
m=floor(Comp_Num/4);
SFD=sort(FDS,'ascend');
if(mfd>SFD(m+2))
    thf=SFD(m+1);
else
    thf=min(mfd,SFD(m));
end
% thf
noise2=find(FDS<=thf);


mfd=median(FREPS);
m=floor(Comp_Num/4);
SFD=sort(FREPS,'ascend');
if(mfd>SFD(m+2))
    thf=SFD(m+1);
else
    thf=min(mfd,SFD(m));
end
thf=0.6;
noise3=find(FREPS<=thf);

a=union(NOISEI,noise2);
a=union(a,noise3);
noise=a;
sigIndex=[];
for i=1:Comp_Num
    if(length(find(noise==i))==0)
        sigIndex=[sigIndex,i];
    end
end



function x1=logg(x)
if(x<eps)
    x1=0;
    return;
end
x1=log2(x);
return;

function [cor]=mycorr(data,label)
    [nFea nsample]=size(data);% 样本数目
    meand=mean(data,2);%均值
    meanl=mean(label);%均值
    x0=data-repmat(meand,1,nsample);
    y0=label-meanl;
    
    stdl=norm(y0);
    cor=zeros(nFea,1);
    
    for i=1:nFea
        stdd=norm(x0(i,:));
        if(stdd<eps)
            stdd=0;
            cor(i)=0;
            continue;
        end
         cor(i)=x0(i,:)*y0/(stdd*stdl);   
    end
    
return;

function data=myZscore(data)

mask_ind = (data ~= 0);
data(mask_ind) = detrend(data(mask_ind), 0);
v2 = data(mask_ind);
    
vstd = std(v2);
if vstd ~= 0
        data=data./(eps + vstd);
else
        disp('-- Not converting to z-scores as division by zero warning may occur.');
end

