!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Run this with a sine wave at 1.1e6
!
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
startmacro/quiet u:device[pic1ioa] &
  n:dec1[3] n:rate[6e6] n:freq[1e6] n:nfft[1k] n:dec2[20]

local dur arate trate item ialg nn afreq 

calc dur 1  ! use 1 second buffers
calc arate rate/dec1/2
calc trate arate/dec2/2
calc srate 40e6 40e6 arate*1.1 / fix /
calc afreq 1000

pic create ramfile sb dur*rate rate
pic create tunfile ci dur*arate arate
pic create algfile ci dur*arate arate
pic create algdfile si 1*dur*arate arate
pic create tundfile ci dur*trate trate

picd reset ^device 

xpipe/setup/controls/nw=128 on 

sourcepic/id=11/wb=1/tl=1/master=2002/port=module1/host=n &
	ramfile(fs=nfft) _ram(ps=128k) ^device, 16*dec1

sourcepic/id=21/wb=2/tl=1/masterx=3002/port=tuner1/dmac=nn/skip=32/cpc=1 &
	tunfile(fs=nfft) _tun(ps=128k) ^device, dec1 freq

sourcepic/id=31/wb=3/tl=1/masterx=4002/port=external1/alg=fm/feed=nn &
	algfile(fs=nfft) _alg(ps=256k) ^device, 1 freq

! the demod signal is in the imag part of the complex word
demux/tl=1k _alg(fs=0) ,, _algd(ps=128k)

ubiq _ram(fs=0) _ramf nfft hann 0
xrtraster/xs=2/dbrange=100/xn="Raw Spectra" _ramf ,,,d1

ubiq _tun(fs=0) _tunf nfft hann 0
xrtplot/xs=3/dbrange=100/xn="Tuned Spectra" _tunf ,,,d1

xrtplot/xs=4/xn="Demod Time Output" _algd(fs=1k) -32k 32k 

sinkpic/id=41/wb=4/throttle=2/srate=srate/port=module2/flags=intclk &
	_algd algdfile ^device

sourcepic/id=51/wb=5/tl=1/port=tuner2 &
	tundfile(fs=nfft) _tund(ps=128k) ^device, dec2 afreq

ubiq _tund(fs=0) _tundf nfft hann 0
xrtplot/xs=5/dbrange=100/xn="Tuned Demoded and reTuned Spectra" _tundf ,,,d1

xpipe off
endmacro

startcontrols
label initialentry
xcontrol/lab=m1 menu item ,, "TEST;Once,Cont,Stop,Exit"
xcontrol/lab=m2 menu ialg 3  "ALG;None,AM,FM,PM" 
xcontrol/lab=f1/name="AFreq: " dval freq freq -rate/2 rate/2 1
xcontrol/lab=f2/name="BFreq: " dval afreq afreq -arate/2 arate/2 1
return

label m1
if item eq 1 then
  res w_1001 1
elseif item eq 2 then
  res w_1001 2
elseif item eq 3 then
  res w_1001 4
else
  stop
endif
return

label m2
if ialg eq 1 then
  res w_3010 1
elseif ialg eq 2 then
  res w_3010 5
elseif ialg eq 3 then
  res w_3010 6
elseif ialg eq 4 then
  res w_3010 7
endif
return

label f1
  res w_2005 freq
return

label f2
  res w_5005 afreq
return


endcontrols
