%% Federico Lombardi
%% Average CPMG data for CS2

clear
Number_of_pulses = 440;
%% Import files
fileList = dir('*80K*408*.DTA');
filename = fileList(1).name;
[time, spc] = eprload(filename);
a = spc*0;
for i =1:length(fileList)
    filename = fileList(i).name;
    [time, spc] = eprload(filename);
    spc = real(spc);
    a = a+spc;
end
spc = spc*-1;
plot(time,spc)
tau = 2*str2double(filename(end-9:end-7));
[t, intensity] = CPMG_integrator_peaks(time, spc, tau, Number_of_pulses);
t = t(3:end);
intensity = intensity(3:end);
hold on 
plot(t,intensity)
B = [t intensity];
filename1 = [filename(1:end-5) '.tsv'];
fileID = fopen(filename1,'w');
fprintf(fileID,'%1s\t%15s\n','t','Echo Intensity');
fprintf(fileID,'%2s\t%2s\n','ns','');
fprintf(fileID,'%20s\t%3s\n','DFB1 DTOL', num2str(tau));
fclose(fileID);
dlmwrite(filename1, B,'delimiter','\t','-append');