#! /bin/bash
# Last edited on 2013-11-15 22:17:09 by stolfilocal

# Plots all raw runs extracted from the 128-electrode datasets.
# Also computes and plots their power spectra.

# Assumes that the data for each run is in file "raw-runs/s{SSS}_r{BBB}{RR}.txt"
# where {SSS} is the subject ID, {BBB} is the block ID (001-008), and 
# {RR} is the the run ID in the block (01--32).  The spectra for each file 
# "{FNAME}.txt" will be written to "{FNAME}_pwr.txt", and the plots will be
# "{FNAME}.png" and "{FNAME}_pwr.png".

printf '# Last edited on %s by %s\n' `yyyy-mm-dd-hhmmss` "${0##*/}" 1>&2
    
chnames=( `cat channel_names.txt | gawk '/^[ ]*[0-9]/{ print $3; }'` )
printf "assuming %d channels = %s\n" "${#chnames[@]}" "${chnames[*]}" 1>&2

mkdir -p raw-runs

( cd ~/programs/c/NEUROMAT/nmeeg_plot_channels && make uninstall build install )
( cd ~/programs/c/NEUROMAT/nmeeg_spectrum      && make uninstall build install )
( cd ~/programs/c/NEUROMAT/nmeeg_plot_spectra  && make uninstall build install )

function plotruns(){

  subject="$1"; shift;  # Subject's ID number.

  ic_ini=0     # First channel for time plots.
  ic_fin=129   # Last channel for time plots.
  vmax=260     # Max voltage for vertical scaling of time plots.

  ie_ini=0      # First electrode for power spectra.
  ie_fin=127    # Last electrode for power spectra.
  pwr_fmax=250  # Max frequency to plot in Hz.
  pwr_vsize=500 # Vertical plot size in pixels for power spectra.

  for rfile in raw-runs/s${subject}_r?????.txt ; do
    fname="${rfile%%.txt}"
    plot_run.sh          NOSHOW ${fname} ${ic_ini} ${ic_fin} ${vmax}
    plot_run_spectrum.sh NOSHOW ${fname} ${ie_ini} ${ie_fin} ${pwr_fmax} ${pwr_vsize}
  done
}

rm -f raw-runs/s???_r?????.png
rm -f raw-runs/s???_r?????_pwr.txt
rm -f raw-runs/s???_r?????_pwr.png

plotruns 013
plotruns 014
