#! /usr/bin/gawk -f # Last edited on 2017-09-21 14:28:41 by jstolfi BEGIN \ { # USAGE: "ARGV[0] -v phase={PHASE} {INFILE} > {OUTFILE} # Reads an isolated run, extracts the frames of the fixation or stimulus phase. # Omits all header lines. # User must define with "-v" the variable {phase} as either "FX" or "ST". abort = -1; if (phase == "") { arg_error(("must define {phase}")); } # Specific for the 2013 128-electrode dataset ne = 128; # Number of electrodes. nc = 130; # Number of channels. # Define the index {icmk} of the phase marker channel (from 1): if (phase == "FX") { icmk = 129; } else if (phase == "ST") { icmk = 130; } else { arg_error(("unknown phase code \"" phase "\"")); } nt_read = 0; # Number of frames seen. nt_write = 0; # Number of frames in selected phase. prev_up = 0; # True if the phase marker channel was positive in previous frame. nups = 0; # Number of up-transaitions of the phase market channel. it_ini = -1; # Index of first frame in phase. it_fin = -1; # Index of last frame in phase. } (abort >= 0) { exit(abort); } # Ignore comments and headers: /^[ ]*[a-zA-Z\#]/ { next; } /^[ ]*$/ { next; } /^[ ]*[-+0-9]/ \ { if (NF != nc) { data_error(("invalid line format")); } it = nt_read; # Index of current frame (from 0). nt_read++; vmk = 0 + $(icmk); # Value of marker channel for selected phase. if (vmk > 0) { if (prev_up == 0) { # Start of selected phase. if (nups > 0) { data_error(("phase \"" phase "\" occurs more than once in run")); } it_ini = it; nups++; } it_fin = it; # Output the frame: print; nt_write++; prev_up = 1; } else { prev_up = 0; } next; } // { data_error(("invalid line format")); } END { if (abort >= 0) { exit(abort); } if (prev_up != 0) { data_error(("phase \"" phase "\" extends to end of file")); } printf "%s %d frames read %d written", FILENAME, nt_read, nt_write > "/dev/stderr"; printf " %d..%d\n", it_ini, it_fin > "/dev/stderr"; } function arg_error(msg) { printf "** %s\n", msg > "/dev/stderr"; abort = 1; exit(1); } function data_error(msg) { printf " «%s»\n", $0 > "/dev/stderr"; printf "%s:%d: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; abort = 1; exit(1); }