% Chirp Echo FT EPR-detected NMR
%
% 11/2017 Nino Wili: nino.wili@phys.chem.ethz.ch
% EPR @ ETH Zurich

% Plot of "EDNMR" spectrum of nitroxide detected w/ chirp echoes

clear, close all;

%load data with function tailored to our AWG spectrometer
options.plot = 0; % 0 for do not plot by uwb_eval
options.evlen = 5500; % length of evaluation window in points
data=uwb_eval('20170831_1000_chirp_echo_ampsweep1',options);

%extract needed data from struct
t=data.t_ax; %time axis
sig=data.dta_avg; %averaged echo transients

%apodization
window=chebwin(numel(t));
sig_apo=sig.*window;

%Find echo maximum and extract phase for the reference spectrum 
[~,poi]=max(abs(sig_apo(:,1)));
phase0=angle(sig_apo(poi,1)); 
%loop over all spectra in data
for ii=1:size(sig,2)
   y=sig_apo(:,ii);%loop copy for convenience
   zero_filler=round(size(y)/2); %zero filler at start and end of echo
   y=[zeros(zero_filler); y; zeros(zero_filler)]; 
   y=circshift(y,-(poi+zero_filler)); %circular shift (=1st order phase correction)
   y=y.*exp(-1i*phase0); %oth order phase correction (usually irrelevant)
   %calculate spectra by FT
   spc(:,ii)=real(fftshift(fft(y))); 
end

%calculate ednmr spectrum as difference of spectra w/a and w/o HTA pulse
ednmr=(real(spc(:,1))-real(spc(:,2))); 
ednmr=ednmr/max(ednmr);

%calculate frequency axis
center_frq=data.exp.events{1}.pulsedef.nu_init;%frequency of HTA pulse
dt=t(2)-t(1); %time step
nyq=1/(2*dt); %nyquist frequency from time step
freq=linspace(-nyq,nyq,size(spc,1))+data.det_frq-center_frq;



%Plot
h=figure(1);
clf
%plot spectrum
plot(freq*1e3,ednmr,'k','linewidth',2);
%make nice
h=niwi_niceplot(h,[15 8]);
box off
hold on
%plot zoom
plot(freq*1e3,ednmr*10+0.2,'k','linewidth',1);

%annotations and changes of figure appearance
text(-78,0.4,'x10')
text(-54,1.55,'$^{1}$H','Horizontalalignment','center','verticalalignment','bottom')
text(52,1.55,'$^{1}$H','Horizontalalignment','center','verticalalignment','bottom')

p14N1=[-38 1];
plot([-38 p14N1(1)],[0.4 p14N1(2)],'k'); 
plot([-30 p14N1(1)],[0.8 p14N1(2)],'k');
plot([-23 p14N1(1)],[0.9 p14N1(2)],'k');
text(p14N1(1)-0.005,p14N1(2),'$^{14}$N','Horizontalalignment','right','verticalalignment','bottom')

p14N2=[40 1];
plot([38 p14N2(1)],[0.75 p14N2(2)],'k'); 
plot([30 p14N2(1)],[0.85 p14N2(2)],'k');
plot([23 p14N2(1)],[0.9 p14N2(2)],'k');
text(p14N2(1)-0.005,p14N2(2),'$^{14}$N','Horizontalalignment','center','verticalalignment','bottom')

pcombi=[70 1];
plot([66 pcombi(1)],[0.8 pcombi(2)],'k'); 
plot([43 pcombi(1)],[0.6 pcombi(2)],'k');
text(pcombi(1)-0.005,pcombi(2),'$^{14}$N+$^{1}$H','Horizontalalignment','center','verticalalignment','bottom')

p13C=[-10 1.2];
plot([12 p13C(1)],[0.65 p13C(2)],'k'); 
plot([-12 p13C(1)],[0.65 p13C(2)],'k');
text(p13C(1)-0.005,p13C(2),'$^{13}$C','Horizontalalignment','right','verticalalignment','bottom')

% set(gca,'Ytick',[])
xlabel('$( \nu - \\nu_{\\text{HTA}} )$ / MHz')
ylabel('Hole Depth / a.u.')
set(gca,'Ytick',[])
axis([-85 85 -0.04 1.54])
set(findall(h,'-property','Fontsize'),'Fontsize',12); % change linewidth

%only needed to integrate it into latex, needs inkscape
% niwi_Plot2LaTex(h,'chirp_ednmr_vs_classical_chirp');