! Test macro for FIRWIN.
!
! @author  Jason Broussard / J. Czechowski IV
! @version $Id: test_firwind.mm,v 1.10 2013/01/25 21:22:59 jxc Exp $
! @since   NeXtMidas 2.1.1
!
startmacro
  ! ==== GENERATE FILTERS IN NEXTMIDAS =========================================
  firwin ram:test_filt1  BANDPASS NONE    32  2.55e6  3.55e6   5e6
  firwin ram:test_filt2  BANDPASS BARTL  512     2e6   4.1e6  10e6
  firwin ram:test_filt3  LOW      HANNI  512     2e6  ,,      10e6
  firwin ram:test_filt4  LOW      HAMMI 1024     333  ,,     2.5e6
  firwin ram:test_filt5  HIGH     BLACK 4096 21.93e6  ,,    28.8e6
  firwin ram:test_filt6  HIGH     BH61    64   100e6  ,,     999e6
  firwin ram:test_filt7  LOW      BH67   512      89  ,,       100
  firwin ram:test_filt8  BANDPASS BH74   512     2e6   9.1e6  10e6
  firwin ram:test_filt9  HIGH     BH92     8   777e6  ,,     900e6
  firwin ram:test_filt10 HIGH     NONE    64   888e6  ,,     999e6
  firwin ram:test_filt11 LOW      NONE    32      22  ,,       100

  ! ==== COMPARE FILTERS TO PRIOR X-MIDAS RESULTS ==============================
  res D:tolr 2e-6

  file open/d tag nxm.dsp.dat.firwinspecs.tmp
    do cnt 1 tag.size
      invoke exp tag.getDataTable(^cnt-1)
      res    act {}
      statistics/onebase ram:test_filt^{cnt} ,act.MEAN,act.STDV,act.ELEM,,act.MAXV,act.MINV,,,act.MAXO,act.MINO

      foreach col inList "MEAN,STDV,MAXV,MINV,ELEM"
        calc delta act.^{col} exp.^{col} - abs
        assert/text="Verifying ^{col} of output file for test ^cnt" delta le tolr
      endfor
    enddo
  file close tag

  ! ==== CHECK COMPLEX OUTPUT (NOT IN X-MIDAS) =================================
  foreach type inList "LowPass,HighPass,BandPass,StopBand"
    firwin       ram:test_filt_real  ^{type} BARTL 1024     2e6   4.1e6  10e6
    firwin/cmplx ram:test_filt_cmplx ^{type} BARTL 1024     2e6   4.1e6  10e6

    fft/psd ram:test_filt_real  ram:test_psd_real
    fft/psd ram:test_filt_cmplx ram:test_psd_cmplx

    if (/PLOT) then
      plot/wtitle="^{type} Time Domain" ram:test_filt_real|ram:test_filt_cmplx
      plot/wtitle="^{type} Freq Domain" ram:test_psd_cmplx|ram:test_psd_real
    endif

    ! Make sure the PSD for both real and complex filters are the same. Note that
    ! with FCALC we are looking ONLY at the common area (i.e. x >= 0.0).
    headermod ram:test_psd_real  type=1000
    headermod ram:test_psd_cmplx type=1000

    fcalc ram:diff ram:test_psd_real ram:test_psd_cmplx - ABS
    statistics ram:diff MAX=maxDiff
    assert/text="Complex ^{type} filter has same PSD as real" (maxDiff LT 1e-10)


    ! Make sure the REAL component of the complex filter is within 0.01% of the
    ! real filter.
    if (type EQS "BandPass") OR (type EQS "StopBand") then
      ! Currently off by a factor of ~0.5, plus a spike at 0 in StopBand
      assert/text="Complex ^{type} filter has same REAL values" /noop="MISS"
    else
      fcalc ram:test_real_tol ram:test_filt_cmplx REAL ram:test_filt_real - ABS ram:test_filt_real 0.0001 * ABS LE
      statistics ram:test_real_tol MIN=minVal MAX=maxVal
      assert/text="Complex ^{type} filter has same REAL values" (minVal EQ 1.0) AND (maxVal EQ 1.0)
    endif
  endfor

  ! ==== TEST /SYM SWITCH (NOT IN X-MIDAS) =====================================
  assert/text="Tests for /SYM"/noop=MISS
endmacro
