#!/bin/csh
# process - sample of a data processing script for the Gnuroscan system.
# Copyright (c) 1996 Matthew Belmonte
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# If you find this program useful, please send mail to Matthew Belmonte.
# <mkb4@Cornell.edu>.  If you base a publication on data processed by this
# program, please notify Matthew Belmonte and include the following citation
# in your publication:
#
#	Matthew Belmonte, `A Software System for Analysis of
#	Steady-State Evoked Potentials', Association for Computing
#	Machinery SIGBIO Newsletter 17:1:9-14 (April 1997).
#
set LDR=$home/fastshft/src/no_pca.ldr
set LP=/bin/lp
set detrend_window=112
set smooth_window=28

#concatenate the separate data files
cntcat $1'1.cnt' $1'2.cnt' $1'3.cnt' $1'4.cnt' $1'5.cnt' $1'6.cnt' $1.cnt

#compute the Hjorth transformation
lderiv $LDR $1.cnt $1_h.cnt
rm -f $1.cnt
mkdir histo
cp $1_h.cnt histo/histo.cnt

#score the Hjorth-transformed data
score $1_h.cnt

#average the scored data
average $1_h.cnt
rm -f $1_h.cnt *.bin *.bw

#compute and plot HEOG histograms, and compute eye-hand correlations
cd histo
score histo.cnt 360.0
#eyehand histo.cnt
#eyehand histo.cnt 2
#eyehand histo.cnt 3
#eyehand histo.cnt 4
#eyehand histo.cnt 5
#eyehand histo.cnt 6
#eyehand histo.cnt 7
#eyehand histo.cnt 8
heoghisto histo.cnt
set files=`/bin/ls ??_histo.bw`
if($#files != 0) then
  mv -f $files ..
else
  mv -f ??_histo.bin ..
endif
cd ..
rm -rf histo
foreach histogram (`/bin/ls ?h_histo.b*`)
	tops $histogram | $LP
end

#detrend and smooth the time-domain averages
set files=`/bin/ls *t.avg`
foreach infile ($files)
	set tmpfile=`echo $infile | sed -e s/\\.avg/d.avg/`
	set outfile=`echo $tmpfile | sed -e s/\\.avg/s.avg/`
	detrend $infile $tmpfile $detrend_window
	smooth $tmpfile $outfile $smooth_window
	rm -f $tmpfile
end

#FFT all time-domain averages
foreach infile ($files)
	set prefix=`echo $infile | sed -e s/\\.avg//`
	power $prefix $detrend_window
end

#smooth and plot the low-frequency frequency-domain averages
set files=`/bin/ls *009.avg *018.avg`
foreach infile ($files)
	set outfile=`echo $infile | sed -e s/\\.avg/s.avg/`
	smooth $infile $outfile $detrend_window
end

#calculate smoothed gamma-band amplitude
#foreach side (l r)
#    foreach bin (0 1 2 3)
#	set files=""
#	foreach freq (36 45 54 63 71 80 89 98)
#	    set files=($files $side'h'$bin'f0'$freq'.avg')
#	end
#	set tmpfile=$side'h'$bin'g.avg'
#	addavg $files $tmpfile
#	smooth $tmpfile $side'h'$bin'gs.avg' $smooth_window
#	rm -f $tmpfile
#    end
#end

#delete intermediate files
#rm -f *[0123456789].avg

#plot phase-locked and free-running smoothed frequency-domain averages
#set files=`/bin/ls lh?t009s.avg lh?t018s.avg`
set files=`/bin/ls lh?t009s.avg`
foreach left_phasic ($files)
	set right_phasic=`echo $left_phasic | sed -e s/l/r/`
	set left_free=`echo $left_phasic | sed -e s/t/f/`
	set right_free=`echo $right_phasic | sed -e s/t/f/`
	set name=`echo $left_phasic | sed -e s/l/_/ | sed -e s/t/_/`
	fplotavg "$1 $name" 15 5 $left_phasic $right_phasic $left_free $right_free | $LP
end

#compute left-right differences
foreach left (`/bin/ls lh??009s.avg`)
	set right = `echo $left | sed -e s/l/r/`
	set difference = `echo $left | sed -e s/l/d/`
	cp $right temp.avg
	scale temp.avg -1
	addavg $left temp.avg $difference
	rm -f temp.avg
end

#use the one second of averaged EEG immediately preceding the stimulus in bin 2
#as the baseline for the z-transformation
subepoch -1000 0 dh2t009s.avg tbase.avg
subepoch -1000 0 dh2f009s.avg fbase.avg

#compute the z-transformation
foreach difference (`/bin/ls dh?t009s.avg`)
	set zscore = `echo $difference | sed -e s/d/z/`
	ztrans tbase.avg $difference $zscore
end
foreach difference (`/bin/ls dh?f009s.avg`)
	set zscore = `echo $difference | sed -e s/d/z/`
	ztrans fbase.avg $difference $zscore
end
rm -f tbase.avg fbase.avg

#compute local extremes
foreach difference (`/bin/ls dh??009s.avg dh??018s.avg`)
	set xtrm = `echo $difference | sed -e s/d/x/`
	extreme $difference $xtrm $detrend_window
end

#plot gamma-band free-running smoothed frequency-domain averages
#set files=`/bin/ls lh?gs.avg`
#foreach left_gamma ($files)
#	set right_gamma=`echo $left_gamma | sed -e s/l/r/`
#	set name=`echo $left_gamma | sed -e s/l/_/`
#	fplotavg "$1 $name" 5 1 $left_gamma $right_gamma | $LP
#end
