%% Federico Lombardi
%% 17/09/2019
%% Extract traces from CPMGvs2tau
%% Feed the a few traces into a Mathematica file to fit it 
%% and analyse the dependance on a possible stretch exponential

clear
%% Import file
fileList = dir('*.DTA');
Number_of_pulses = 420;
%% Cycle
for i =1:length(fileList)
    filename = fileList(i).name;
    [time,spc] = eprload(filename);
    spc = real(spc);
    spc = spc*-1;
    tau = 2*str2double(filename(end-8:end-6));
    [t, intensity] = CPMG_integrator_peaks(time, spc, tau, Number_of_pulses);
    t = t(3:end);
    intensity = intensity(3:end);
    intensity = intensity - mean(spc(end-20,end));
    B = [t intensity];
    plot(time,spc)
    hold on
    plot(t,intensity)
    filename1 = [filename(1:end-4) '.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');
end
