comparison tools/sp_weights.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 sp_weights.c Map auditory image onto spiral sector-weights contour.
3 --------------
4
5 Read auditory image frames (gensai or genspl with output set) from a
6 file or the stdin. Input must have a pipes header, (for frame dimensions).
7
8 Note, you can use either gensai or genspl output, but remember that their
9 default options are different, (particularly pwidth, nwidth, and dencf).
10
11 For each frame, (of frameheight*framewidth binary shorts),
12 write a sector-weights contour, (of m binary shorts), on the stdout.
13
14 Arguments:
15 -m The number of sectors (default SECTORS).
16 -fA-B Process the A'th to B'th frame inclusive,
17 (or the A'th to the eof if b=='e'), (default -o1-e).
18 -fA Process just the A'th frame.
19
20 *************************************************************************/
21
22 #include <stdio.h>
23 #include <math.h>
24 #include "header.h"
25 #include "options.h"
26 #include "units.h"
27 #include "strmatch.h"
28
29 char applic[] = "Map auditory image frames onto spiral sector-weights contours.\n (Input and output in binary shorts)." ;
30
31 static char *helpstr, *debugstr, *infostr, *fstr, *mstr ;
32
33 static Options option[] = {
34 { "help" , "off" , &helpstr , "help" , DEBUG },
35 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
36 { "info" , "off" , &infostr , "print frame size information", DEBUG },
37 { "frames" , "1-max" , &fstr , "select frames inclusively" , VAL },
38 { "nsectors" , "64" , &mstr , "number of sectors" , SVAL },
39 ( char * ) 0 } ;
40
41
42 #define log2(x) (log((double)x)/log(2.0))
43 #define round(x) ((int)(x+0.5))
44
45 int frameheight, framewidth ; /* Parameters read from header */
46 int frames;
47 double orig_spd ;
48
49 main(argc, argv)
50 int argc ;
51 char *argv[] ;
52 {
53 FILE *fp ;
54 char *header, *val1, *val2 ;
55 char *headeropt, *versionstr;
56 int i, a, b;
57 int m ; /* Number of sectors around spiral */
58 int framebytes; /* Number of bytes in each frame */
59 short *frame; /* Each frame */
60
61 fp = openopts( option,argc,argv ) ;
62 if ( !isoff( helpstr ) )
63 helpopts( helpstr, argv[0], applic, option ) ;
64
65 m = atoi( mstr ) ;
66
67 /* parse bounds on number of frames */
68
69 if ( getvals( fstr, &val1, &val2, "-" ) == BADVAL ) {
70 fprintf(stderr,"loudness: bad frame selector [%s]\n", fstr ) ;
71 exit( 1 ) ;
72 }
73 a = atoi( val1 ) ;
74 if ( isempty( val2 ) ) b = a ;
75 else if ( ismax( val2 ) ) b = 0 ;
76 else b = atoi( val2 ) ;
77
78 if (b<a && b>0) fprintf(stderr,"sp_weights warning: bad frame specifiers\n");
79
80
81
82 /* Read header to find dimensions of auditory image */
83
84 if ((header = ReadHeader(fp)) == (char *) 0 ) {
85 fprintf(stderr,"sp_weights: header not found\n");
86 exit(1);
87 }
88
89 if ( (versionstr = HeaderString( header, "Version" )) == (char *)0 ) {
90 fprintf(stderr,"sp_weights: model version-number not found in header\n");
91 exit(1);
92 }
93
94 if ( (headeropt = HeaderString( header, "frameheight" )) == (char *)0) {
95 fprintf(stderr,"sp_weights: frameheight not found in header\n");
96 exit(1);
97 }
98 else frameheight = atoi( headeropt );
99
100 if ( (headeropt = HeaderString( header, "framewidth" )) == (char *)0) {
101 fprintf(stderr,"sp_weights: framewidth not found in header\n");
102 exit(1);
103 }
104 else framewidth = atoi( headeropt );
105
106 if ( (headeropt = HeaderString( header, "frames" )) == (char *)0) {
107 fprintf(stderr,"sp_weights: frames not found in header\n");
108 exit(1);
109 }
110 else frames = atoi( headeropt );
111
112 if ( (headeropt = HeaderString( header, "orig_spd" )) == (char *)0) {
113 fprintf(stderr,"sp_weights: orig_spd not found in header\n");
114 exit(1);
115 }
116 else orig_spd = atof( headeropt );
117
118 if ( ison( debugstr ) ) {
119 printf("a=%d b=%d m=%d \n", a, b, m );
120 printf("frames=%d frameheight=%d framewidth=%d orig_spd=%.2f\n", frames,frameheight,framewidth,orig_spd ) ;
121 exit(0);
122 }
123
124 if ( ison( infostr ) || a==0 ) {
125 printf("sp_weights input: %s", versionstr ) ;
126 printf(" frames=%d frameheight=%d framewidth=%d\n", frames,frameheight,framewidth ) ;
127 exit(0);
128 }
129
130 if (frames<=0) {
131 if (frames==0) fprintf(stderr,"sp_weights: zero frames input\n");
132 if (frames<0) fprintf(stderr,"sp_weights: garbled number of frames, (start set too big?)\n");
133 exit(1);
134 }
135
136 if (a>frames || b>frames)
137 fprintf(stderr,"sp_weights warning: bad frame specifiers\n");
138
139 framebytes = frameheight * framewidth * sizeof(short) ;
140
141 /* Allocate space for an auditory image frame */
142
143 if (( frame = (short *)malloc( framebytes )) == NULL)
144 fprintf(stderr,"sp_weights: malloc out of space\n");
145
146 /* Seek past a-1 frames, then read the next b-a+1 frames, or all */
147 /* remaining frames if b==0. */
148
149 for (i=1 ; i<a && fread(frame,framebytes,1,fp) ; i++)
150 ;
151 if (b > 0)
152 for ( ; i<=b && fread(frame,framebytes,1,fp) ; i++)
153 write_weights(frame,m);
154 else
155 while ( fread(frame,framebytes,1,fp) )
156 write_weights(frame,m);
157
158 fclose(fp);
159 }
160
161
162
163 /***************************************************************************
164 Spoke sector weight routines (originally as src/src8/spoke.c)
165
166 Write m sector weights as binary shorts on the stdout.
167 ***************************************************************************/
168
169 write_weights(frame,m)
170 short *frame;
171 int m; /* number of sectors around the spiral plane */
172 {
173 static short *sector;
174 static int *logtime;
175 int i, k, t, col;
176 double sum;
177 static int first=1; /* flag for first-call of this routine */
178
179 /* Allocate space (first-time call only). */
180 if (first) {
181 first = 0;
182 /* Allocate space and compute base-2 log time for spoke detection */
183 if ( (sector = (short *)malloc( m*sizeof(short) ) ) == NULL)
184 fprintf(stderr,"sp_weights: malloc out of space\n");
185 if ( (logtime = (int *)malloc( framewidth*sizeof(int) ) ) == NULL )
186 fprintf(stderr,"sp_weights: malloc out of space\n");
187 gen_logtime(logtime,m);
188 }
189 /* clear sector bins */
190 for (i=0 ; i<m ; i++)
191 sector[i]=0;
192 /* for each time point along the SAI frame (with time origin on right) */
193 /* (exclude time-origin bin to prevent overweighting by this sample) */
194 for (t=round(pow(2.0,orig_spd)) ; t<framewidth ; t++) {
195 col = (framewidth-1)-t;
196 /* sum frame column (ie across all frequency channels) */
197 for (i=0, k=0, sum=0 ; i<frameheight ; i++, k+=framewidth)
198 sum += frame[col+k];
199 /* Normalize (divide by frameheight) for average magnitude */
200 sum /= frameheight;
201 /* Accumulate sum in log-time sector bin */
202 sector[logtime[t]] += sum;
203 }
204 /* write total sum activity in spokes around the spiral from the datum */
205 /* line in an anticlockwise direction. (ie in "natural order"). */
206 for (i=0 ; i<m ; i++)
207 /* printf("%d\n", sector[i]); */
208 fwrite(&sector[i], sizeof(short), 1, stdout);
209 }
210
211 /****************************************************************************
212 List the sector numbers, 0,1,...,m-1, which correspond to each time
213 t=1,2,...,framewidth-1. The return array, "logtime", maps time index onto
214 a spiral sector number.
215 (Note that since the sector width increases with radius, there is an
216 increasing number of adjacent frame indices within the sector bin).
217 ****************************************************************************/
218 gen_logtime(logtime, m)
219 int *logtime;
220 int m;
221 {
222 int t;
223 double theta, fptr, iptr;
224
225 logtime[0] = 0; /* special case for log(0) */
226 for (t=1 ; t<framewidth ; t++) {
227 theta = log2(t) - orig_spd;
228 fptr = modf(theta,&iptr); /* find fractional part of log time */
229 /* scale up to given resolution, and round */
230 logtime[t] = (int)(0.5+(fptr*m));
231 }
232 }
233
234
235