function rcos_pulse

% Generate amplitude and (partial) tlan files for raised-cosine pulse

dt=1.2; % DDS amplitude time step (microseconds)
plen=100; % pulse length (microseconds)
pstart=100; % pulse start time in tlan file (microseconds)

npoints=ceil(plen/dt);

t=dt*(0:npoints-1);

a=0.5 - 0.5*cos(2*pi*t/plen);

figure
plot(t,a,'o-');
xlabel('Time \mu s');
ylabel('Amplitude');

% Write out the amplitude file

fid=fopen('rcos_pulse.amp','w');
if fid==-1
  error('cannot open amplitude file');
end

% header and first (tx off) amplitude
fprintf(fid,'AMPPAR_VS 1.0\n1 0\n');

% amplitude for our pulse
for k=1:npoints
  x=uint16(round(16383*a(k))); % 0x3FFF is maximum amplitude
  fprintf(fid,'%d 0x%04X\n',k+1,x);
end

% final tx off amplitude
fprintf(fid,'%d 0\n',npoints+2);

fclose(fid);

% Write out the pulse part of the tlan file

fid=fopen('rcos_pulse_part.tlan','w');
if fid==-1
  error('cannot open tlan file');
end

fprintf(fid,'AT      %.1f  RFON*,NOFLP*        %% Open SOUSY switch\n',pstart - 1);
for k=1:npoints
  fprintf(fid,'AT      %.1f  UPDt*               %% Kick in AMP(%d)\n', ...
          pstart+(k-1)*dt,k+1);
end
fprintf(fid,'AT      %.1f  UPDt*               %% Kick in final amplitude\n', ...
        pstart+npoints*dt);
fprintf(fid,'AT      %.1f  RFOFF*              %% Close SOUSY switch\n', ...
        pstart+npoints*dt+1);

fclose(fid);
