function [FWHM, slope, energy]= width_FWHM(t,f,data,peak_amp,peak_lat)
FWHM=0;slope=0;energy=0;
T=find(abs(t-peak_lat)<1/(2*f));
if abs(data(T-1))>abs(peak_amp)
    FWHM=NaN;slope=NaN;energy=NaN;
else
    
    % figure; plot(data)
    
    T1=T;T2=T;
    while (data(T1-1)-peak_amp/2)*(data(T1)-peak_amp/2)>0
        T1=T1-1;
        if T1==1
            FWHM=NaN;
            break;
        end
    end
    while (data(T2-1)-peak_amp/2)*(data(T2)-peak_amp/2)>0 
        T2=T2+1;
        if T2>size(t)
            FWHM=NaN;
            break;
        end
    end
    if isnan(FWHM)
    else
        FWHM=(T2-T1)/f;
    end
    
    T1=T;T2=T;
    while (data(T1-1)-0.8*peak_amp)*(data(T1)-0.8*peak_amp)>0
        T1=T1-1;
        if T1==1
            slope=NaN;
            break;
        end
    end
    while (data(T2-1)-0.2*peak_amp)*(data(T2)-0.2*peak_amp)>0 
        T2=T2-1;
        if T2==1
            slope=NaN;
            break;
        end
    end
    if isnan(slope)
    elseif (T1-T2)~=0
        slope=0.6*peak_amp/((T1-T2)/f)/1000;
    end   
    
    % by TM, 05-06-2015
    
    T1=T;T2=T;
    while (data(T1-1)-0)*(data(T1)+0)>0
        T1=T1-1;
        if T1==1
            energy=NaN;
            break;
        end
    end
    while (data(T2-1)-0)*(data(T2)+0)>0
        T2=T2+1;
        if T2>size(t)
            energy=NaN;
            break;
        end
    end
    if isnan(energy)
    elseif (T1-T2)~=0
        energy=sum(data(T1:T2).*1/f); %figure; plot(data(T1:T2))
    end
    
    %
    
end
    


