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