!
! This is a test macro for the NEWRATE primitive
!
!  DEPENDENCIES:
!      WAVEFORM, FFT, STATUS, COMPARE, STATISTICS
!
! @author  John Marsh / J. Czechowski IV
! @since   NeXtMidas 2.5.0
STARTMACRO
  ! ==== BASIC TESTS ===========================================================
  ! Basic tests check out upsampling and downsampling on files of all
  ! supported types.  Sine wave amplitude is chosen to cover the numeric
  ! range of the data type, and threshold is based on previous results
  ! using X-Midas newrate
  call basic_test "sb" 100    1
  call basic_test "si" 30000  234
  call basic_test "sl" 1m     8177
  call basic_test "sf" 1m     8177
  call basic_test "sd" 1m     8177
  call basic_test "cb" 100    1
  call basic_test "ci" 30000  234
  call basic_test "cl" 1m     8192
  call basic_test "cf" 1m     8192
  call basic_test "cd" 1m     8192

  call advanced_test

  call sync_test
  
  ! ==== TEST <FILTER> =========================================================
  waveform/seed=12345 ram:noise si 100 white 30000
  newrate ram:noise ram:o1 .777 filter=nxm.dsp.dat.newrate_infilter_test.prm
  assert/text="NEWRATE filter test" ram:o1 FEQ nxm.dsp.dat.newrate_output_test1.prm

  ! ==== TEST /OFFSET= =========================================================
  ! /OFFSET test involves shifting the phase offset one way, and then
  ! back again, and checking that we end up with basically the same data
  waveform ram:sf1m sf 1m sin 1m
  newrate ram:sf1m ram:o1 1 /offset=+.777
  newrate ram:o1   ram:o2 1 /offset=-.777

  status ram:o1 xs=abs
  compare ram:sf1m(^{abs}:) ram:o1 max min
  assert/text="NEWRATE offset test (O1)" ^abs EQ 13.777 AND ^max GT 13000 AND ^min LT -13000

  status ram:o2 xs=abs
  compare ram:sf1m(^{abs}:) ram:o2 max min
  assert/text="NEWRATE offset test (O2)" ^abs EQ 27 AND ^max LT 4400 AND ^min GT -4400
  
  ! ==== TEST /TUNE= ===========================================================
  ! /TUNE test involves tuning the signal one way, and then
  ! back again, and checking the frequencies of the results
  waveform ram:cf4k cf 4k sin 1m phase=0.25
  newrate ram:cf4k ram:o1 1 /tune=+.05
  newrate ram:o1   ram:o2 1 /tune=-.05
  fft ram:cf4k ram:fft0 4k /mag /partial
  fft ram:o1   ram:fft1 4k /mag /partial
  fft ram:o2   ram:fft2 4k /mag /partial
  statistics ram:fft0 omax=omax0
  statistics ram:fft1 omax=omax1
  statistics ram:fft2 omax=omax2
  assert/text="NEWRATE tune test" ^omax1 EQ 2293 AND ^omax0 EQ 2088 AND ^omax2 EQ 2088
  
  ! ==== TEST NEWRATE WITH PIPES ===============================================
  waveform ram:cf1m cf 211k sin 1m phase=0.25                    !! without pipe
  newrate ram:cf1m ram:o1 0.7777 /offset=0.11 /tune=0.3
  
  PIPE ON                                                        !! with pipe
    waveform _cf1m cf 211k sin 1m phase=0.25
    newrate _cf1m _out 0.7777 /offset=0.11 /tune=0.3
    noop _out ram:o2
  PIPE OFF

  assert/text="NEWRATE pipe test" ram:o1 FEQ ram:o2
  erase ram:sf1m ram:o1 ram:o2 ram:cf4k ram:fft0 ram:fft1 ram:fft2 ram:cf1m

  %test_newrate_instep_gt_ntaps ! NXM-3613
ENDMACRO

