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: /* napgraph.c tomwalters@0: * tomwalters@0: * M. Akeroyd 11th June 1993. Revised Winter 1994. tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "tip.h" tomwalters@0: tomwalters@0: tomwalters@0: /* Function declarations */ tomwalters@0: /*---------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: void parsecommandline (int argc, char *argv[], char inputfn[], char outputfn[]); tomwalters@0: tomwalters@0: int readheader_nap(FILE *inputfp); tomwalters@0: void checkheader(); tomwalters@0: void writeheader(FILE *outputfp); tomwalters@0: int fakeheader_sai(); tomwalters@0: void changeheader(int no_frames, int new_pwidth, int new_nwidth, int framewidth_samples_output); tomwalters@0: tomwalters@0: void find_intervals_nap(int channel, int maximum_interval, int sample); 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: tomwalters@0: void write_matrix_hori_time(int framewidth, FILE *outputfigurefp); tomwalters@0: void write_matrix_hori_freq(int framewidth, FILE *outputfigurefp); tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* Data arrays: global */ tomwalters@0: /*---------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: short freqdata[MAX_CHANNELS]; /* input .nap: a single column */ tomwalters@0: short previousfreqdata[MAX_CHANNELS]; tomwalters@0: short previouspreviousfreqdata[MAX_CHANNELS]; tomwalters@0: short outputdata[MAX_DATA]; tomwalters@0: short outputfiguredata[MAX_DATA]; tomwalters@0: short outputgrounddata[MAX_DATA]; tomwalters@0: tomwalters@0: short interval[MAX_CHANNELS][MAX_DATA]; /* histogram */ tomwalters@0: short summation[MAX_CHANNELS]; tomwalters@0: tomwalters@0: short clip[MAX_CHANNELS]; /* set to ON of clipping occured*/ tomwalters@0: tomwalters@0: tomwalters@0: /* other variables */ tomwalters@0: /*---------------------------------------------------------------------------*/ tomwalters@0: struct Peak peak[MAX_PEAKS]; tomwalters@0: struct Peak peak_nap_right[MAX_CHANNELS]; tomwalters@0: struct Peak peak_nap_left[MAX_CHANNELS]; 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: 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: int no_columns; tomwalters@0: int nap_height; tomwalters@0: int greyscaleflag = OFF; 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 removeflag = OFF; /* -r */ tomwalters@0: double removevalue = 0.1; tomwalters@0: int widthflag = OFF; /* -w */ /* if OFF, use DEFAULT */ tomwalters@0: int total_width; /* -w */ /* total width of new .sai */ tomwalters@0: int asciiflag = OFF; tomwalters@0: int asciireverseflag = OFF; tomwalters@0: int summationflag = OFF; tomwalters@0: int oppositearchflag = OFF; /* -oparch : see saigraph.c for info */ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* ................... Main .............................*/ 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 column, channel; tomwalters@0: int framewidth_samples_input, tomwalters@0: framewidth_samples_output; tomwalters@0: int new_pwidth, tomwalters@0: new_nwidth; tomwalters@0: int header_bytes = 0; 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: tomwalters@0: strcpy(progname, argv[0]); tomwalters@0: strcpy(inputfn, ""); tomwalters@0: strcpy(outputbasefn, ""); tomwalters@0: strcpy(outputfigurefn, ""); tomwalters@0: tomwalters@0: /* parse command line */ tomwalters@0: parsecommandline(argc, argv, inputfn, outputbasefn); tomwalters@0: tomwalters@0: if (strcmp(outputbasefn, "") == 0) tomwalters@0: strcpy(outputfigurefn, ""); tomwalters@0: else { tomwalters@0: strcpy(outputfigurefn, outputbasefn); tomwalters@0: /* strcat(outputfigurefn, OUTPUT_EXT);*/ tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: /* open files */ tomwalters@0: /* default directions are: tomwalters@0: * input = stdin tomwalters@0: * figure = stdout tomwalters@0: */ tomwalters@0: tomwalters@0: inputfp = open_file(inputfn, stdin, READ); tomwalters@0: outputfigurefp = open_file(outputfigurefn, stdout, WRITE); tomwalters@0: tomwalters@0: /* read Header */ tomwalters@0: header_bytes = readheader_nap(inputfp); tomwalters@0: tomwalters@0: /* do some error-checking on the header, and set the fread pointer tomwalters@0: * to the right place */ tomwalters@0: checkheader(); tomwalters@0: tomwalters@0: /* fake the .sai header, but first copy the important variables: tomwalters@0: * no of columns (no_frames) tomwalters@0: * height (frameheight) tomwalters@0: */ tomwalters@0: no_columns = no_frames; tomwalters@0: nap_height = frameheight; tomwalters@0: header_lines = fakeheader_sai(); tomwalters@0: tomwalters@0: /* reset the variables */ tomwalters@0: framewidth_samples_input = framewidth_samples; tomwalters@0: if (widthflag == OFF) { tomwalters@0: new_pwidth = + PWIDTH; tomwalters@0: new_nwidth = - NWIDTH;} tomwalters@0: else { tomwalters@0: new_pwidth = + NEW_PWIDTH; tomwalters@0: new_nwidth = - (new_pwidth + total_width);} tomwalters@0: tomwalters@0: framewidth_samples_output = (int) ((int) (abs(new_pwidth) + abs(new_nwidth)) * samplerate ) / 1000; tomwalters@0: tomwalters@0: if (framewidth_samples_output >= MAX_DATA) { tomwalters@0: fprintf(stderr, "%s: new frame is too wide at %i: only alllowed %i samples\n", progname, framewidth_samples_output, MAX_DATA); tomwalters@0: exit(-1); } tomwalters@0: tomwalters@0: tomwalters@0: /* change header */ tomwalters@0: changeheader(1, new_pwidth, new_nwidth, framewidth_samples_output); tomwalters@0: tomwalters@0: /* write header */ tomwalters@0: if (asciiflag == OFF) tomwalters@0: writeheader(outputfigurefp); tomwalters@0: else { tomwalters@0: /* begining of ascii header. The rest depends on the matrix format ... */ tomwalters@0: fprintf(outputfigurefp, "# napgraph output. %s\n", inputfn); tomwalters@0: fprintf(outputfigurefp, "# \n"); tomwalters@0: fprintf(outputfigurefp, "# samplerate=%i pwidth=%d nwidth=%d columns=%i ", samplerate, new_pwidth, new_nwidth, no_columns); tomwalters@0: fprintf(outputfigurefp, " mincf=%i maxcf=%i channels=%i \n", mincf, maxcf, nap_height); tomwalters@0: fprintf(outputfigurefp, "# \n"); tomwalters@0: } tomwalters@0: tomwalters@0: /* --------------------------------------------------------------------------*/ tomwalters@0: tomwalters@0: tomwalters@0: if (verboseflag==ON) { tomwalters@0: fprintf(stderr, " channels "); tomwalters@0: fflush(stderr);} tomwalters@0: tomwalters@0: tomwalters@0: /* clear arrays */ tomwalters@0: tomwalters@0: for (channel=0; channel=1; channel--) tomwalters@0: freqdata[channel] = freqdata[channel-1]; tomwalters@0: tomwalters@0: /* This next is a simple input check: if any numbers < 0 , then say so */ tomwalters@0: for(channel=0; channel 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, " -a ASCII output: rows = channels\n"); tomwalters@0: fprintf(stderr, " -ar ASCII output: rows = time intervals\n"); tomwalters@0: fprintf(stderr, " -s include summations (ASCII only)\n"); tomwalters@0: fprintf(stderr, "\n"); tomwalters@0: fprintf(stderr, " -w width of graph.sai (msecs).\n"); tomwalters@0: fprintf(stderr, " -r remove any peaks F(n) < F(n+1) & F(n-1) * fract \n"); tomwalters@0: fprintf(stderr, " default = %.3f\n", removevalue); tomwalters@0: fprintf(stderr, "\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, "\n\n"); tomwalters@0: exit(-1);} 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: tomwalters@0: tomwalters@0: tomwalters@0: