!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! This algorithm is used to calibrate tuner timecode by 
! reconstructing the original input from the tuner output
! and using cross spectral techniques to calculate delays.
! These are compared to the pic_tc() results to verify
! the theoretical equations.
!
! Authors: Sid Moore, Joe Briggs, and Jeff Schoen
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
startmacro/quiet u:device[pic1] n:dec[64] a:format[SI] &
		a:infile[tcwhite] a:aflags[] d:rate[10e6]

if /rtinit gt 0
  pic clean
  pic create tctest SI 20e6 10e6
  picd reset ^device ii
  picd/port=module1/tc=sdn0/verbose=2 acquire ^device tctest
  status tctest
!  digtc/bit=0/novft/noupd/quiet=0/sdn tctest
  stop
endif
if /init gt 0
  pic clean
  pic copy infile tctest
  stop
endif
if /live gt 0 then
  pic create tctest SI 2*rate rate
endif
if /file gt 0
  res wbfile archive_wb
  res nbfile archive_nb
else
  res wbfile tctest
  res nbfile tctunfile
endif

timer set
calc ilen wbfile(hcb.sz)
calc ifreq 1/wbfile(hcb.xd)
calc indelta wbfile(hcb.xd)
if /file gt 0
  calc tune 1e6
else
  calc tune ifreq/8
endif
calc nfft 64k

if format eqs "CI"
 calc olen ilen/2 dec / 2 / power2 2k max
 calc ofreq ifreq dec / 2 /
 calc odec dec 2 *
 calc outdelta indelta odec *
 calc orate 1.0 outdelta /
 calc upshift tune
 calc obw orate 0.5 *
 calc obw orate 0.6 *
elseif format eqs "SI"
 calc olen ilen/2 dec / power2 4k max
 calc ofreq ifreq dec /
 calc odec dec
 calc outdelta indelta odec *
 calc orate 1.0 outdelta /
 calc upshift tune orate 4 / - ! Adjust for output center freq
 calc obw orate 0.25 *
 calc obw orate 0.30 *
else
 error "Illegal format type"
endif

calc ssrate ifreq
if aflags eqs "POVSR"
  calc dec dec /ovsr *
endif
if /chn gt 0
  res l:chn /chn
else
  res l:chn 1
endif
timex 2004:01:01::0 toff

say "Test TC on Tuner=^chn Dec=^dec Fmt=^format Flags=^aflags"

if /live gt 0 then

  picd/flags=^aflags reset ^device 
  pic create nbfile ^format olen ofreq
  pic create nbjunk ^format olen ofreq
  if /ovsr ge 1 picd/port=module1 set ^device "OVSR" /ovsr
  pipe on 
    picd/port=module1/tc=sdn0/tcoff=toff/pause=0.2/flags=^aflags &
	acquire ^device wbfile ,,, 1 tune
    picd/port=tuner^chn/tc=sdn0/tcoff=toff/pause=0.0/flags=^aflags &
	acquire ^device nbfile ,,, dec tune
  pipe off

else

  picd reset ^device t1
  pic create nbfile ^format olen ofreq
  if /ovsr ge 1 picd/port=module1 set ^device "OVSR" /ovsr
  pipe on 
    picd/flags=muxclk=i/port=module2/pause=0.2/replay=-2/srate=ssrate &
  	play ^device wbfile
    picd/port=tuner^chn/tc=sdn0/tcoff=toff/pause=0.3/flags=^aflags &
	acquire ^device nbfile ,,, dec tune
  pipe off

endif

timex file=^nbfile time2s|time2f
timex file=^wbfile time1s|time1f
calc toffset time2s-time1s time2f-time1f +
calc ioffset toffset wbfile(hcb.xd) /
calc outabsc toffset indelta / round indelta *
calc intersample toffset outabsc -

say "TC diff = ^toffset sec  or ^ioffset samples + ^intersample"

 header nbfile xs=outabsc sz=olen
pipe on
 dpco/nowarn ,,,,, olen,outdelta,outabsc,upshift,,,_mixer
 mult nbfile _mixer _tunshift
 upzero/ps=1m _tunshift odec _tunup
 spectra _tunup specu nfft bh92 0 1 -1 wbfile speci,tccross
pipe off
 thin tccross tccross1 tune-obw tune+obw 0 ab
 thin tccross tccross2 tune-(obw/2) tune+(obw/2) 0 ab
 polar tccross2 ,, tphase yes
 polyfit tphase 1 slope ,, error
 statis error ,, stdev
 calc stdev stdev 360 /
 calc delay slope01 360 /  intersample +
 calc delayns delay 1e9 * round 
 calc delaysamp delay indelta /
 say "*** Delay = ^delayns ns  or ^delaysamp Samples STD = ^stdev"
 timer elapse
if /x gt 0
 xplot/all specu|speci|tccross ,,, ablo
 xplot/all tccross1|tccross2 ,,, abph 
endif

endmacro
