function rcos_VLFpulse

% This version, VLF, is for higher frequencies above about 5 KHz
% Generate amplitude and (partial) tlan files for raised-cosine pulse
% For Chinese ELF/VLF 201403-2:
%  
%  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=166666.0   %us for  6.0 Hz 
%tlname='6Hzb.tlan'
%repeats=6;

%rep=103092.8   %us for  9.7 Hz 
%tlname='9_7Hzb.tlan'
%repeats=5;

%rep= 99009.9   %us for 10.1 Hz 
%tlname='10_1Hzb.tlan'
%repeats=5;

%rep=85470.0   %us for  11.7 Hz 
%tlname='11_7Hzb.tlan'
%repeats=6;

%rep=54348.0   %us for  18.4 Hz 
%tlname='18_4Hzb.tlan'
%repeats=8;

%rep=50505.0   %us for  19.8 Hz 
%tlname='19_8Hzb.tlan'
%repeats=8;

rep=43860.0   %us for  22.8 Hz 
tlname='22_8Hzb.tlan'
repeats=10;

%rep= 4608.3   %us for 217 Hz 
%tlname='217Hzb.tlan'
%repeats=5;

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

%rep=1930.4   %us for  518 Hz 
%tlname='518Hzb.tlan'
%repeats=5

%rep=1890.0   %us for  529 Hz 
%tlname='529Hzb.tlan'
%repeats=5

%rep=995.0 %us for 1005 Hz 
%tlname='1005Hzb.tlan'
%repeats=10

%rep=982.2 %us for 1018 Hz 
%tlname='1018Hzb.tlan'
%repeats=10

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

%rep=659.2 %us for 1517 Hz 
%tlname='1517Hzb.tlan'
%repeats=11;

%rep=499.2 %us for 2003 Hz 
%tlname='2003Hz.tlan'
%repeats=11;

%rep=550.3 %us for 1817 Hz 
%tlname='1817Hz.tlan'
%repeats=11;

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

%rep=397.2 %us for 2517 Hz 
%tlname='2517Hzb.tlan'
%repeats=12;


%rep=331.4 %us for 3017 Hz 
%tlname='3017Hzb.tlan'
%repeats=17

%rep=284.3 %us for 3517 Hz 
%tlname='3517Hzb.tlan'
%repeats=20;

%rep=248.9 %us for 4017 Hz 
%tlname='4017Hzb.tlan'
%repeats=23

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

%rep=199.3 %us for 5017 Hz 
%tlname='5017Hz.tlan'
%repeats=50;

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

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 3.0\nBLOCKLEN 3 BYTES\n');
fprintf(fid,'PAFPAR_VS 3.0\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+rep/2-shapelen,txn);    %% pulse decay AMP(%d)\n',
  end


  fprintf(fid,'AT      %.1f    %s\n',pstart+npoints*dt+rep/2-shapelen,txn); 
  fprintf(fid,'AT      %.1f  RFOFF*      %% Close SOUSY switch\n', pstart+npoints*dt+rep/2-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);
