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