!******************************************************************************!
! Illustrates the trade-offs between various standard windows
!
! BARTlett   -27        1.33          12       Bartlett (triangle)
! HANNing    -32        1.50          18       Hanning (cosine bell)
! HAMMing    -43        1.36           6       Hamming (bell on pedestal)
! BLACkman   -58        1.73          18       Blackman
!
! Arguments: None
!
! @author:  Jeanette McCall
!******************************************************************************!
startmacro l:elem[120] l:navg[8] l:tl[20]
  switch CLEAN  clean  GET DEF=FALSE SDEF=TRUE

  if CLEAN then
    res quals "{AUX=RAM}"
  else
    res quals ""
  endif

  call adjustColors

  res none_           "etex_no_window.tmp^quals"
  res hanning         "etex_hanning_window.tmp^quals"
  res hamming         "etex_hamming_window.tmp^quals"
  res blackman        "etex_blackman_window.tmp^quals"
  res fft_none        "etex_fft_no_window.tmp^quals"
  res fft_hanning     "etex_fft_hanning_window.tmp^quals"
  res fft_hamming     "etex_fft_hamming_window.tmp^quals"
  res fft_blackman    "etex_fft_blackman_window.tmp^quals"

  res nelem 125

  call setInstructions

  ! create the windows
  window NAME=none  OUT=none_ SIZE=^nelem
  window NAME=hann  OUT=hanning  SIZE=^nelem
  window NAME=hamm  OUT=hamming  SIZE=^nelem
  window NAME=black OUT=blackman SIZE=^nelem

  ! take an fft of each to show the frequency domain
  fft none_    fft_none nfft=1k    win=none
  fft hanning  fft_hanning nfft=1k  win=none
  fft hamming  fft_hamming nfft=1k  win=none
  fft blackman fft_blackman nfft=1k  win=none

  pipe on

    panel/msgid=MAIN/setup/controls=gc/THEME={^slide}
    gcontrol label macro "DSP ETEX MACRO"
    gcontrol menu  main "Click EXIT to close =" "EXIT" TABLE={ItemColors={EXIT="YELLOW"}} /tleft
    gcontrol text  idtext "Instructions" instructions 24 16 /title  

    plot/id=windows hanning{LAYER={LINE={THICKNESS=3}}}|hamming{LAYER={LINE={THICKNESS=3}}}|blackman{LAYER={LINE={THICKNESS=3}}}|none_{LAYER={LINE={THICKNESS=5}}} /LEGEND

    plot/id=fft_all/readout="+OnAxis|-scientific" fft_hanning{LAYER={LINE={THICKNESS=3}}}|fft_blackman{LAYER={LINE={THICKNESS=3}}}|fft_hamming{LAYER={LINE={THICKNESS=3}}}|fft_none{LAYER={LINE={THICKNESS=3}}} CM=20LOG

  pipe off

  call eraseFiles
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    pipe stop
  endif

!******************************************************************************!
! sets up color scheme based on current theme
procedure adjustColors

  if env.theme EQSS GEAR THEN
    res slide "CBG=midnightblue,CGF=GREEN,CWTS=midnightblue,CWFH=GREEN,CWFH=YELLOW,CWFG=white,CWTITLE=RED,CWBS=midnightblue,CBGX=green,CWMS=midnightblue,CWBG=BLUE}"
    res thick 4
    res c_green green
    res c_blue  blue
    res c_yel_or yellow
    res c_pnk_mar pink
  else
    res slide "CBG=midnightblue,CGF=GREEN,CWTS=midnightblue,CWFH=GREEN,CWFH=YELLOW,CWFG=white,CWTITLE=RED,CWBS=midnightblue,CBGX=green,CWMS=midnightblue,CWBG=BLUE}"
    res thick 4
    res c_green olive
    res c_blue  navy
    res c_yel_or darkorange
    res c_pnk_mar maroon
  endif

return
!******************************************************
procedure setInstructions

  res instructions  &
  "A few of NeXtMidas's DSP WINDOW options\n\n&
  Code     Sidelobe   Equivalent     Rolloff\n         &
           Max (dB)   Bandwidth    (dB/Octave)\n&
  NONE       -13        1.00           6\n&
  HANNing    -32        1.50          18\n&
  HAMMing    -43        1.36           6\n&
  BLACkman   -58        1.73          18\n\n&
  Windows are used for converting an infinite signal to &
  a finite signal. They reduce discontinuities in the time domain, &
  shaping the side lobes in the frequency domain. In the time domain, &
  the signal tapers down to 0 at each end."
return
!******************************************
procedure eraseFiles
  erase none_ hanning hamming blackman
  erase fft_none fft_hanning fft_hamming fft_blackman
return