function rcos_ELFbpulse

% This version, ELFb, is for ELF frequencies below about 5 KHz
% Generates amplitude and tlan files for raised-cosine pulses
% For Chinese ELF/VLF from 20140303:
%  
%  The timing for multiple shaped pulses are put in each tlan file 
%  so that by repeating them less than 2^16 times (=loop counter) 
%  The phase of the modultion does not change within 10 minutes.

dt=1.1; % DDS amplitude time step (microseconds)
shapelen=100; % length (microseconds) of the shaped part of the pulse 
              % which is the length of half a period (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;   %us for 2017 Hz 
%tlname='2017Hz.tlan'
%%totreps=63680
%repeats=19;

%rep=1934.2;   %us for  517 Hz 
%tlname='517Hz.tlan'
%%totreps=62041
%repeats=5;

%rep=983.3; %us for 1017 Hz 
%tlname='1017Hz.tlan'
%%totreps=61019
%repeats=10;

%rep=331.5; %us for 3017 Hz 
%tlname='3017Hz.tlan'
%%totreps=60332
%repeats=30;

rep=248.9; %us for 4017 Hz 
tlname='4017Hz.tlan'
%totreps=60265
repeats=40;

iper=600;    % integration period

%totreps=iper*10^6/rep;
%repeats=totreps/62000;
%repeats=5;

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,'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  \n');
fprintf(fid,'%%%%  %s\n',tlname);
fprintf(fid,'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  \n');

for r=1:repeats
  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);    %% pulse rise
    end
  end

  for k=pkpoint+1:npoints
    fprintf(fid,'AT      %.1f    %s\n',pstart+(k-1)*dt+ceil(10*rep/2.0)/10.0-shapelen,txn);    %% pulse decay AMP(%d)\n',
  end


  fprintf(fid,'AT      %.1f    %s\n',pstart+npoints*dt+ceil(10*rep/2.0)/10.0-shapelen,txn); 
  fprintf(fid,'AT      %.1f  RFOFF*      %% Close SOUSY switch\n', pstart+npoints*dt+ceil(10*rep/2.0)/10.0-shapelen);

   if r==repeats
     fprintf(fid,'SETTCR    %.1f\n',0);
     fprintf(fid,'AT      %.1f  REP              %% ', repeats*rep);
   else
     fprintf(fid,'SETTCR    %.1f\n',r*rep);       
   end
end

fclose(fid);
