function [ST] = Basis_regressors(EEG,ST)
ST.regressor=[];
if ST.method_type==5  
    T=find(ST.f_T*1000>=ST.interval(1)&ST.f_T*1000<=ST.interval(2));
    dat=squeeze(mean(ST.f_trials(T,:),2));
    for i=1:size(ST.peak,1)
        T_peak(i)=find(abs(ST.f_T*1000-ST.peak(i,1))==min(abs(ST.f_T*1000-ST.peak(i,1))))-T(i)+1;    
    end
    t=ST.f_T(T);
else
    T=find(EEG.times>=ST.interval(1)&EEG.times<=ST.interval(2));
    dat=squeeze(mean(EEG.data(ST.channel_index,T,:),3));
    for i=1:size(ST.peak,1)
        T_peak(i)=find(abs(EEG.times-ST.peak(i,1))==min(abs(EEG.times-ST.peak(i,1))))-T(i)+1;    
    end
    t=EEG.times(T);
end

T_peak1=sort(T_peak,'ascend');
for i=1:length(T_peak1)
    V_peak1(i)=ST.peak(find(T_peak==T_peak1(i)),2);
end

%%
if T_peak1>=2
    for i=1:length(T_peak1)-1
        for j=T_peak1(i):T_peak1(i+1)
            if dat(j)*dat(j+1)<0
                zeropoint(i)=j;
            end
        end
    end
end

if length(T_peak1)==1
        avg(1,:)=dat;
elseif length(T_peak1)==2
    avg(1,:)=dat;avg(1,zeropoint(1):end)=0;
    avg(2,:)=dat;avg(2,1:zeropoint(1))=0;
else    
    avg(1,:)=dat;avg(1,zeropoint(1):end)=0;
    avg(size(ST.peak,1),:)=dat;avg(size(ST.peak,1),1:zeropoint(end))=0;
    for i=2:size(ST.peak,1)-1    
        avg(i,:)=dat;avg(i,1:zeropoint(i-1))=0;avg(i,zeropoint(i):end)=0;     
    end
end
%% 
Fs=round(EEG.srate);
number_trial=round(Fs*0.05); %% the default latency jitter limit
if Fs>500
    index_=3;%% if out of memory, this number should be increased
elseif Fs<=500
    index_=1; 
end
k=1:index_/(number_trial-1):2; %% the default compression limit
for i=1:size(avg,1)
    peak=T_peak1(i);
%     data=zeros(length(k),length(dat));    
%     for j=1:length(k)
%         T(j,:)=t/k(j);
%         ur = resample(double(avg(i,:)),round(Fs/k(j)),Fs);
%         peak_point=find(abs(ur-V_peak1(i))==min(abs(ur-V_peak1(i))));%% peak point
%         if peak-peak_point<=0
%             data(j,1:length(ur))=ur; 
%         else 
%             data(j,peak-peak_point+1:peak-peak_point+length(ur))=ur;  
%         end          
%     end
%     data1=zeros(length(k),length(t)+length(k));
%     for j=1:length(k)
%         data1(j,j:length(t)+j-1)=avg(i,:);    
%     end
%     data1=data1(:,round(length(k)/2)+1:length(t)+round(length(k)/2));
%     DATA=[data;data1];

    data=zeros(length(k),length(dat));
    for j=1:length(k)
        T(j,:)=t/k(j);
        ur = resample(double(avg(i,:)),round(Fs/k(j)),Fs);
%         peak_point=find(abs(ur-V_peak1(i))==min(abs(ur-V_peak1(i))));%% peak point
        peak_point=floor(T_peak1(i)*round(Fs/k(j))/Fs);
        if peak-peak_point<=0
            data(j,1:length(ur))=ur; 
        else
            data(j,peak-peak_point+1:peak-peak_point+length(ur))=ur;  
        end
    end
    DATA=[];
    for j=1:length(k)
        data1=zeros(length(k),length(t)+length(k));
        for ii=1:length(k)
            data1(ii,index_*ii:length(t)+index_*ii-1)=data(j,:);    
        end
        data1=data1(:,round(length(k)/2)*index_+1:length(t)+round(length(k)/2)*index_);
        DATA=[DATA;data1];
    end
    [W,Y,val] = pca(DATA);
    for kk=1:3  ST.regressor{i}(:,kk)=real(Y(kk,:));end
    ST.PCA_val(i)=sum(val(1:3))/sum(val(1:end));
    clear peak data data1 DATA Y W val;
end
