function [ST] = peak_measure(EEG,ST)


if ST.method_type==5  
    T=find(ST.f_T*1000>=ST.interval(1)&ST.f_T*1000<=ST.interval(2));
    dat=squeeze(ST.f_trials(T,:));
    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(EEG.data(ST.channel_index,T,:));
    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

for i=1:length(ST.regressor)
    Reg(:,size(ST.regressor{i},2)*(i-1)+1:size(ST.regressor{i},2)*i)=ST.regressor{i}(:,1:size(ST.regressor{i},2));
end
Reg(:,end+1)=ones;

for i=1:size(dat,2)                                                         
    Coeffs(i,:)=(regress(dat(:,i),Reg))';         
end
ST.Coeffs=Coeffs;
nfunc=size(Reg,2);
ntrials=size(dat,2);
%Multiply design matrix y by parameters b for each epoch
for i=1:nfunc          
    for j=1:ntrials             
        fmult(:,i,j)=Coeffs(j,i).*Reg(:,i);
    end
end

%sum up the contributions         
Fits=squeeze(sum(fmult,2));

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


for i=1:size(Fits,2)
    [tmax tmin vmax vmin]=extreme_point(Fits(:,i)');
    for ii=1:length(V_peak1)
        Duration=round(EEG.srate*ST.peak_interval(ii)/1000);
        plim1=max([1 round(T_peak1(ii)-Duration/2)]);
        plim2=min([size(Fits,1)-1 round(T_peak1(ii)+Duration/2)]);
        Pmax=[tmax(intersect(find(tmax>=plim1),find(tmax<=plim2)));vmax(intersect(find(tmax>=plim1),find(tmax<=plim2)))];
        Pmin=[tmin(intersect(find(tmin>=plim1),find(tmin<=plim2)));vmin(intersect(find(tmin>=plim1),find(tmin<=plim2)))];
        Fit(i,:)=sum(fmult(:,size(ST.regressor{ii},2)*(ii-1)+1:size(ST.regressor{ii},2)*ii,i),2);
        temp=corrcoef(mean(dat,2),Fit(i,:));
        if V_peak1(ii)<=0
            if temp(1,2)>0
                if length(Pmin)>0
                    Ppeak=min(Pmin(2,:));lat=t(find(Fits(:,i)==Ppeak));
                    [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                elseif length(Pmin)==0
                    if length(Pmax)==0
                        Ppeak=NaN;lat=NaN;FWHM = NaN;slope=NaN;energy=NaN;
                    else
                        Ppeak=max(Pmax(2,:));lat=t(find(Fits(:,i)==Ppeak));
                        [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                    end
                end

            %% part 2    
            elseif temp(1,2)<0
                if length(Pmax)>0
                    Ppeak=max(Pmax(2,:));lat=t(find(Fits(:,i)==Ppeak));
                    [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                elseif length(Pmax)==0
                    if length(Pmin)==0
                        Ppeak=NaN;lat=NaN;FWHM = NaN;slope=NaN;energy=NaN;
                    else
                        Ppeak=min(Pmin(2,:));lat=t(find(Fits(:,i)==Ppeak));
                        [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                    end
                end
            end            
        elseif V_peak1(ii)>0
            if temp(1,2)>0
                if length(Pmax)>0
                    Ppeak=max(Pmax(2,:));lat=t(find(Fits(:,i)==Ppeak));
                    [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                elseif length(Pmax)==0
                    if length(Pmin)==0
                        Ppeak=NaN;lat=NaN;FWHM = NaN;slope=NaN;energy=NaN;
                    else
                        Ppeak=min(Pmin(2,:));lat=t(find(Fits(:,i)==Ppeak));
                        [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                    end
                end
            %% part 2    
            elseif temp(1,2)<0
                if length(Pmin)>0
                    Ppeak=min(Pmin(2,:));lat=t(find(Fits(:,i)==Ppeak));
                    [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                elseif length(Pmin)==0
                    if length(Pmax)==0
                        Ppeak=NaN;lat=NaN;FWHM=NaN;slope=NaN;energy=NaN;
                    else
                        Ppeak=max(Pmax(2,:));lat=t(find(Fits(:,i)==Ppeak));
                        [FWHM, slope, energy] = width_FWHM(t,EEG.srate,Fits(:,i),Ppeak,lat);
                    end
                end
            end
        end
        
      Value = goodnessOfFit(dat(:,i),Fits(:,i),'MSE');
      ST.goodnessOfFit(i)=Value;
      ST.amplitude(i,ii)=Ppeak;  
      ST.latency(i,ii)=lat;
      ST.FWHM(i,ii)=FWHM;
      ST.slope(i,ii)=slope;
      ST.energy(i,ii)=energy;
    end    
end



