!******************************************************************************!
! Test for Jeff's new FFT/CMAG support for Cross Spectral Density
! Starter automatic testing for FFT <cross> and CMAG options via comparison with FCALC results
! Waveforms used are similiar to those in X-Midas UBIQUITOUS testing
! Interactive has visual portion of test
!
! @bug      NXM-3899  FFT needs magnitude, 20log, and dB log on cross spectrum output
! @since    NeXtMidas 4.1.4
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1

  call setDatDir

  res prefix "test_fft_cmag_"
  call createFilesAutoTest

  ! Only allow interAct if PANEL command Exists - may be running in LIBNXM mode
  invoke panelExists reg.shell.M.dictionary.isDictionaryEntry(PANEL)

  if (interAct or coreGraphics) and panelExists then
    set instructions "Tests Jeff's /CMAG support for &
                      FFT in conjunction with /DBF, /MAG, and/or /LOG\n\n&
                      SINE   - 2Hz\n&
                      COSINE - 5Hz\n&
                      CROSS  - 2Hz and 5Hz\n\n&
                      Check if automated tests passed"

    pipe init

      panel/setup/controls=gc
      gcontrol label macro "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text   idtext "Instructions" "^{instructions}" 24 14 /title

      plot/all/id=plainfft fm_plain_fft
      plot/all/id=dbffft fm_dbf_fft
      plot/all/id=magfft fm_mag_fft
      plot/all/id=logfft fm_log_fft

      plot/all/id=plainsine sine_plain_fft   X1=-10 x2=10
      plot/all/id=dbfsine sine_dbf_fft       X1=-10 x2=10
      plot/all/id=magsine sine_mag_fft       X1=-10 x2=10
      plot/all/id=logsine sine_log_fft       X1=-10 x2=10

      plot/all/id=plaincos cos_plain_fft   X1=-10 x2=10
      plot/all/id=dbfcos cos_dbf_fft       X1=-10 x2=10
      plot/all/id=magcos cos_mag_fft       X1=-10 x2=10
      plot/all/id=logcos cos_log_fft       X1=-10 x2=10


      plot/all/id=plaincross cross_plain_fft   X1=-10 x2=10
      plot/all/id=dbfcross cross_dbf_fft       X1=-10 x2=10
      plot/all/id=magcross cross_mag_fft       X1=-10 x2=10
      plot/all/id=logcross cross_log_fft       X1=-10 x2=10

    pipe off
  endif

  files/e ^{prefix}*
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "UNZOOM"
  elseif msg.name eqs "ZOOM"
  else
    say "Unhandled message name=^msg.name info=^msg.info fid=^msg.fid data=^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! create the files and do the auto testing
!******************************************************************************!
procedure createFilesAutoTest
  if ^{env.NMROOT} EQS "classpath:" then
    res fm_file "fm_capture_t1000_ram.tmp{AUX=RAM}"
    file copy "^{datDir}.fm_capture_t1000.prm" fm_file
    file unprotect fm_file
  else
    res fm_file "^{datDir}.fm_capture_t1000.prm"
  endif

  res fm_plain_fft "^{prefix}plain_fm.tmp"
  res fm_dbf_fft "^{prefix}dbf_fm.tmp"
  res fm_mag_fft "^{prefix}mag_fm.tmp"
  res fm_log_fft "^{prefix}log_fm.tmp"

  res sine_plain_fft "^{prefix}plain_sine.tmp"
  res sine_dbf_fft "^{prefix}dbf_sine.tmp"
  res sine_mag_fft "^{prefix}cmag_mag_sine.tmp"
  res sine_log_fft "^{prefix}cmag_log_sine.tmp"

  res cos_plain_fft "^{prefix}plain_cos.tmp"
  res cos_dbf_fft "^{prefix}dbf_cos.tmp"
  res cos_mag_fft "^{prefix}mag_cos.tmp"
  res cos_log_fft "^{prefix}log_cos.tmp"

  res cross_plain_fft "^{prefix}plain_cross.tmp"
  res cross_dbf_fft "^{prefix}dbf_cross.tmp"
  res cross_mag_fft "^{prefix}mag_cross.tmp"
  res cross_log_fft "^{prefix}log_cross.tmp"

  res exp_cross         "^{prefix}cross_exp.tmp"
  res exp_cross_dbf_fft "^{prefix}dbf_cross_exp.tmp"
  res exp_cross_mag_fft "^{prefix}mag_cross_exp.tmp"
  res exp_cross_log_fft "^{prefix}log_cross_exp.tmp"

  res sine_wave "^{prefix}sine_waveform.tmp"
  res cos_wave "^{prefix}cos_waveform.tmp"


  fft in1=fm_file out1=fm_plain_fft
  fft/dbf in1=fm_file out1=fm_dbf_fft
  fft/mag in1=fm_file out1=fm_mag_fft
  fft/log in1=fm_file out1=fm_log_fft

  waveform out=sine_wave shape=sine form=cf freq=2 delta=1/100
  waveform out=cos_wave shape=cos form=cf freq=5 delta=1/100

  ! Get the FFT results
  fft          in1=sine_wave in2=cos_wave out1=sine_plain_fft &
                out2=cos_plain_fft cross=cross_plain_fft
  fft/dbf/cmag in1=sine_wave in2=cos_wave out1=sine_dbf_fft &
                out2=cos_dbf_fft cross=cross_dbf_fft
  fft/mag/cmag in1=sine_wave in2=cos_wave out1=sine_mag_fft &
                out2=cos_mag_fft cross=cross_mag_fft
  fft/log/cmag in1=sine_wave in2=cos_wave out1=sine_log_fft &
                out2=cos_log_fft cross=cross_log_fft

  ! Get the expected /<operation>/CMAG results using FCALC
  fcalc ^exp_cross_dbf_fft ^cross_plain_fft DB
  fcalc ^exp_cross_mag_fft ^cross_plain_fft MAG2
  fcalc ^exp_cross_log_fft ^cross_plain_fft LOG/B 10 *   ! FFT/LOG is 20LOG (i.e. 10 * 10LOG)
  !list2/bg cross_dbf_fft
  !list2/bg exp_cross_dbf_fft
  !list2/bg cross_mag_fft
  !list2/bg exp_cross_mag_fft
  !list2/bg cross_log_fft
  !list2/bg exp_cross_log_fft

  ! Get the expected <cross> using FCALC
  fcalc exp_cross sine_plain_fft cos_plain_fft ~*
  !list2/bg exp_cross
  !list2/bg cross_plain_fft

  ! Test if FCALC operation on plain cross output gives similiar
  ! results to doing the extra operation in FFT using /CMAG
  res z:dbfInTol false
  if/TOL=1 cross_dbf_fft FEQ/D/T  exp_cross_dbf_fft then
    res z:dbfInTol true
  endif
  assert/text="^reg.main.name: FFT/DBF/CMAG data within 1%" dbfInTol ISTRUE
  assert/text="^reg.main.name: FFT/MAG/CMAG data EQ" cross_mag_fft FEQ/D/T exp_cross_mag_fft
  assert/text="^reg.main.name: FFT/LOG/CMAG data EQ" cross_log_fft FEQ/D/T exp_cross_log_fft
  assert/text="^reg.main.name: FFT Cross Spectral Density data EQ" cross_plain_fft FEQ/D/T exp_cross

return