! Test cases for DPCO.
!
! @author  J. Czechowski IV
! @version $Id: test_dpco.mm,v 1.4 2014/05/12 17:42:19 ntn Exp $
startmacro
  set  fnprefix "dpco_^{env.pid}"

  call runXMidasTests
endmacro

! This procedure is a direct port of the code in $XMDISK/xm/test/mcr/test_dpco.txt,
! except for the changes required to run in NeXtMidas and to use the ASSERT command
! rather than manual error-counting (as is done in X-Midas).
! NTN: modded to use fnprefix for temporary output files
procedure runXMidasTests
  debug/save=saveDebug
  debug off deprecate  ! <-- We are using deprecated keys in STATISTICS

  ! This first test should generate a constant sinewave, and it should
  ! generate exactly the requested number of samples.
  ! DR 666167 - describes a problem where the number of samples was
  ! being truncated.
  res tstsize 22978
  res tstfreq 2000.0
  res fstart  32.046250
  res fdelta 1/8000
  dpco ^{fnprefix}_sine ,,,, tstsize fdelta fstart tstfreq ,,, ^{fnprefix}_cxout

  ! verify that the file is the correct size
  assert/text="Sinewave file has size ^tstsize" tstsize EQ FILE(^{fnprefix}_sine).size

  ! verify this sinewave by comparing it to a straight line of values
  fft/MAG ^{fnprefix}_sine ^{fnprefix}_usine 4k
  peakpick ^{fnprefix}_usine ^{fnprefix}_pksloc
  waveform ^{fnprefix}_pksline SF FILE(^{fnprefix}_pksloc).size CONST tstfreq ,, FILE(^{fnprefix}_pksloc).xstart FILE(^{fnprefix}_pksloc).xdelta

  assert/text="Generated freq is 2400.0"/TOL=1e-4 ^{fnprefix}_pksloc FEQ/D/T ^{fnprefix}_pksline

  ! Basic idea of this test is that it ramps up the frequency, uses UBIQ and
  ! PEAKPICK to verify that the frequency *is* changing over time, fits a line
  ! to the result.

  ! Control the frequency smoothly
  res fstart 12.34e3
  res fdelta 34.567e3
  waveform ^{fnprefix}_r0 sd 1e7 RAMP INF 0 0 0 1
  fcalc/type=d ^{fnprefix}_r1 ^{fnprefix}_r0 fdelta * fstart +

  ! Use an odd transfer length to create more buffer boundary conditions
  dpco/tl=345 ^{fnprefix}_sine ,,,, 1e7 1e-6 ,, ^{fnprefix}_r1 ,,,, ab
  fft/MAG ^{fnprefix}_sine ^{fnprefix}_usine 16k
  peakpick ^{fnprefix}_usine ^{fnprefix}_pksloc
  ! Should be a nice clean line.  Fit a line to it and test the residual.
  polyfit ^{fnprefix}_pksloc,1,p_coef,^{fnprefix}_xxx,^{fnprefix}_pksline

  fcalc ^{fnprefix}_diff ^{fnprefix}_pksloc ^{fnprefix}_pksline -
  statistics ^{fnprefix}_diff stdv=p_stdev maxv=p_maxval

  assert/text="Line fit residual is less than ^{p_stdev}*2" p_maxval lt p_stdev*2

  ! Now verify that we've recovered our original ramp values
! The following tests will fail right now... this is because of the differences
! in the FFT -> PEAKPICK -> POLYFIT section of the code above. In X-Midas this
! is UBIQ -> PEAKPICK -> POLYFIT and doesn't have the issues with a buggy PEAKPICK
! that chokes on complex data, etc. A manual comparison of the 'sine' file output
! from DPCO shows it to be a match to the X-Midas file.
  assert/text="Frequency start recovered"/TOL=1e-4 p_coef(0) eqt fstart /NOOP="SKIP"
  assert/text="Frequency delta recovered"/TOL=1e-4 p_coef(1) eqt fdelta /NOOP="SKIP"

  erase/quiet ^{fnprefix}_r0 ^{fnprefix}_r1 ^{fnprefix}_sine ^{fnprefix}_usine ^{fnprefix}_pksloc
  erase/quiet ^{fnprefix}_pksline ^{fnprefix}_diff ^{fnprefix}_cxout ^{fnprefix}_xxx
  debug/load=saveDebug
return
