annotate saitools/napgraph.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
rev   line source
tomwalters@0 1 /*
tomwalters@0 2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1993
tomwalters@0 3 ===========================================================================
tomwalters@0 4
tomwalters@0 5 Permission to use, copy, modify, and distribute this software without fee
tomwalters@0 6 is hereby granted for research purposes, provided that this copyright
tomwalters@0 7 notice appears in all copies and in all supporting documentation, and that
tomwalters@0 8 the software is not redistributed for any fee (except for a nominal
tomwalters@0 9 shipping charge). Anyone wanting to incorporate all or part of this
tomwalters@0 10 software in a commercial product must obtain a license from the Medical
tomwalters@0 11 Research Council.
tomwalters@0 12
tomwalters@0 13 The MRC makes no representations about the suitability of this
tomwalters@0 14 software for any purpose. It is provided "as is" without express or
tomwalters@0 15 implied warranty.
tomwalters@0 16
tomwalters@0 17 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
tomwalters@0 18 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
tomwalters@0 19 THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
tomwalters@0 20 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
tomwalters@0 21 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
tomwalters@0 22 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
tomwalters@0 23 SOFTWARE.
tomwalters@0 24 */
tomwalters@0 25 /* napgraph.c
tomwalters@0 26 *
tomwalters@0 27 * M. Akeroyd 11th June 1993. Revised Winter 1994.
tomwalters@0 28 */
tomwalters@0 29
tomwalters@0 30
tomwalters@0 31
tomwalters@0 32
tomwalters@0 33
tomwalters@0 34 #include <stdio.h>
tomwalters@0 35 #include <string.h>
tomwalters@0 36 #include <stdlib.h>
tomwalters@0 37 #include "tip.h"
tomwalters@0 38
tomwalters@0 39
tomwalters@0 40 /* Function declarations */
tomwalters@0 41 /*---------------------------------------------------------------------------*/
tomwalters@0 42
tomwalters@0 43 void parsecommandline (int argc, char *argv[], char inputfn[], char outputfn[]);
tomwalters@0 44
tomwalters@0 45 int readheader_nap(FILE *inputfp);
tomwalters@0 46 void checkheader();
tomwalters@0 47 void writeheader(FILE *outputfp);
tomwalters@0 48 int fakeheader_sai();
tomwalters@0 49 void changeheader(int no_frames, int new_pwidth, int new_nwidth, int framewidth_samples_output);
tomwalters@0 50
tomwalters@0 51 void find_intervals_nap(int channel, int maximum_interval, int sample);
tomwalters@0 52
tomwalters@0 53 void writedata_output (FILE *outputfp, int samples_to_write);
tomwalters@0 54 FILE *open_file (char filefn[], FILE *dir_default, int streamtype);
tomwalters@0 55
tomwalters@0 56 void write_matrix_hori_time(int framewidth, FILE *outputfigurefp);
tomwalters@0 57 void write_matrix_hori_freq(int framewidth, FILE *outputfigurefp);
tomwalters@0 58
tomwalters@0 59
tomwalters@0 60
tomwalters@0 61
tomwalters@0 62 /* Data arrays: global */
tomwalters@0 63 /*---------------------------------------------------------------------------*/
tomwalters@0 64
tomwalters@0 65
tomwalters@0 66 short freqdata[MAX_CHANNELS]; /* input .nap: a single column */
tomwalters@0 67 short previousfreqdata[MAX_CHANNELS];
tomwalters@0 68 short previouspreviousfreqdata[MAX_CHANNELS];
tomwalters@0 69 short outputdata[MAX_DATA];
tomwalters@0 70 short outputfiguredata[MAX_DATA];
tomwalters@0 71 short outputgrounddata[MAX_DATA];
tomwalters@0 72
tomwalters@0 73 short interval[MAX_CHANNELS][MAX_DATA]; /* histogram */
tomwalters@0 74 short summation[MAX_CHANNELS];
tomwalters@0 75
tomwalters@0 76 short clip[MAX_CHANNELS]; /* set to ON of clipping occured*/
tomwalters@0 77
tomwalters@0 78
tomwalters@0 79 /* other variables */
tomwalters@0 80 /*---------------------------------------------------------------------------*/
tomwalters@0 81 struct Peak peak[MAX_PEAKS];
tomwalters@0 82 struct Peak peak_nap_right[MAX_CHANNELS];
tomwalters@0 83 struct Peak peak_nap_left[MAX_CHANNELS];
tomwalters@0 84
tomwalters@0 85
tomwalters@0 86 /* variables read from header */
tomwalters@0 87 /*---------------------------------------------------------------------------*/
tomwalters@0 88
tomwalters@0 89 char header[MAX_LINES_HEADER][MAX_LINE_LENGTH];
tomwalters@0 90 int header_lines;
tomwalters@0 91
tomwalters@0 92 int no_frames;
tomwalters@0 93 int frameheight; /* number of channels */
tomwalters@0 94 int framewidth_samples; /* pwidth + nwidth * samplerate */
tomwalters@0 95 int frameshift_samples; /* frstep_aid * samplerate */
tomwalters@0 96 int pwidth; /* in msecs */
tomwalters@0 97 int nwidth; /* in msecs: NEGATIVE */
tomwalters@0 98 int width_win; /* pixels */
tomwalters@0 99 int height_win; /* pixels */
tomwalters@0 100 long samplerate; /* samples per sec */
tomwalters@0 101 int mincf; /* Hz */
tomwalters@0 102 int maxcf; /* Hz */
tomwalters@0 103
tomwalters@0 104 int no_columns;
tomwalters@0 105 int nap_height;
tomwalters@0 106 int greyscaleflag = OFF;
tomwalters@0 107
tomwalters@0 108
tomwalters@0 109 /* misc */
tomwalters@0 110 /*---------------------------------------------------------------------------*/
tomwalters@0 111
tomwalters@0 112 char progname[MAX_STRING_LENGTH];
tomwalters@0 113 char outputfigurefn[MAX_STRING_LENGTH];
tomwalters@0 114
tomwalters@0 115
tomwalters@0 116 int verboseflag = OFF; /* -v */
tomwalters@0 117 int removeflag = OFF; /* -r */
tomwalters@0 118 double removevalue = 0.1;
tomwalters@0 119 int widthflag = OFF; /* -w */ /* if OFF, use DEFAULT */
tomwalters@0 120 int total_width; /* -w */ /* total width of new .sai */
tomwalters@0 121 int asciiflag = OFF;
tomwalters@0 122 int asciireverseflag = OFF;
tomwalters@0 123 int summationflag = OFF;
tomwalters@0 124 int oppositearchflag = OFF; /* -oparch : see saigraph.c for info */
tomwalters@0 125
tomwalters@0 126
tomwalters@0 127
tomwalters@0 128 /* ................... Main .............................*/
tomwalters@0 129 /* ..........................................................................*/
tomwalters@0 130 /* ..........................................................................*/
tomwalters@0 131
tomwalters@0 132
tomwalters@0 133
tomwalters@0 134 void main (int argc, char *argv[])
tomwalters@0 135 {
tomwalters@0 136 int sample;
tomwalters@0 137 int n;
tomwalters@0 138
tomwalters@0 139 int column, channel;
tomwalters@0 140 int framewidth_samples_input,
tomwalters@0 141 framewidth_samples_output;
tomwalters@0 142 int new_pwidth,
tomwalters@0 143 new_nwidth;
tomwalters@0 144 int header_bytes = 0;
tomwalters@0 145
tomwalters@0 146 char inputfn[MAX_STRING_LENGTH],
tomwalters@0 147 outputbasefn[MAX_STRING_LENGTH];
tomwalters@0 148
tomwalters@0 149 FILE *inputfp, *outputfigurefp;
tomwalters@0 150
tomwalters@0 151
tomwalters@0 152 strcpy(progname, argv[0]);
tomwalters@0 153 strcpy(inputfn, "");
tomwalters@0 154 strcpy(outputbasefn, "");
tomwalters@0 155 strcpy(outputfigurefn, "");
tomwalters@0 156
tomwalters@0 157 /* parse command line */
tomwalters@0 158 parsecommandline(argc, argv, inputfn, outputbasefn);
tomwalters@0 159
tomwalters@0 160 if (strcmp(outputbasefn, "") == 0)
tomwalters@0 161 strcpy(outputfigurefn, "");
tomwalters@0 162 else {
tomwalters@0 163 strcpy(outputfigurefn, outputbasefn);
tomwalters@0 164 /* strcat(outputfigurefn, OUTPUT_EXT);*/
tomwalters@0 165 }
tomwalters@0 166
tomwalters@0 167
tomwalters@0 168 /* open files */
tomwalters@0 169 /* default directions are:
tomwalters@0 170 * input = stdin
tomwalters@0 171 * figure = stdout
tomwalters@0 172 */
tomwalters@0 173
tomwalters@0 174 inputfp = open_file(inputfn, stdin, READ);
tomwalters@0 175 outputfigurefp = open_file(outputfigurefn, stdout, WRITE);
tomwalters@0 176
tomwalters@0 177 /* read Header */
tomwalters@0 178 header_bytes = readheader_nap(inputfp);
tomwalters@0 179
tomwalters@0 180 /* do some error-checking on the header, and set the fread pointer
tomwalters@0 181 * to the right place */
tomwalters@0 182 checkheader();
tomwalters@0 183
tomwalters@0 184 /* fake the .sai header, but first copy the important variables:
tomwalters@0 185 * no of columns (no_frames)
tomwalters@0 186 * height (frameheight)
tomwalters@0 187 */
tomwalters@0 188 no_columns = no_frames;
tomwalters@0 189 nap_height = frameheight;
tomwalters@0 190 header_lines = fakeheader_sai();
tomwalters@0 191
tomwalters@0 192 /* reset the variables */
tomwalters@0 193 framewidth_samples_input = framewidth_samples;
tomwalters@0 194 if (widthflag == OFF) {
tomwalters@0 195 new_pwidth = + PWIDTH;
tomwalters@0 196 new_nwidth = - NWIDTH;}
tomwalters@0 197 else {
tomwalters@0 198 new_pwidth = + NEW_PWIDTH;
tomwalters@0 199 new_nwidth = - (new_pwidth + total_width);}
tomwalters@0 200
tomwalters@0 201 framewidth_samples_output = (int) ((int) (abs(new_pwidth) + abs(new_nwidth)) * samplerate ) / 1000;
tomwalters@0 202
tomwalters@0 203 if (framewidth_samples_output >= MAX_DATA) {
tomwalters@0 204 fprintf(stderr, "%s: new frame is too wide at %i: only alllowed %i samples\n", progname, framewidth_samples_output, MAX_DATA);
tomwalters@0 205 exit(-1); }
tomwalters@0 206
tomwalters@0 207
tomwalters@0 208 /* change header */
tomwalters@0 209 changeheader(1, new_pwidth, new_nwidth, framewidth_samples_output);
tomwalters@0 210
tomwalters@0 211 /* write header */
tomwalters@0 212 if (asciiflag == OFF)
tomwalters@0 213 writeheader(outputfigurefp);
tomwalters@0 214 else {
tomwalters@0 215 /* begining of ascii header. The rest depends on the matrix format ... */
tomwalters@0 216 fprintf(outputfigurefp, "# napgraph output. %s\n", inputfn);
tomwalters@0 217 fprintf(outputfigurefp, "# \n");
tomwalters@0 218 fprintf(outputfigurefp, "# samplerate=%i pwidth=%d nwidth=%d columns=%i ", samplerate, new_pwidth, new_nwidth, no_columns);
tomwalters@0 219 fprintf(outputfigurefp, " mincf=%i maxcf=%i channels=%i \n", mincf, maxcf, nap_height);
tomwalters@0 220 fprintf(outputfigurefp, "# \n");
tomwalters@0 221 }
tomwalters@0 222
tomwalters@0 223 /* --------------------------------------------------------------------------*/
tomwalters@0 224
tomwalters@0 225
tomwalters@0 226 if (verboseflag==ON) {
tomwalters@0 227 fprintf(stderr, " channels ");
tomwalters@0 228 fflush(stderr);}
tomwalters@0 229
tomwalters@0 230
tomwalters@0 231 /* clear arrays */
tomwalters@0 232
tomwalters@0 233 for (channel=0; channel<MAX_CHANNELS; channel++) {
tomwalters@0 234 previousfreqdata[channel] = previouspreviousfreqdata[channel] = 0;
tomwalters@0 235 peak_nap_left[channel].sample = 0;
tomwalters@0 236 peak_nap_right[channel].sample = 0;
tomwalters@0 237 for(sample=0; sample<MAX_DATA; sample++)
tomwalters@0 238 interval[channel][sample] = 0;
tomwalters@0 239 clip[channel] = OFF;
tomwalters@0 240 }
tomwalters@0 241
tomwalters@0 242
tomwalters@0 243 /* Main loop */
tomwalters@0 244
tomwalters@0 245
tomwalters@0 246 /* ..... */
tomwalters@0 247
tomwalters@0 248 for (column=1; column<=no_columns; column++) {
tomwalters@0 249
tomwalters@0 250 if ((column % 100) == 1 ) {
tomwalters@0 251 if (verboseflag==ON) {
tomwalters@0 252 fprintf(stderr, " %i ", column);
tomwalters@0 253 fflush(stderr);}
tomwalters@0 254 }
tomwalters@0 255
tomwalters@0 256 /* load a column's worth of data */
tomwalters@0 257 fread (freqdata, 2, (size_t) nap_height, inputfp);
tomwalters@0 258
tomwalters@0 259 /* move everything UP one byte.
tomwalters@0 260 * Don't know why ... */
tomwalters@0 261 for (channel=nap_height; channel >=1; channel--)
tomwalters@0 262 freqdata[channel] = freqdata[channel-1];
tomwalters@0 263
tomwalters@0 264 /* This next is a simple input check: if any numbers < 0 , then say so */
tomwalters@0 265 for(channel=0; channel<nap_height; channel++)
tomwalters@0 266 if (freqdata[channel] < 0 ) {
tomwalters@0 267 fprintf(stderr, "%s: something's gone wrong: the data is negative.\n", progname);
tomwalters@0 268 exit(-1); }
tomwalters@0 269
tomwalters@0 270
tomwalters@0 271 /* the important bits ... */
tomwalters@0 272 for (channel=1; channel <=nap_height; channel++)
tomwalters@0 273 find_intervals_nap(channel, framewidth_samples_output, column);
tomwalters@0 274
tomwalters@0 275
tomwalters@0 276 } /* column */
tomwalters@0 277
tomwalters@0 278
tomwalters@0 279 /* --------------------------------------------------------------------------*/
tomwalters@0 280 /* --------------------------------------------------------------------------*/
tomwalters@0 281
tomwalters@0 282
tomwalters@0 283 fflush(stdout);
tomwalters@0 284
tomwalters@0 285 /* If required, sum the values */
tomwalters@0 286 if (summationflag==ON) {
tomwalters@0 287 /* clear ... */
tomwalters@0 288 for (channel = 1; channel <= frameheight; channel ++)
tomwalters@0 289 summation[channel] = 0;
tomwalters@0 290 /* set ... */
tomwalters@0 291 for (channel = 1; channel <= frameheight; channel ++)
tomwalters@0 292 for (sample = 0; sample < framewidth_samples_output; sample++)
tomwalters@0 293 summation[channel] += interval[channel][sample];
tomwalters@0 294 }
tomwalters@0 295
tomwalters@0 296
tomwalters@0 297 /* write output */
tomwalters@0 298
tomwalters@0 299 if (asciiflag == OFF) {
tomwalters@0 300 for (channel = 1; channel <= frameheight; channel ++){
tomwalters@0 301 for (sample = 0; sample < framewidth_samples_output; sample++)
tomwalters@0 302 outputfiguredata[sample] = interval[channel][sample];
tomwalters@0 303 writedata_output(outputfigurefp, framewidth_samples_output);
tomwalters@0 304 }
tomwalters@0 305 }
tomwalters@0 306 else {
tomwalters@0 307 frameheight = nap_height;
tomwalters@0 308 if (asciireverseflag == OFF)
tomwalters@0 309 write_matrix_hori_freq(framewidth_samples_output, outputfigurefp);
tomwalters@0 310 else
tomwalters@0 311 write_matrix_hori_time(framewidth_samples_output, outputfigurefp);
tomwalters@0 312 }
tomwalters@0 313
tomwalters@0 314
tomwalters@0 315 /* --------------------------------------------------------------------------*/
tomwalters@0 316 /* --------------------------------------------------------------------------*/
tomwalters@0 317
tomwalters@0 318
tomwalters@0 319 /* Tidy up and exit */
tomwalters@0 320 fprintf(stderr, "\n");
tomwalters@0 321 fclose(inputfp);
tomwalters@0 322 fclose(outputfigurefp);
tomwalters@0 323
tomwalters@0 324 if ( ferror(inputfp) != 0) {
tomwalters@0 325 fprintf(stderr, " %s : error closing input file.\n", progname);
tomwalters@0 326 exit(-1);}
tomwalters@0 327
tomwalters@0 328 if ( ferror(outputfigurefp) != 0) {
tomwalters@0 329 fprintf(stderr, " %s : error closing figure file.\n", progname);
tomwalters@0 330 exit(-1);}
tomwalters@0 331
tomwalters@0 332 exit(0);
tomwalters@0 333
tomwalters@0 334 } /* Main */
tomwalters@0 335
tomwalters@0 336
tomwalters@0 337
tomwalters@0 338
tomwalters@0 339
tomwalters@0 340
tomwalters@0 341
tomwalters@0 342
tomwalters@0 343 /* ..........................................................................*/
tomwalters@0 344 /* ..........................................................................*/
tomwalters@0 345 /* ..........................................................................*/
tomwalters@0 346 /* ..........................................................................*/
tomwalters@0 347
tomwalters@0 348
tomwalters@0 349
tomwalters@0 350
tomwalters@0 351
tomwalters@0 352
tomwalters@0 353
tomwalters@0 354
tomwalters@0 355
tomwalters@0 356
tomwalters@0 357 void parsecommandline(int argc, char *argv[], char inputfn[], char outputbasefn[])
tomwalters@0 358 {
tomwalters@0 359 int x=1, helpflag = OFF;
tomwalters@0 360 int group;
tomwalters@0 361
tomwalters@0 362 while (x < argc){
tomwalters@0 363 if (!strcmp(argv[x], "-o")) {strcpy(outputbasefn, argv[x+1]); x+=2;}
tomwalters@0 364 else if (!strcmp(argv[x], "-output")) {strcpy(outputbasefn, argv[x+1]); x+=2;}
tomwalters@0 365 else if (!strcmp(argv[x], "-i")) {strcpy(inputfn, argv[x+1]); x+=2;}
tomwalters@0 366 else if (!strcmp(argv[x], "-input")) {strcpy(inputfn, argv[x+1]); x+=2;}
tomwalters@0 367 else if (!strcmp(argv[x], "-help")) {helpflag = ON; x+=1;}
tomwalters@0 368 else if (!strcmp(argv[x], "-h")) {helpflag = ON; x+=1;}
tomwalters@0 369 else if (!strcmp(argv[x], "-verbose")) {verboseflag = ON; x+=1;}
tomwalters@0 370 else if (!strcmp(argv[x], "-v")) {verboseflag = ON; x+=1;}
tomwalters@0 371 else if (!strcmp(argv[x], "-w")) {widthflag = ON; total_width=atof(argv[x+1]); x+=2;}
tomwalters@0 372 else if (!strcmp(argv[x], "-width")) {widthflag = ON; total_width=atof(argv[x+1]); x+=2;}
tomwalters@0 373 else if (!strcmp(argv[x], "-v")) {verboseflag = ON; x+=1;}
tomwalters@0 374 else if (!strcmp(argv[x], "-r")) { removeflag = ON; removevalue = atof(argv[x+1]); x+=2;}
tomwalters@0 375 else if (!strcmp(argv[x], "-a")) { asciiflag = ON;x+=1;}
tomwalters@0 376 else if (!strcmp(argv[x], "-ar")) { asciiflag = ON; asciireverseflag=ON; x+=1;}
tomwalters@0 377 else if (!strcmp(argv[x], "-s")) { summationflag=ON; x+=1;}
tomwalters@0 378 else if (!strcmp(argv[x], "-g")) { greyscaleflag=ON; x+=1;}
tomwalters@0 379 else if (!strcmp(argv[x], "-grey")) { greyscaleflag=ON; x+=1;}
tomwalters@0 380 else if (!strcmp(argv[x], "-gray")) { greyscaleflag=ON; x+=1;}
tomwalters@0 381 else if (!strcmp(argv[x], "-oparch")) {oppositearchflag = ON; x+=1;}
tomwalters@0 382 else {fprintf(stderr, "%s: unknown option %s\n", progname, argv[x]);
tomwalters@0 383 exit(-1);}
tomwalters@0 384 }
tomwalters@0 385 if (helpflag == ON)
tomwalters@0 386 {
tomwalters@0 387 fprintf(stderr, "\n");
tomwalters@0 388 fprintf(stderr, " %s\n", progname);
tomwalters@0 389 fprintf(stderr, " -----------------------------------------------------------\n");
tomwalters@0 390 fprintf(stderr, " Works out a histogram of peak intervals, from a .nap\n");
tomwalters@0 391 fprintf(stderr, " Default output is a .sai file: if -a or -ar specified,\n");
tomwalters@0 392 fprintf(stderr, " then output is a ASCII matrix.\n");
tomwalters@0 393 fprintf(stderr, "\n");
tomwalters@0 394 fprintf(stderr, " The -oparch option is REQUIRED if reading a Sun .sai files \n");
tomwalters@0 395 fprintf(stderr, " on a DEC cpu or vice-versa.\n");
tomwalters@0 396 fprintf(stderr, " Add .nap to input filename, .sai to output filename.\n");
tomwalters@0 397 fprintf(stderr, " -----------------------------------------------------------\n");
tomwalters@0 398 fprintf(stderr, " -i <.nap file> input file default = stdin\n");
tomwalters@0 399 fprintf(stderr, " -o <.sai file> output file default = stdout\n");
tomwalters@0 400 fprintf(stderr, " -oparch assume input was generated on an opposing architecture cpu\n");
tomwalters@0 401 fprintf(stderr, "\n");
tomwalters@0 402 fprintf(stderr, " -a ASCII output: rows = channels\n");
tomwalters@0 403 fprintf(stderr, " -ar ASCII output: rows = time intervals\n");
tomwalters@0 404 fprintf(stderr, " -s include summations (ASCII only)\n");
tomwalters@0 405 fprintf(stderr, "\n");
tomwalters@0 406 fprintf(stderr, " -w <int> width of graph.sai (msecs).\n");
tomwalters@0 407 fprintf(stderr, " -r <fraction> remove any peaks F(n) < F(n+1) & F(n-1) * fract \n");
tomwalters@0 408 fprintf(stderr, " default = %.3f\n", removevalue);
tomwalters@0 409 fprintf(stderr, "\n");
tomwalters@0 410 fprintf(stderr, " -v verbose output (to stderr)\n");
tomwalters@0 411 fprintf(stderr, " -g Change .sai format to 'grayscale'.\n");
tomwalters@0 412 fprintf(stderr, "\n\n");
tomwalters@0 413 exit(-1);}
tomwalters@0 414
tomwalters@0 415 }
tomwalters@0 416
tomwalters@0 417
tomwalters@0 418
tomwalters@0 419
tomwalters@0 420
tomwalters@0 421
tomwalters@0 422
tomwalters@0 423
tomwalters@0 424
tomwalters@0 425
tomwalters@0 426
tomwalters@0 427
tomwalters@0 428
tomwalters@0 429
tomwalters@0 430
tomwalters@0 431
tomwalters@0 432
tomwalters@0 433