/* generate BIP shape
   Evgeny Tishencko & Peter Sandor, Agilent 2010-14   */

if ($# < 3)  then
           
    write('alpha',' usage:  makeBIP(shapeName,referencePower,referencePulseWidth)' )
            return 
	  endif   
 
"make BIP pulse, name, reference power, reference pulsewidth"


//input parameters
$shCBIP=$1
$refClvl=$2
$refCpw=$3

//output file
 
 $bipfile=userdir+'/shapelib/'+$shCBIP+'.RF'
 
 
  write('alpha','generating Shaka\'s  BIP 125-15-900 pulse shape, JMR v151, pp. 269-283, 2001\n')
  write('alpha','+/-125 percent B1 inversion profile, 15 percent B1 inhomogenity')   
  write('alpha','set pw to 1/(4B1)*900/90=pw90*900/90')
  write('alpha','i.e. if B1=20kHz, then pwC=12.5   and pwBIP=900/90*12.5 =125us')
 


//Shaka's broadband pulse at full power 

 
format(900.0/90.0*$refCpw+0.5,0,0):$pwCBIP
 

$nsteps=0
format(2.0*$pwCBIP+0.5,0,0):$nsteps 

echo('reference power ', $refClvl,'reference hard pulse width',$refCpw) 
echo('bip shape name  is ',$shCBIP)
echo('bip pulsewidth  is ',$pwCBIP)
echo('set bip power to',$refClvl)
echo('number of bip shape steps  is ',$nsteps)
 
  write('reset',$bipfile)
  write('file',$bipfile,'##   SHAKA\'S BROADBAND INVERSION PULSE')
  write('file',$bipfile,'##   BIP 125-15-900')
  write('file',$bipfile,'##   125 percent B1 inversion profile')
  write('file',$bipfile,'##   15 percent B1 inhomogenity')
  write('file',$bipfile,'##   set pw to 1/(4B1)*900/90=pw90*900/90')
  write('file',$bipfile,'##   i.e. if B1=20kHz, then pwC=12.5   and pwBIP=900/90*12.5 =125us')
  write('file',$bipfile,'##   JMR v151, 269-283, 2001')
  write('file',$bipfile,'##   generated by macro makeBIP_125_15_900')
  write('file',$bipfile,'%s %d', '##       number of steps',$nsteps)
  write('file',$bipfile,'%s %.1f','##       total pulsewdth',$pwCBIP)
  write('file',$bipfile,'%s %d', '##       power level (dB)',$refClvl)
  write('file',$bipfile,'%s %.3f','##       power corresponds to B1, (kHz) ',250.0/$refCpw)
  write('file',$bipfile,'%s %.1f','##       inversion width (kHz) ',2*1.25*250/$refCpw)
  write('file',$bipfile,'##   ')
  
//// ACTUAL BIP PULSE SHAPE GENERATION  
$i=0
repeat
    $i=$i+1 
    $t=-10.0+20.0*$i/($nsteps+1)
    $t=sqrt($t*$t)
    
    if($t<10.0)   then  $a0=-1259072.11  $a1=379331.238   $a2=-38085.2526 $a3=1274.85179 endif 
    
    if($t<9.7996) then  $a0=-1965320.74  $a1=601349.240  $a2=-61334.02    $a3=2085.82748 endif

    if($t<9.5992) then  $a0=-106039.89   $a1=38252.123   $a2=-4545.8981   $a3=178.88367  endif

    if($t<9.2986) then  $a0=46149.93     $a1=-14657.871  $a2=1553.8492    $a3=-54.46298  endif

    if($t<8.9980) then  $a0=12194.50     $a1=-4453.146   $a2=543.7961     $a3=-21.64103  endif

    if($t<7.9960) then  $a0=-1343.97     $a1=601.301     $a2=-85.1971     $a3=4.44969    endif

    if($t<5.9920) then  $a0=-13.89       $a1=18.959      $a2=-1.9599      $a3=0.59524    endif

    if($t<3.9880) then  $a0=8.85         $a1=-13.807     $a2=10.1821      $a3=-0.74779   endif

    if($t<1.9840) then  $a0=0.0          $a1=0.0         $a2=3.0118       $a3=0.49239    endif


    $phase= $a0 + $a1*$t + $a2*$t*$t + $a3*$t*$t*$t    
    $phase= (($phase*10.0) %3600)/10.0
    
    $ppp='' format($phase,1,1):$ppp

    write('file',$bipfile,' %6.1f   1023.0    1.0   1',$ppp)
    
until $i=$nsteps

return($shCBIP,$refClvl,$pwCBIP) 


