function rcos_VLFapulse

% This version (rcos_VLFapulse.m), based on ELFb, is for VLF frequencies
%  above about 5 KHz
% Generates amplitude and tlan files for sinusoidal pulses
% For Chinese ELF/VLF from 20140304:
%  
%  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= 76.8 %us for 13017 Hz 
tlname='13017Hz.tlan'
pafname='p13017Hz.paf'
repeats=26;

%rep= 99.8 %us for 10017 Hz 
%tlname='10017Hz.tlan'
%pafname='p10017Hz.paf'
%repeats=38;

%rep=133.0 %us for 7517 Hz 
%tlname='7517Hz.tlan'
%pafname='p7517Hz.paf'
%repeats=29;

%rep=142.5 %us for 7017 Hz 
%tlname='7017Hz.tlan'
%pafname='p7017Hz.paf'
%repeats=27;

%rep=153.4 %us for 6517 Hz 
%tlname='6517Hz.tlan'
%pafname='p6517Hz.paf'
%repeats=25;

%rep=166.2 %us for 6017 Hz 
%tlname='6017Hz.tlan'
%pafname='p6017Hz.paf'
%repeats=23;

%rep=181.3 %us for 5517 Hz 
%tlname='5517Hz.tlan'
%pafname='p5517Hz.paf'
%repeats=21;

%rep=221.3 %us for 4517 Hz 
%tlname='4517Hz.tlan'
%pafname='p4517Hz.paf'
%repeats=26;

%rep=248.9 %us for 4017 Hz 
%tlname='4017Hz.tlan'
%pafname='p4017Hz.paf'
%repeats=23;

%rep=284.3 %us for 3517 Hz 
%tlname='3517Hz.tlan'
%pafname='p3517Hz.paf'
%repeats=20;

%rep=199.5 %us for 5017 Hz 
%tlname='5017Hz.tlan'
%pafname='p5017Hz.paf'
%repeats=50;

%rep=166.2;   %us for 6017 Hz 
%tlname='6017Hz.tlan'
%pafname='p6017Hz.paf'
%totreps=64466;
%repeats=56;

%rep=142.5;   %us for 7017 Hz 
%tlname='7017Hz.tlan'
%pafname='p7017Hz.paf'
%%totreps=64777
%repeats=65;

rep=124.8; %us for 8012 Hz 
tlname='8012Hz.tlan'
pafname='p8012Hz.paf'
%%totreps=65021
repeats=74;

%rep=124.7; %us for 8017 Hz 
%tlname='8017Hz.tlan'
%pafname='p8017Hz.paf'
%%totreps=65021
%repeats=74;

%rep=110.9; %us for 9017 Hz 
%tlname='9017Hz.tlan'
%pafname='p9017Hz.paf'
%%totreps=65184
%repeats=83;

%rep=99.8; %us for 10017 Hz 
%tlname='10017Hz.tlan'
%pafname='p10017Hz.paf'
%%totreps=65348
%repeats=92;

shapelen=rep;
iper=600;    % integration period

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

% amplitude for our pulse
for k=1:npoints-1
%  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);
 
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
  if r==1 
    fprintf(fid,'AT      %.1f  RFON*,NOFLP*        %% Open SOUSY switch\n',pstart);
  end
  for k=1:npoints-1
    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);
