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

% Plot of the Nitroxide chirp echo spectrum with and w/o HTA, Resonator

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
spc=spc/max(spc(:)); %normalize spectra

%calculate frequency axis
dt=t(2)-t(1); %time step
nyq=1/(2*dt); %nyquist frequency from time step
freq=linspace(-nyq,nyq,size(spc,1))+data.exp.LO+data.det_frq;

%Plot
h=figure(1);
hold on

%plot reference spectrum
plot(freq,spc(:,1),'k')

%annotation
nu_HTA=data.exp.events{1}.pulsedef.nu_init+data.exp.LO;
plot(nu_HTA*[1 1],[ 0.65 0.92],'--k')
text(nu_HTA,0.93,'$\nu_\\text{HTA}$','horizontalalignment','center','verticalalignment','bottom')
axis([34.64 35.06 -0.04 1.04])

%plot Resonator profile (which is also saved if resonator comp was used)
freq_res = data.exp.events{2}.pulsedef.resonator.range+data.exp.events{2}.pulsedef.resonator.LO;
res_nu1=data.exp.events{2}.pulsedef.resonator.nu1;
ch_res=28:66;
plot(freq_res(ch_res),res_nu1(ch_res)/max(res_nu1)*0.8,'r');
%plot axis of resonator profile by hand
max_nu1=max(res_nu1)*1e3;
xlims=xlim;
plot(xlims(2)*[1 1],[0 0.8],'r')
plot(xlims(2)+[0 -0.01],[1 1]*0.8,'r'); text(xlims(2)+0.01,0.8,'{\color{red}100}','horizontalalignment','left','verticalalignment','middle')
plot(xlims(2)+[0 -0.01],[1 1]*0.4,'r'); text(xlims(2)+0.01,0.4,'{\color{red}50}','horizontalalignment','left','verticalalignment','middle')
text(xlims(2),1,'{\color{red}$\\nu_1 /$ MHz}','horizontalalignment','center','verticalalignment','bottom')

%make nice lines set size
h=niwi_niceplot(h,[15 8]);

%plot holespectrum after niceplot
plot(freq,spc(:,2),'k')

% adjust figure
xlabel('$\nu$ / GHz')
ylabel('FT Intensity / a.u.')
set(findall(h,'-property','Fontsize'),'Fontsize',12); % change linewidth
set(gca,'ytick',[])
box off

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