tomwalters@0: /* tomwalters@0: Copyright (c) Applied Psychology Unit, Medical Research Council. 1993 tomwalters@0: =========================================================================== tomwalters@0: tomwalters@0: Permission to use, copy, modify, and distribute this software without fee tomwalters@0: is hereby granted for research purposes, provided that this copyright tomwalters@0: notice appears in all copies and in all supporting documentation, and that tomwalters@0: the software is not redistributed for any fee (except for a nominal tomwalters@0: shipping charge). Anyone wanting to incorporate all or part of this tomwalters@0: software in a commercial product must obtain a license from the Medical tomwalters@0: Research Council. tomwalters@0: tomwalters@0: The MRC makes no representations about the suitability of this tomwalters@0: software for any purpose. It is provided "as is" without express or tomwalters@0: implied warranty. tomwalters@0: tomwalters@0: THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING tomwalters@0: ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL tomwalters@0: THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES tomwalters@0: OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, tomwalters@0: WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, tomwalters@0: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS tomwalters@0: SOFTWARE. tomwalters@0: */ tomwalters@0: /*---------------------------------------------------------------------------*/ tomwalters@0: /* tomwalters@0: * saisummary tomwalters@0: * tomwalters@0: * builds a summary .sai tomwalters@0: * Output is one channel. tomwalters@0: * tomwalters@0: * M. Akeroyd. May 1993. Version 2.0 tomwalters@0: * Revised Winter 1994. tomwalters@0: * revised Autumn 1993. tomwalters@0: * variuos fiddles added Summer 1994. tomwalters@0: * (and some new options) tomwalters@0: * and some more Spring 1995. tomwalters@0: * and more Summer 1995. tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include tomwalters@0: tomwalters@0: #include "tip.h" tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* Function declarations */ tomwalters@0: /*---------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: void parsecommandline(int argc, char *argv[], char inputfn[], char outputfn[]); tomwalters@0: tomwalters@0: int readheader(FILE *inputfp); tomwalters@0: void checkheader(); tomwalters@0: void writeheader(FILE *outputfp); tomwalters@0: tomwalters@0: void writedata_output (FILE *outputfp, int samples_to_write); tomwalters@0: FILE *open_file (char filefn[], FILE *dir_default, int streamtype); tomwalters@0: void copytooutput_single(int no_peaks, int frame, int channel, int trigger_peak); tomwalters@0: tomwalters@0: int findpeaksreverse(); tomwalters@0: int findbigpeaks(int total_peaks); tomwalters@0: void integratebigpeaks(int total_bigpeaks); tomwalters@0: tomwalters@0: void changeheader(int no_frames, int new_pwidth, int new_nwidth, int framewidth_samples_output); tomwalters@0: void changeheader_B(int new_mincf, int new_maxcf, int new_channels); tomwalters@0: tomwalters@0: void write_matrix_hori_time(int framewidth, FILE *outputfigurefp); tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* Data arrays: global */ tomwalters@0: /*--------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: short inputdata[MAX_DATA]; tomwalters@0: short outputdata[MAX_DATA]; tomwalters@0: short outputfiguredata[MAX_DATA]; tomwalters@0: short outputgrounddata[MAX_DATA]; tomwalters@0: float summary[MAX_DATA]; tomwalters@0: float framesummary[MAX_DATA]; tomwalters@0: tomwalters@0: short interval[MAX_CHANNELS][MAX_DATA]; /* required for matrix.c */ tomwalters@0: short summation[MAX_CHANNELS]; /* required for matrix.c */ tomwalters@0: tomwalters@0: int clip[MAX_DATA]; tomwalters@0: tomwalters@0: /* other variables */ tomwalters@0: /*-------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: /* variables read from header */ tomwalters@0: /*-------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: char header[MAX_LINES_HEADER][MAX_LINE_LENGTH]; tomwalters@0: int header_lines; tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* .sai parameters */ tomwalters@0: /*-------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: int no_frames; tomwalters@0: int frameheight; /* number of channels */ tomwalters@0: int framewidth_samples; /* pwidth + nwidth * samplerate */ tomwalters@0: int frameshift_samples; /* frstep_aid * samplerate */ tomwalters@0: int pwidth; /* in msecs */ tomwalters@0: int nwidth; /* in msecs: NEGATIVE */ tomwalters@0: int width_win; /* pixels */ tomwalters@0: int height_win; /* pixels */ tomwalters@0: long samplerate; /* samples per sec */ tomwalters@0: int mincf; /* Hz */ tomwalters@0: int maxcf; /* Hz */ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* misc */ tomwalters@0: /*---------------- ---------------------------------------------------------*/ tomwalters@0: tomwalters@0: char progname[MAX_STRING_LENGTH]; tomwalters@0: char outputfigurefn[MAX_STRING_LENGTH]; tomwalters@0: tomwalters@0: tomwalters@0: int verboseflag = OFF; /* -v */ tomwalters@0: int summationflag = OFF; tomwalters@0: int frameflag = OFF; /* -f */ tomwalters@0: int asciiflag = OFF; tomwalters@0: int greyscaleflag = OFF; tomwalters@0: int divideflag = OFF; tomwalters@0: tomwalters@0: int oppositearchflag = OFF; /* -oparch : see saigraph.c for info */ tomwalters@0: tomwalters@0: int nstopflag = ON; tomwalters@0: int nwarnflag = OFF; tomwalters@0: int n32760flag = OFF; tomwalters@0: int n0flag = OFF; tomwalters@0: int headeroutputflag = ON; tomwalters@0: int singleframeflag = OFF; tomwalters@0: int frameaverageflag = ON; tomwalters@0: int specificchannelflag = OFF; tomwalters@0: int cutchannels[MAX_CHANNELS]; tomwalters@0: int nchannels=0; tomwalters@0: tomwalters@0: int framestart; tomwalters@0: int frameend; tomwalters@0: int cut_framestart; tomwalters@0: int cut_frameend; tomwalters@0: tomwalters@0: tomwalters@0: /* .............. Main .............................*/ tomwalters@0: /* .....................................................................*/ tomwalters@0: /* .....................................................................*/ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: void main (int argc, char *argv[]) tomwalters@0: { tomwalters@0: int sample; tomwalters@0: int n; tomwalters@0: tomwalters@0: int frame, channel; tomwalters@0: int trigger_peak = 0; tomwalters@0: int no_peak = 0, total_peaks =0; tomwalters@0: int total_bigpeaks = 0; tomwalters@0: int trueframeheight = 0; tomwalters@0: int header_bytes = 0; tomwalters@0: tomwalters@0: double temp; tomwalters@0: tomwalters@0: char inputfn[MAX_STRING_LENGTH], tomwalters@0: outputbasefn[MAX_STRING_LENGTH]; tomwalters@0: tomwalters@0: FILE *inputfp, *outputfigurefp; tomwalters@0: tomwalters@0: strcpy(progname, argv[0]); tomwalters@0: strcpy(inputfn, ""); tomwalters@0: strcpy(outputbasefn, ""); tomwalters@0: strcpy(outputfigurefn, ""); tomwalters@0: tomwalters@0: for (n=0; n= cut_framestart) && (frame <= cut_frameend)){ tomwalters@0: if (cutchannels[channel] == ON){ tomwalters@0: nchannels ++; tomwalters@0: /*fprintf(stderr, "c%d ", channel);*/ tomwalters@0: for(sample=0; sample 32760.0)||( temp < 0)) { tomwalters@0: if (clip[sample] == OFF) { tomwalters@0: fprintf(stderr, "\nclipping: time interval %i\n", sample); tomwalters@0: clip[sample] = ON; } tomwalters@0: temp = 32761.0; } tomwalters@0: interval[1][sample] = outputfiguredata[sample] = (short) temp; }} tomwalters@0: else { tomwalters@0: for(sample=0; sample 32760.0) || (temp < 0.0)){ tomwalters@0: if (clip[sample] == OFF) { tomwalters@0: fprintf(stderr, "\nclipping: time interval %i\n", sample); tomwalters@0: clip[sample] = ON; } tomwalters@0: temp = 32761.0; } tomwalters@0: interval[1][sample] = outputfiguredata[sample] = (short) temp; }} tomwalters@0: tomwalters@0: /* This next is a simple output check: */ tomwalters@0: for(sample=0; sample 32760 )||(summation[1] < 0 )) { tomwalters@0: if (clip[0] == OFF) { tomwalters@0: fprintf(stderr, "\nclipping: summation\n"); tomwalters@0: clip[0] = ON; } tomwalters@0: summation[1] = 32761;}} tomwalters@0: tomwalters@0: } tomwalters@0: tomwalters@0: /* add to frame average */ tomwalters@0: if ((frame >= cut_framestart) && (frame <= cut_frameend)){ tomwalters@0: for (sample=0; sample < framewidth_samples; sample++) tomwalters@0: framesummary[sample] += outputfiguredata[sample]; tomwalters@0: } tomwalters@0: tomwalters@0: /* write output: outputfiguredata[] for the .sai tomwalters@0: * interval[] & summation[] for the ascii tomwalters@0: * Note hack for frameheight tomwalters@0: * BUT: not if only 1 frame is to be output tomwalters@0: */ tomwalters@0: tomwalters@0: if ((frame >= cut_framestart) && (frame <= cut_frameend)){ tomwalters@0: if (singleframeflag == OFF) { tomwalters@0: if (asciiflag == OFF) tomwalters@0: writedata_output(outputfigurefp, framewidth_samples); tomwalters@0: else { tomwalters@0: trueframeheight=frameheight; tomwalters@0: frameheight=1; tomwalters@0: write_matrix_hori_time(framewidth_samples, outputfigurefp); tomwalters@0: frameheight=trueframeheight; }} tomwalters@0: else ; tomwalters@0: } tomwalters@0: tomwalters@0: /* -------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: } /* frame */ tomwalters@0: tomwalters@0: if (singleframeflag == ON) { tomwalters@0: if (verboseflag == ON) { tomwalters@0: fprintf(stderr, " averaging "); tomwalters@0: fflush(stderr);} tomwalters@0: clip[0] = OFF; tomwalters@0: for (sample=0; sample < framewidth_samples; sample++) { tomwalters@0: if (frameaverageflag == ON) tomwalters@0: temp = framesummary[sample]/(cut_frameend - cut_framestart + 1); tomwalters@0: else tomwalters@0: temp = framesummary[sample]; tomwalters@0: /* clip check */ tomwalters@0: if (temp > 32760) { tomwalters@0: if (clip[0] == OFF) { tomwalters@0: fprintf(stderr, "\nclipping: frameaverage at sample %i\n", sample); tomwalters@0: clip[0] = ON; } tomwalters@0: temp = 32760;} tomwalters@0: outputfiguredata[sample]= (short)temp; tomwalters@0: interval[1][sample] = outputfiguredata[sample];} tomwalters@0: if (asciiflag == OFF) tomwalters@0: writedata_output(outputfigurefp, framewidth_samples); tomwalters@0: else { tomwalters@0: trueframeheight=frameheight; tomwalters@0: frameheight=1; tomwalters@0: write_matrix_hori_time(framewidth_samples, outputfigurefp); tomwalters@0: frameheight=trueframeheight; }} tomwalters@0: else ; tomwalters@0: tomwalters@0: tomwalters@0: /* Tidy up and exit */ tomwalters@0: fprintf(stderr, "\n"); tomwalters@0: fclose(inputfp); tomwalters@0: fclose(outputfigurefp); tomwalters@0: tomwalters@0: if ( ferror(inputfp) != 0) { tomwalters@0: fprintf(stderr, " %s : error closing input file.\n", progname); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: if ( ferror(outputfigurefp) != 0) { tomwalters@0: fprintf(stderr, " %s : error closing figure file.\n", progname); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: exit(0); tomwalters@0: tomwalters@0: } /* Main */ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* .....................................................................*/ tomwalters@0: /* .....................................................................*/ tomwalters@0: /* .....................................................................*/ tomwalters@0: /* .....................................................................*/ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: void parsecommandline(int argc, char *argv[], char inputfn[], char outputfn[]) tomwalters@0: { tomwalters@0: int x=1, helpflag = OFF; tomwalters@0: int group; tomwalters@0: tomwalters@0: while (x < argc){ tomwalters@0: if (!strcmp(argv[x], "-o")) {strcpy(outputfn, argv[x+1]); x+=2;} tomwalters@0: else if (!strcmp(argv[x], "-output")) {strcpy(outputfn, argv[x+1]); x+=2;} tomwalters@0: else if (!strcmp(argv[x], "-i")) {strcpy(inputfn, argv[x+1]); x+=2;} tomwalters@0: else if (!strcmp(argv[x], "-input")) {strcpy(inputfn, argv[x+1]); x+=2;} tomwalters@0: else if (!strcmp(argv[x], "-help")) {helpflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-h")) {helpflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-verbose")) {verboseflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-v")) {verboseflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-s")) {summationflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-g")) {greyscaleflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-gray")) {greyscaleflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-grey")) {greyscaleflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-ar")) {asciiflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-d")) {divideflag = ON; x+=1;} tomwalters@0: else if (!strcmp(argv[x], "-channels")) {specificchannelflag = ON; tomwalters@0: x+=1; tomwalters@0: while (x input file default = stdin\n"); tomwalters@0: fprintf(stderr, " -o <.sai file> output file default = stdout\n"); tomwalters@0: fprintf(stderr, " -oparch assume input was generated on an opposing architecture cpu\n"); tomwalters@0: fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, " -frames (inclusive) (default=all)\n"); tomwalters@0: fprintf(stderr, " -channels <> <> ..... specify channel numbers to be included (1++)\n"); tomwalters@0: fprintf(stderr, " -d divide by no. of channels (Warning: precision may be lost).\n"); tomwalters@0: fprintf(stderr, " -ar ASCII output: rows=time intervals (requires -oneframe)\n"); tomwalters@0: fprintf(stderr, " -s include summations (ascii only)\n"); tomwalters@0: fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, " -header include AIM header in output file (default)\n"); tomwalters@0: fprintf(stderr, " -noheader don't ... ... ... \n"); tomwalters@0: fprintf(stderr, " -v verbose output (to stderr)\n"); tomwalters@0: fprintf(stderr, " -g change .sai format to 'grayscale'\n"); tomwalters@0: fprintf(stderr, " -oneframe average frames together; one (1) output frame\n"); tomwalters@0: fprintf(stderr, " -sumframe sum frames together; one (1) output frame\n"); tomwalters@0: fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, " -nstop if negative inputs found, stop processing (default)\n"); tomwalters@0: fprintf(stderr, " -nwarn if negative inputs found, continue (but warn user) \n"); tomwalters@0: fprintf(stderr, " -nignore if negative inputs found, continue \n"); tomwalters@0: fprintf(stderr, " -n0 if negative inputs found, replace with 0 \n"); tomwalters@0: fprintf(stderr, " -n32760 if negative inputs found, replace with 32760 \n"); tomwalters@0: fprintf(stderr, "\n\n"); tomwalters@0: exit(-1);} tomwalters@0: } tomwalters@0: tomwalters@0: /* The End. */ tomwalters@0: /*------------------------------------------------------------------------------------------------------*/