!******************************************************************************!
! This tests the functionality of sigstatistics.
!
! Arguments: None

! @author  Linnet Nguyen
! @since   NeXtMidas 3.1.2
!******************************************************************************!
startmacro/SEDITLEGACY=false
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch SCALAR      scalar   GET DEF=FALSE SDEF=TRUE  ! run the scalar case even if not using native
  !timer set

  set additionalRunTime 0
  env get usenative tmpUseNative
  if SEDIT(tmpUseNative,SELECT,"Math") lt 0 then
    calc additionalRunTime additionalRunTime 40 +
  endif
  if SEDIT(tmpUseNative,SELECT,"Cast") lt 0 then
    calc additionalRunTime additionalRunTime 40 +
  endif
  if SEDIT(tmpUseNative,SELECT,"IO") lt 0 then
    calc additionalRunTime additionalRunTime 20 +
  endif
  if additionalRunTime gt 0 then
    say "^reg.main.name: This test may take ^{additionalRunTime}+ seconds to run b/c useNative=^tmpUseNative ..."
  endif

  call setDatDir
  call createExpFmTable

  if scalar then
    call testscalar
  else
    info "^reg.main.name: skipping scalar portion of test: use /SCALAR switch to run scalar section"
  endif
  call testcomplex

  !timer elapsed

  if interact then
    %test_sigstatdemo
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the 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
  else
    !say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  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

!******************************************************************************!
! scalar data
procedure testscalar

  WAVEFORM,OUT=wave1,FORM=SI,ELEM=1.35e8,SHAPE=WHITE,AMP=1,FREQ=2,PHASE=0,START=0,DELTA=4e-8
  SIGSTAT/id=sigstatTest/res=sigstatsTest IN=wave1 OUT=out1 FFT=fft1 NFFT=1K FREQ=1 BW=0

  file open/d tag out1
  res totalRows tag.getNumberOfRows()
  calc lastRow totalRows 1 -
  res tab tag.getDataTable(lastRow)

  !info "Comparing the expected values:"

  invoke tableSUBS nxm.sys.test.TestUtil.tableSubset(tab,sigstatsTest)
  assert/text="^reg.main.name: SIGSTATISTICS Scalar Input: OUTFILE values SUBS of RES" tableSUBS ISTRUE
  ! Cleanup test files
  file close tag
  erase wave1 fft1 out1

return

!******************************************************************************!
! complex data

procedure testcomplex

  mergefile ^{datDir}.fm_capture_t1000.prm fm_capture_t1000_mult.tmp /REPLAY=100
  SIGSTAT/id=ci_sigstatTest/res=ci_sigstatsTest IN=fm_capture_t1000_mult.tmp OUT=outci FFT=fftci NFFT=1K FREQ=2147483584

  file open/d tag outci
  res totalRows tag.getNumberOfRows()
  calc lastRow totalRows 1 -
  res tab tag.getDataTable(lastRow)

  invoke tableSUBS nxm.sys.test.TestUtil.tableSubset(tab,ci_sigstatsTest)
  assert/text="^reg.main.name: SIGSTATISTICS Complex Input: OUTFILE values SUBS of RES" tableSUBS ISTRUE
  invoke tableEqual nxm.sys.test.TestUtil.tablesEqual(fm_sigstat_exp,tab)
  assert/text="^reg.main.name: SIGSTATISTICS of FM Signal matches expected and/or current values" tableEqual ISTRUE

  ! Cleanup test files
  file close tag
  erase fftci outci fm_capture_t1000_mult.tmp
return

*******************************************************************************************
! set up table for current results for fm_capture
! Note: a few values like signal freq are known, but most of this is assuming current values are correct

procedure createExpFmTable
  table fm_sigstat_exp CREATE
  res fm_sigstat_exp.NOISE_FLOOR     3
  res fm_sigstat_exp.NOISE_THRESHOLD  -8
  res fm_sigstat_exp.SIGNAL_STRENGTH  60
  res fm_sigstat_exp.SIGNAL_SNR      57
  res fm_sigstat_exp.PEAK_SIGNAL_FREQ  2147483584
  res fm_sigstat_exp.PEAK_SIGNAL_STRENGTH  63
  res fm_sigstat_exp.PEAK_SIGNAL_SNR  60
  res fm_sigstat_exp.AVG_NOISE_FLOOR  24
  res fm_sigstat_exp.AVG_NOISE_THRESHOLD  13
  res fm_sigstat_exp.AVG_SIGNAL_STRENGTH  55
  res fm_sigstat_exp.AVG_SIGNAL_SNR  31
  res fm_sigstat_exp.AVG_PEAK_SIGNAL_FREQ  2147483556
  res fm_sigstat_exp.AVG_PEAK_SIGNAL_STRENGTH  58
  res fm_sigstat_exp.AVG_PEAK_SIGNAL_SNR  34
  res fm_sigstat_exp.SIGNAL_MEAN     54.690216
  res fm_sigstat_exp.SIGNAL_SDEV     0.0
  res fm_sigstat_exp.SIGNAL_ELEM     1
  res fm_sigstat_exp.SIGNAL_MIN      39.492188
  res fm_sigstat_exp.SIGNAL_MAX      64.05469
  res fm_sigstat_exp.SIGNAL_AMIN     2147483569
  res fm_sigstat_exp.SIGNAL_AMAX     2147483569
  res fm_sigstat_exp.SIGNAL_OMIN     506
  res fm_sigstat_exp.SIGNAL_OMAX     506
  res fm_sigstat_exp.SIGNAL_SUM      56002.78
  res fm_sigstat_exp.SPECTRUM_MEAN   20.718569
  res fm_sigstat_exp.SPECTRUM_SDEV   12.022274
  res fm_sigstat_exp.SPECTRUM_ELEM   1024
  res fm_sigstat_exp.SPECTRUM_MIN    -25.40625
  res fm_sigstat_exp.SPECTRUM_MAX    66.0
  res fm_sigstat_exp.SPECTRUM_AMIN   2147483647
  res fm_sigstat_exp.SPECTRUM_AMAX   2147483584
  res fm_sigstat_exp.SPECTRUM_OMIN   1014
  res fm_sigstat_exp.SPECTRUM_OMAX   507
  res fm_sigstat_exp.SPECTRUM_SUM    2.1724994E7

return
