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