clear
filename = '020_120K_dtol_448ns';
filename = [filename '.DTA'];
[time,intensity,params] = eprload(filename);
intensity = -real(intensity);
baseline = median(intensity);
intensity = intensity-baseline;
% plot(time,intensity)
% xlim([0 3200])
tau=448;

N =420;
x = ones(1,N)';
y = x;
time = time-tau*2-424+tau*2+16;
t = tau*2;
for ii = 1:N
    pos = find(time>(t-90) & time<(t+90));
    x(ii) = t;
    y(ii) = trapz(intensity(pos));
    t = t+2*tau;
end
plot(x,y/max(y))
hold on
yline(0);
plot(time,intensity/max(intensity),'-r')


A = [x y];
filename = filename(1:end-4);
filename = [filename, '.tsv'];
fileID = fopen(filename,'w');
fprintf(fileID,'%12s\t%18s\n','2tau','Echo Intensity');
fprintf(fileID,'%6s\t%12s\n','ns','arb.un.');
fprintf(fileID,'%6s\t%24s\n',' ', 'dtol 80K');
fclose(fileID);
dlmwrite(filename, A,'delimiter','\t','-append');