#! /usr/bin/gawk -f # Last edited on 2013-11-24 21:08:24 by stolfilocal BEGIN { # Reads an isolated run, prints the range and avg of each electrode within the fixation and stimulus phases. abort = -1; # Specific for the 2013 128-electrode dataset ne = 128; # Number of electrodes. nc = 130; # Number of channels. # The phase index {ip} ranges in {1..np} split("", icmk); # {icmk[ip]} is the marker channel's index (from 1) for phase {ip}. split("", phname); # {phname[ip]} is the name of phase {ip}. np = 0; icmk[1] = 129; phname[1] = "FX"; np++; # Fixation. icmk[2] = 130; phname[2] = "ST"; np++; # Stimulus. split("", ns); # {ns[ip]} is number of times phase {ip} was seen. split("", nt); # {nt[ip]} is number of frames in phase {ip}. split("", it_ini); # {it_ini[ip]} is the index (from 0) of the first frame of phase {ip}. split("", it_fin); # {it_fin[ip]} is the index (from 0) of the last frame of phase {ip}. split("", min_v); # {minv[ip,ie]} is min value of electrode {ie} (from 1) during phase {ip} (1 or 2). split("", max_v); # {maxv[ip,ie]} is max value of electrode {ie} (from 1) during phase {ip} (1 or 2). split("", sum_v); # {sumv[ip,ie]} is total value of electrode {ie} (from 1) during phase {ip} (1 or 2) split("", up); # {up[ip]} tells whether the marker for phase {ip} was positive (1) or zero (0) in last frame. for (ip = 1; ip <= np; ip++) { ns[ip] = 0; nt[ip] = 0; it_ini[ip] = +9999999; it_fin[ip] = +9999999; up[ip] = 0; for (ie = 1; ie <= ne; ie++) { min_v[ip,ie] = +9999999; max_v[ip,ie] = -9999999; sum_v[ip,ie] = 0; } } nframes = 0; # Number of data frames seen. } (abort >= 0) { exit(abort); } # Ignore comments and headers: /^[ ]*[a-zA-Z\#]/ { next; } /^[ ]*$/ { next; } /^[ ]*[-+0-9]/ { if (NF != nc) { data_error(("invalid line format")); } it = nframes; # Index of current frame (from 0). prev_ip = -1; # Last phase active in this frame, or -1 if none. for (ip = 1; ip <= np; ip++) { vmk = 0 + $(icmk[ip]); # Value of marker channel for phase {ip}. if (vmk > 0) { # Frame belongs to phase {ip}: nt[ip]++; # One more frame in phase. if (up[ip] == 0) { # Start of phase {ip}. if (ns[ip] > 0) { data_error(("phase " ip " occurs more than once in run")); } it_ini[ip] = it; ns[ip]++; } it_fin[ip] = it; for (ie = 1; ie <= ne; ie++) { ve = 0 + $(ie); # Value of electrode {ie}. if (ve < min_v[ip,ie]) { min_v[ip,ie] = ve; } if (ve > max_v[ip,ie]) { max_v[ip,ie] = ve; } sum_v[ip,ie] += ve; } up[ip] = 1; if (prev_ip > 0) { data_error(("phases " prev_ip " and " ip " both on")); } prev_ip = ip; } } nframes ++; next; } // { data_error(("invalid line format")); } END { if (abort >= 0) { exit(abort); } printf "%d frames read\n", nframes > "/dev/stderr"; for (ip = 1; ip <= np; ip++) { printf "phase %s :", phname[ip]; if (ns[ip] == 0) { data_error(("phase " ip " does not occur in run")); } printf " spans %d frames %6d .. %6d", nt[ip], it_ini[ip], it_fin[ip]; printf "\n"; } for (ie = 1; ie <= ne; ie++) { printf "electrode %3d :", ie; for (ip = 1; ip <= np; ip++) { printf " %s ", phname[ip]; printf " range %+9.2f _ %+9.2f ", min_v[ip,ie], max_v[ip,ie] printf " del %9.2f ", max_v[ip,ie] - min_v[ip,ie] ;; printf " avg %+9.2f", sum_v[ip,ie]/nt[ip]; } printf "\n"; } } 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); }