!-------------------------------------------------------------------------------
! Create a sine wave of frequency 1Hz, upsample to 1.625 Hz, and then
! downsample back to 1 Hz.  Compare the results to the original (allowing
! for difference in abscissa)
!
PROCEDURE basic_test s:type d:amp d:thresh
  waveform ram:^{type}1m ^type 333k sin ^amp phase=0.25        !! Bigger than I/O buffers
  newrate ram:^{type}1m ram:^{type}o1 1.625
  newrate ram:^{type}o1 ram:^{type}o2 1.0

  status  ram:^{type}o2 xs=abs
  compare ram:^{type}1m(^{abs}:) ram:^{type}o2 max min
  assert/text="NEWRATE basic test (^{type})" ^max LE ^thresh AND ^min GE -^thresh
  erase ram:^{type}1m ram:^{type}o1 ram:^{type}o2
RETURN

!-------------------------------------------------------------------------------
! Additional tests that actually use some more realistic signal properties
! and not just 1 Hz signals.
procedure advanced_test
  waveform ram:wave CF FREQ=100e3 BW=500e3 DURATION=10
  newrate  ram:wave ram:resamp 450e3

  fft/psd/1D ram:wave   ram:wave_spec   OVER=0.5 NAVG=16 NFFT=32k
  fft/psd/1D ram:resamp ram:resamp_spec OVER=0.5 NAVG=16 NFFT=32k

  statistics ram:wave_spec   AMAX=IN_FREQ
  statistics ram:resamp_spec AMAX=OUT_FREQ

  assert/text="NEWRATE Input  FREQ matches expected" 100e3 EQ/T IN_FREQ  /TOL=1e-3
  assert/text="NEWRATE Output FREQ matches expected" 100e3 EQ/T OUT_FREQ /TOL=1e-3
  
  if (/PLOT) then
    fft/psd ram:wave   ram:wave_rast   OVER=0.5 NAVG=16
    fft/psd ram:resamp ram:resamp_rast OVER=0.5 NAVG=16
    plot/bg/id="IN_FILE"  ram:wave_rast    X1=-275e3 X2=275e3
    plot/bg/id="OUT_FILE" ram:resamp_rast  X1=-275e3 X2=275e3
    erase ram:wave_rast ram:resamp_rast
  endif
  erase ram:wave ram:resamp ram:wave_spec ram:resamp_spec
return

!-----------------------------------------------------------------------------
!  Test using sync input file
!  Basic test to ensure functionality
!  This feature has undergone limited testing
procedure sync_test
  WAVEFORM,OUT=ram:wave,FORM=CF,ELEM=1089536,SHAPE=SIN,AMP=1,FREQ=1000,PHASE=0,START=0,DELTA=3.04e-5
  fft/psd/1D ram:wave   ram:wave_spec   OVER=0.5 NAVG=16 NFFT=32k
  statistics ram:wave_spec   AMAX=IN_FREQ

  newrate ram:wave ram:resamp 32894.73526060603 nxm.dsp.test.sample_sync_file.tmp
  call check_sync_test ram:resamp IN_FREQ 1

  newrate ram:wave ram:resamp 32894.73526060603 nxm.dsp.test.sample_sync_file_mod.tmp
  call check_sync_test ram:resamp IN_FREQ 2

  newrate/XMTO ram:wave ram:resamp 32894.73526060603 nxm.dsp.test.sample_sync_file.tmp
  call check_sync_test ram:resamp IN_FREQ 3

  newrate/XMTO ram:wave ram:resamp 32894.73526060603 nxm.dsp.test.sample_sync_file_mod.tmp
  call check_sync_test ram:resamp IN_FREQ 4

  erase ram:wave ram:wave_spec ram:resamp
return

procedure check_sync_test sync_file d:IN_FREQ i:count
  
  fft/psd/1D sync_file     ram:resamp_spec OVER=0.5 NAVG=16 NFFT=32k
  statistics ram:resamp_spec AMAX=OUT_FREQ

  assert/text="NEWRATE (sync ^count) Input  FREQ matches expected" 1e3 EQ/T IN_FREQ  /TOL=1e-3
  assert/text="NEWRATE (sync ^count) Output FREQ matches expected" 1e3 EQ/T OUT_FREQ /TOL=1e-3

  erase ram:resamp_spec
return;
