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 /* sairotate.c
|
tomwalters@0
|
26 * ------------
|
tomwalters@0
|
27 *
|
tomwalters@0
|
28 * 'Rotates' a .sai frame by 90degrees clockwise, so the horizontal
|
tomwalters@0
|
29 * scale is frequency, vertical image-time.
|
tomwalters@0
|
30 *
|
tomwalters@0
|
31 * If -R is specified, then put the trigger at the top, so ImageTime goes
|
tomwalters@0
|
32 * down. This is equiv to a 270degree rotation pluse
|
tomwalters@0
|
33 * a refelction about x=midpoint axis.
|
tomwalters@0
|
34 *
|
tomwalters@0
|
35 * If -T is specified, then start drawing from the "beginning" of the
|
tomwalters@0
|
36 * TriggerPeak (defined as the right nextevent).
|
tomwalters@0
|
37 *
|
tomwalters@0
|
38 *
|
tomwalters@0
|
39 * M Akeroyd. June 1993. v1.00 Revised Spring 1994.
|
tomwalters@0
|
40 */
|
tomwalters@0
|
41
|
tomwalters@0
|
42
|
tomwalters@0
|
43
|
tomwalters@0
|
44 #include <stdio.h>
|
tomwalters@0
|
45 #include <string.h>
|
tomwalters@0
|
46 #include <stdlib.h>
|
tomwalters@0
|
47 #include "tip.h"
|
tomwalters@0
|
48
|
tomwalters@0
|
49
|
tomwalters@0
|
50
|
tomwalters@0
|
51
|
tomwalters@0
|
52 /* Function declarations */
|
tomwalters@0
|
53 /*-------------------------------------------------------------------------*/
|
tomwalters@0
|
54
|
tomwalters@0
|
55 void parsecommandline (int argc, char *argv[], char inputfn[], char outputfn[]);
|
tomwalters@0
|
56 int readheader(FILE *inputfp);
|
tomwalters@0
|
57 void checkheader();
|
tomwalters@0
|
58 void writeheader(FILE *outputfp);
|
tomwalters@0
|
59 void changeheader(int no_frames, int new_pwidth, int new_nwidth, int framewidthsamples_output);
|
tomwalters@0
|
60 void changeheader_B(int new_mincf, int new_maxcf, int new_channels);
|
tomwalters@0
|
61
|
tomwalters@0
|
62 void writedata_output (FILE *outputfp, int samples_to_write);
|
tomwalters@0
|
63 FILE *open_file (char filefn[], FILE *dir_default, int streamtype);
|
tomwalters@0
|
64
|
tomwalters@0
|
65 int findpeaksreverse ();
|
tomwalters@0
|
66 int find_trigger(int frame, int channel, int total_peaks);
|
tomwalters@0
|
67
|
tomwalters@0
|
68
|
tomwalters@0
|
69 /* Data Arrays */
|
tomwalters@0
|
70 /* ------------------------------------------------------------------------*/
|
tomwalters@0
|
71 short inputdata[MAX_DATA];
|
tomwalters@0
|
72 short inputbychannel[MAX_CHANNELS][MAX_DATA];
|
tomwalters@0
|
73 short outputfiguredata[MAX_DATA];
|
tomwalters@0
|
74 short outputgrounddata[MAX_DATA];
|
tomwalters@0
|
75
|
tomwalters@0
|
76
|
tomwalters@0
|
77 /* other variables */
|
tomwalters@0
|
78 /*-------------------------------------------------------------------------*/
|
tomwalters@0
|
79
|
tomwalters@0
|
80 struct Peak peak[MAX_PEAKS];
|
tomwalters@0
|
81 struct NextEvent nextevent[MAX_NEXTEVENTS];
|
tomwalters@0
|
82
|
tomwalters@0
|
83
|
tomwalters@0
|
84 /* variables read from header */
|
tomwalters@0
|
85 /*-------------------------------------------------------------------------*/
|
tomwalters@0
|
86 char header[MAX_LINES_HEADER][MAX_LINE_LENGTH];
|
tomwalters@0
|
87 int header_lines;
|
tomwalters@0
|
88
|
tomwalters@0
|
89 /* .sai parameters */
|
tomwalters@0
|
90 int no_frames;
|
tomwalters@0
|
91 int frameheight; /* number of channels */
|
tomwalters@0
|
92 int framewidth_samples; /* pwidth + nwidth * samplerate */
|
tomwalters@0
|
93 int frameshift_samples; /* frstep_aid * samplerate */
|
tomwalters@0
|
94 int pwidth; /* in msecs */
|
tomwalters@0
|
95 int nwidth; /* in msecs: NEGATIVE */
|
tomwalters@0
|
96 int width_win; /* pixels */
|
tomwalters@0
|
97 int height_win; /* pixels */
|
tomwalters@0
|
98 long samplerate; /* samples per sec */
|
tomwalters@0
|
99 int mincf; /* Hz */
|
tomwalters@0
|
100 int maxcf; /* Hz */
|
tomwalters@0
|
101
|
tomwalters@0
|
102 /* parameters read from command line*/
|
tomwalters@0
|
103 int framestart;
|
tomwalters@0
|
104 int frameend;
|
tomwalters@0
|
105 int minchannel;
|
tomwalters@0
|
106 int maxchannel;
|
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 int verboseflag = OFF; /* -v */
|
tomwalters@0
|
116 int trigger_bottomflag = ON; /* -R ("lots of rotation" */
|
tomwalters@0
|
117 int triggerflag = OFF; /* -T */
|
tomwalters@0
|
118 int greyscaleflag = OFF; /* Not used: required for compilation */
|
tomwalters@0
|
119 int oppositearchflag = OFF; /* -oparch : see saigraph.c for info */
|
tomwalters@0
|
120
|
tomwalters@0
|
121 /* ................... Main ................................*/
|
tomwalters@0
|
122 /* .........................................................................*/
|
tomwalters@0
|
123 /* .........................................................................*/
|
tomwalters@0
|
124
|
tomwalters@0
|
125
|
tomwalters@0
|
126
|
tomwalters@0
|
127 void main (int argc, char *argv[])
|
tomwalters@0
|
128 {
|
tomwalters@0
|
129 int n, sample;
|
tomwalters@0
|
130 int frame, channel;
|
tomwalters@0
|
131 int trigger_peak = 0;
|
tomwalters@0
|
132 int header_bytes = 0;
|
tomwalters@0
|
133 int cut_framestart, cut_frameend, cut_no_frames;
|
tomwalters@0
|
134 int cut_minchannel, cut_maxchannel, cut_frameheight;
|
tomwalters@0
|
135 char inputfn[MAX_STRING_LENGTH], outputbasefn[MAX_STRING_LENGTH];
|
tomwalters@0
|
136 FILE *inputfp, *outputfigurefp;
|
tomwalters@0
|
137 int total_peaks;
|
tomwalters@0
|
138
|
tomwalters@0
|
139
|
tomwalters@0
|
140 /*-------------------------------------*/
|
tomwalters@0
|
141 strcpy(progname, argv[0]);
|
tomwalters@0
|
142 strcpy(inputfn, "");
|
tomwalters@0
|
143 strcpy(outputbasefn, "");
|
tomwalters@0
|
144 strcpy(outputfigurefn, "");
|
tomwalters@0
|
145
|
tomwalters@0
|
146 parsecommandline(argc, argv, inputfn, outputbasefn);
|
tomwalters@0
|
147
|
tomwalters@0
|
148 if (strcmp(outputbasefn, "") == 0)
|
tomwalters@0
|
149 strcpy(outputfigurefn, "");
|
tomwalters@0
|
150 else {
|
tomwalters@0
|
151 strcpy(outputfigurefn, outputbasefn);
|
tomwalters@0
|
152 /* strcat(outputfigurefn, OUTPUT_EXT);*/
|
tomwalters@0
|
153 }
|
tomwalters@0
|
154
|
tomwalters@0
|
155 /*--------------------------------------*/
|
tomwalters@0
|
156 /* open files, read and check header */
|
tomwalters@0
|
157 inputfp = open_file(inputfn, stdin, READ);
|
tomwalters@0
|
158 outputfigurefp = open_file(outputfigurefn, stdout, WRITE);
|
tomwalters@0
|
159 header_bytes = readheader(inputfp);
|
tomwalters@0
|
160 checkheader();
|
tomwalters@0
|
161
|
tomwalters@0
|
162 /*--------------------------------------*/
|
tomwalters@0
|
163
|
tomwalters@0
|
164 /* reset the frame/channel counters
|
tomwalters@0
|
165 * framewidth -> frameheight
|
tomwalters@0
|
166 * framehieght -> framewidth
|
tomwalters@0
|
167 * (it seems that onlr framewidth has an affect on 'gensai -use':
|
tomwalters@0
|
168 * pwidth/nwidth, nor mincf/maxcf don't do anything)
|
tomwalters@0
|
169 */
|
tomwalters@0
|
170
|
tomwalters@0
|
171 changeheader(no_frames, pwidth, nwidth, frameheight);
|
tomwalters@0
|
172 changeheader_B(mincf, maxcf, framewidth_samples);
|
tomwalters@0
|
173
|
tomwalters@0
|
174 writeheader(outputfigurefp);
|
tomwalters@0
|
175
|
tomwalters@0
|
176 /*--------------------------------------*/
|
tomwalters@0
|
177
|
tomwalters@0
|
178 if (verboseflag==ON) {
|
tomwalters@0
|
179 fprintf(stderr, " frames ");
|
tomwalters@0
|
180 fflush(stderr);}
|
tomwalters@0
|
181
|
tomwalters@0
|
182 /*------------------------------------------------------------------------*/
|
tomwalters@0
|
183
|
tomwalters@0
|
184 for (frame=1; frame<=no_frames; frame++) {
|
tomwalters@0
|
185 if (verboseflag==ON) {
|
tomwalters@0
|
186 fprintf(stderr, " %i ", frame); fflush(stderr);}
|
tomwalters@0
|
187
|
tomwalters@0
|
188 /*--------------------------------------*/
|
tomwalters@0
|
189
|
tomwalters@0
|
190 for (channel=1; channel <=frameheight; channel ++) {
|
tomwalters@0
|
191
|
tomwalters@0
|
192 for(n=0; n < MAX_PEAKS; n++) {
|
tomwalters@0
|
193 peak[n].tent = UNSET;
|
tomwalters@0
|
194 nextevent[n].type = UNSET;}
|
tomwalters@0
|
195
|
tomwalters@0
|
196 for(sample=0; sample<framewidth_samples; sample++)
|
tomwalters@0
|
197 inputdata[sample] = 0;
|
tomwalters@0
|
198
|
tomwalters@0
|
199 fread (inputdata, 2, framewidth_samples, inputfp);
|
tomwalters@0
|
200
|
tomwalters@0
|
201 /* input check */
|
tomwalters@0
|
202 for(sample=0; sample<framewidth_samples; sample++)
|
tomwalters@0
|
203 if (inputdata[sample] < 0 ) {
|
tomwalters@0
|
204 fprintf(stderr, "%s: something's gone wrong: the data is negative.\n", progname);
|
tomwalters@0
|
205 exit(-1); }
|
tomwalters@0
|
206
|
tomwalters@0
|
207 if (triggerflag == ON) {
|
tomwalters@0
|
208 /* find peaks */
|
tomwalters@0
|
209 total_peaks = findpeaksreverse();
|
tomwalters@0
|
210 /* find the trigger peak */
|
tomwalters@0
|
211 trigger_peak = 0;
|
tomwalters@0
|
212 trigger_peak = find_trigger(frame, channel, total_peaks);
|
tomwalters@0
|
213 /* from its rightwards nextevent onwards, set everything to zero.*/
|
tomwalters@0
|
214 for (sample = nextevent[trigger_peak-1].sample; sample<framewidth_samples; sample++)
|
tomwalters@0
|
215 inputdata[sample] = 0;
|
tomwalters@0
|
216 }
|
tomwalters@0
|
217
|
tomwalters@0
|
218 for(sample=0; sample<framewidth_samples; sample++)
|
tomwalters@0
|
219 inputbychannel[channel][sample] = inputdata[sample];
|
tomwalters@0
|
220
|
tomwalters@0
|
221 } /* (loading of) channel */
|
tomwalters@0
|
222
|
tomwalters@0
|
223 /*--------------------------------------*/
|
tomwalters@0
|
224
|
tomwalters@0
|
225 /* Write output */
|
tomwalters@0
|
226 if (trigger_bottomflag == ON ) {
|
tomwalters@0
|
227 /* write this frame, rotated through 90 degrees
|
tomwalters@0
|
228 * Trigger goes at top */
|
tomwalters@0
|
229 for(sample= framewidth_samples-1; sample >= 0; sample--) {
|
tomwalters@0
|
230 for(channel = 1; channel <= frameheight; channel++)
|
tomwalters@0
|
231 outputfiguredata[(channel-1)] = inputbychannel[channel][sample];
|
tomwalters@0
|
232 writedata_output(outputfigurefp, frameheight); } }
|
tomwalters@0
|
233 else {
|
tomwalters@0
|
234 /* write this frame, rotated through 270 degrees (but with a mirror on
|
tomwalters@0
|
235 * centre freq as well)
|
tomwalters@0
|
236 * Trigger goes at bottom */
|
tomwalters@0
|
237 for(sample=0; sample<framewidth_samples; sample++) {
|
tomwalters@0
|
238 for(channel = 1; channel <= frameheight; channel++)
|
tomwalters@0
|
239 outputfiguredata[(channel-1)] = inputbychannel[channel][sample];
|
tomwalters@0
|
240 writedata_output(outputfigurefp, frameheight); }
|
tomwalters@0
|
241 }
|
tomwalters@0
|
242
|
tomwalters@0
|
243
|
tomwalters@0
|
244 /*--------------------------------------*/
|
tomwalters@0
|
245
|
tomwalters@0
|
246 } /* frame */
|
tomwalters@0
|
247
|
tomwalters@0
|
248 /*----------------------------------------------------------------------*/
|
tomwalters@0
|
249
|
tomwalters@0
|
250 /* Tidy up and exit */
|
tomwalters@0
|
251 fprintf(stderr, "\n");
|
tomwalters@0
|
252 fclose(inputfp); fclose(outputfigurefp);
|
tomwalters@0
|
253
|
tomwalters@0
|
254 if ( ferror(inputfp) != 0) {
|
tomwalters@0
|
255 fprintf(stderr, " %s : error closing input file.\n", progname);
|
tomwalters@0
|
256 exit(-1);}
|
tomwalters@0
|
257
|
tomwalters@0
|
258 if ( ferror(outputfigurefp) != 0) {
|
tomwalters@0
|
259 fprintf(stderr, " %s : error closing figure file.\n", progname);
|
tomwalters@0
|
260 exit(-1);}
|
tomwalters@0
|
261
|
tomwalters@0
|
262 exit(0);
|
tomwalters@0
|
263
|
tomwalters@0
|
264 } /* Main */
|
tomwalters@0
|
265
|
tomwalters@0
|
266
|
tomwalters@0
|
267
|
tomwalters@0
|
268
|
tomwalters@0
|
269 /*......................................................................*/
|
tomwalters@0
|
270 /*......................................................................*/
|
tomwalters@0
|
271
|
tomwalters@0
|
272
|
tomwalters@0
|
273
|
tomwalters@0
|
274
|
tomwalters@0
|
275
|
tomwalters@0
|
276 void parsecommandline(int argc, char *argv[], char inputfn[], char outputbasefn[])
|
tomwalters@0
|
277 {
|
tomwalters@0
|
278 int x=1, helpflag = OFF;
|
tomwalters@0
|
279
|
tomwalters@0
|
280 while (x < argc){
|
tomwalters@0
|
281 if (!strcmp(argv[x], "-o")) {strcpy(outputbasefn, argv[x+1]); x+=2;}
|
tomwalters@0
|
282 else if (!strcmp(argv[x], "-output")) {strcpy(outputbasefn, argv[x+1]); x+=2;}
|
tomwalters@0
|
283 else if (!strcmp(argv[x], "-i")) {strcpy(inputfn, argv[x+1]); x+=2;}
|
tomwalters@0
|
284 else if (!strcmp(argv[x], "-input")) {strcpy(inputfn, argv[x+1]); x+=2;}
|
tomwalters@0
|
285 else if (!strcmp(argv[x], "-help")) {helpflag = ON; x+=1;}
|
tomwalters@0
|
286 else if (!strcmp(argv[x], "-h")) {helpflag = ON; x+=1;}
|
tomwalters@0
|
287 else if (!strcmp(argv[x], "-verbose")) {verboseflag = ON; x+=1;}
|
tomwalters@0
|
288 else if (!strcmp(argv[x], "-v")) {verboseflag = ON; x+=1;}
|
tomwalters@0
|
289 else if (!strcmp(argv[x], "-T")) {triggerflag = ON; x+=1;}
|
tomwalters@0
|
290 else if (!strcmp(argv[x], "-oparch")) {oppositearchflag = ON; x+=1;}
|
tomwalters@0
|
291 else if (!strcmp(argv[x], "-R")) {trigger_bottomflag = OFF; x+=1;}
|
tomwalters@0
|
292 else {fprintf(stderr, "%s: unknown option %s\n", progname, argv[x]);
|
tomwalters@0
|
293 exit(-1);}
|
tomwalters@0
|
294 }
|
tomwalters@0
|
295
|
tomwalters@0
|
296 if (helpflag == ON)
|
tomwalters@0
|
297 {
|
tomwalters@0
|
298 fprintf(stderr, "\n");
|
tomwalters@0
|
299 fprintf(stderr, " %s\n", progname);
|
tomwalters@0
|
300 fprintf(stderr, " -----------------------------------------------------------\n");
|
tomwalters@0
|
301 fprintf(stderr, " Rotates / reflects a .sai. Default is start at nwidth=max,\n");
|
tomwalters@0
|
302 fprintf(stderr, " and put the trigger at the bottom (90 deg rotation)\n");
|
tomwalters@0
|
303 fprintf(stderr, "\n");
|
tomwalters@0
|
304 fprintf(stderr, " The -oparch option is REQUIRED if reading a Sun .sai files \n");
|
tomwalters@0
|
305 fprintf(stderr, " on a DEC cpu or vice-versa.\n");
|
tomwalters@0
|
306 fprintf(stderr, " Add .sai to both input and output filenames.\n");
|
tomwalters@0
|
307 fprintf(stderr, " -----------------------------------------------------------\n");
|
tomwalters@0
|
308 fprintf(stderr, " -i <.sai file> input file default = stdin\n");
|
tomwalters@0
|
309 fprintf(stderr, " -o <.sai file> output file default = stdout\n");
|
tomwalters@0
|
310 fprintf(stderr, " -oparch assume input was generated on an opposing architecture cpu\n");
|
tomwalters@0
|
311 fprintf(stderr, "\n");
|
tomwalters@0
|
312 fprintf(stderr, " -T start at the TriggerPeak\n");
|
tomwalters@0
|
313 fprintf(stderr, " -R put Trigger at top (270 deg)\n");
|
tomwalters@0
|
314 fprintf(stderr, "\n");
|
tomwalters@0
|
315 fprintf(stderr, " -v verbose output (to stderr)\n");
|
tomwalters@0
|
316 fprintf(stderr, "\n\n" );
|
tomwalters@0
|
317 exit(-1);}
|
tomwalters@0
|
318
|
tomwalters@0
|
319 }
|
tomwalters@0
|
320
|
tomwalters@0
|
321
|
tomwalters@0
|
322
|
tomwalters@0
|
323
|
tomwalters@0
|
324
|
tomwalters@0
|
325
|
tomwalters@0
|
326
|