% sim_spinecho_xN.m
% Robin Simpson and Jamie Near, 2014.
% 
% USAGE:
% out = sim_spinecho(n,sw,Bfield,linewidth,sys,tau,Nechoes)
% 
% DESCRIPTION:
% This function simulates a multi-echo spin-echo experiment with 'Nechoes' instantaneous RF pulses.
% 
% INPUTS:
% n         = number of points in fid/spectrum
% sw        = desired spectral width in [Hz]
% Bfield    = main magnetic field strength in [T]
% linewidth = linewidth in [Hz]
% sys       = spin system definition structure
% tau       = echo time in [ms]
% Nechoes   = number of spin echoes (optional.  Default = 10);
%
% OUTPUTS:
% out       = simulated spectrum, in FID-A structure format, using multi-echo 
%             spin-echo sequence.

function out = sim_spinecho_xN(n,sw,Bfield,linewidth,sys,tau,Nechoes)

if nargin<7
    Nechoes=10;
end

%Set water to centre
centreFreq=4.65;
for k=1:length(sys)
    sys(k).shifts=sys(k).shifts-centreFreq;
end

%Calculate Hamiltonian matrices and starting density matrix.
[H,d]=sim_Hamiltonian(sys,Bfield);

delay=tau/(2*Nechoes);

%BEGIN PULSE SEQUENCE************
d=sim_excite(d,H,'x');                            %EXCITE
for k=1:Nechoes
    d=sim_evolve(d,H,delay/1000);                        %Evolve by tau/2
    d=sim_rotate(d,H,180,'y');                       %180 degree refocusing pulse about y' axis.
    d=sim_evolve(d,H,delay/1000);                        %Evolve by tau/2
end
[out,dout]=sim_readout(d,H,n,sw,linewidth,90);  %Readout along y (90 degree phase);
%END PULSE SEQUENCE**************

%Correct the ppm scale:
out.ppm=out.ppm-(4.65-centreFreq);

%Fill in structure header fields:
out.seq=['spinecho_x' num2str(Nechoes)];
out.te=tau;
out.sim='ideal';

%Additional fields for compatibility with FID-A processing tools.
out.sz=size(out.specs);
out.date=date;
out.dims.t=1;
out.dims.coils=0;
out.dims.averages=0;
out.dims.subSpecs=0;
out.dims.extras=0;
out.averages=1;
out.rawAverages=1;
out.subspecs=1;
out.rawSubspecs=1;
out.flags.writtentostruct=1;
out.flags.gotparams=1;
out.flags.leftshifted=0;
out.flags.filtered=0;
out.flags.zeropadded=0;
out.flags.freqcorrected=0;
out.flags.phasecorrected=0;
out.flags.averaged=1;
out.flags.addedrcvrs=1;
out.flags.subtracted=1;
out.flags.writtentotext=0;
out.flags.downsampled=0;
out.flags.isFourSteps=0;

