function rcos_pulse_ua

% Generate amplitude and (partial) tlan files for raised-cosine pulse
% For Chinese ELF/VLF 20140228:
%  Note that the first UPDt command should be replaced by a STMCt command.
%

dt=1.1; % DDS amplitude time step (microseconds)
shapelen=100; % length (microseconds) of the shaped part of the pulse (total time for the rise and fall)
pstart=1.0; % pulse start time in tlan file (microseconds)
%elf_freq=1000;  % Hz
txn='UPDt*';  % for all transmitters

%rep=10^6/(elf_freq); % microseconds

%rep=495.9;   %for 2017 Hz 
%tlname='2017Hz.tlan'

%rep=1934.2   %for  517 Hz 
%tlname='517Hz.tlan'

%rep=983.3 %for 1017 Hz 
%tlname='1017Hz.tlan'

%rep=331.5 %for 3017 Hz 
%tlname='3017Hz.tlan'

%rep=248.9 %for 4017 Hz 
%tlname='4017Hz.tlan'

rep=166.2 %for 6017 Hz 
tlname='6017Hz.tlan'

npoints=ceil(shapelen/dt);
pkpoint=ceil(npoints/2);
t=dt*(0:npoints-1);

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

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

% Write out the amplitude file for 

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

% header and first (tx off) amplitude
% fprintf(fid,'AMPPAR_VS 2.0\n1 0\n');
fprintf(fid,'PAFPAR_VS 2.0\nBLOCKLEN 3 BYTES\n');

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

% final tx off amplitude
fprintf(fid,'%d A 0\n',npoints+1);
 
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,'%%%%  %s\n',tlname);
fprintf(fid,'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  \n');

fprintf(fid,'AT      %.1f  RFON*,NOFLP*        %% Open SOUSY switch\n',pstart);
for k=1:pkpoint
  if k==1
     fprintf(fid,'AT      %.1f    %s\n',pstart+(k-1)*dt,'STMCt*');
  else 
     fprintf(fid,'AT      %.1f    %s\n',pstart+(k-1)*dt,txn);    %% Kick in AMP(%d)\n', ...
  end
end

fprintf(fid,'SETTCR    %.1f\n',rep/2-shapelen);       %% Kick in final amplitude decay\n', ...
for k=pkpoint+1:npoints
  fprintf(fid,'AT      %.1f    %s\n',pstart+(k-1)*dt,txn);    %% Kick in AMP(%d)\n', ...
end


fprintf(fid,'AT      %.1f    %s\n',pstart+npoints*dt,txn);       %% Kick in final amplitude\n', ...
fprintf(fid,'AT      %.1f  RFOFF*              %% Close SOUSY switch\n', pstart+npoints*dt);
fprintf(fid,'SETTCR    %.1f\n',0);       %% Kick in final amplitude decay\n', ...
fprintf(fid,'AT      %.1f  REP              %% ', rep);

fclose(fid);
