comparison tools/integframe.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 integrate.c Integrate (ie average) SAI frames over time or frequency.
3 -----------
4
5 Sequence of frames must have an SAI header.
6 The whole of every frame read in is integrated (in either time or freq).
7 Use edframe to set integration limits and select frames.
8 The output is a sequence of arrays without a header, one per input frame,
9 each of length `framewidth'.
10 If `average=on' the output is one array of length `framewidth' which is
11 the average of the sequence of arrays.
12
13 Example:
14
15 1. Integrate the 2nd SAI frame over time from 10ms to 20ms.
16 gensai output=stdout ... | select frame=2 time=10ms-20ms | integrate var=time > ...
17
18 2. Integrate the 2nd SAI frame over frequency from channels 5 to 8 inclusive.
19 gensai output=stdout ... | select frame=2 freq=5-8 | integrate var=time > ...
20
21 */
22
23 #include <stdio.h>
24 #include <math.h>
25 #include "header.h"
26 #include "options.h"
27 #include "strmatch.h"
28
29
30 char applic[] = "Integrate SAI frames over time or frequency. " ;
31
32 static char *helpstr, *debugstr, *varstr, *avstr, *typestr, *infostr ;
33
34 static Options option[] = {
35 { "help" , "off" , &helpstr , "help" , DEBUG },
36 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
37 { "variable" , "time" , &varstr , "variable of integration (time or frequency)" , VAL },
38 { "average" , "off" , &avstr , "average all integrated frames" , VAL },
39 { "type" , "short" , &typestr , "output data type" , VAL },
40 { "info" , "off" , &infostr , "print frame size info" , VAL },
41 ( char * ) 0 } ;
42
43
44 int frameheight, framewidth ; /* Parameters read from header */
45 int framebytes ;
46 int average ;
47 int type ;
48
49
50 main(argc, argv)
51 int argc ;
52 char *argv[] ;
53 {
54 FILE *fp ;
55 char *header, *SaiHeader();
56 short *frame ;
57 float *buf, *avbuf ;
58 int i, j, k, n ;
59
60 fp = openopts( option,argc,argv ) ;
61 if ( !isoff( helpstr ) )
62 helpopts( helpstr, argv[0], applic, option ) ;
63
64 average = ison( avstr ) ;
65 if ( ( type = typeindex( typestr ) ) < 0 ) {
66 fprintf( stderr, "integframe: bad type [%s]\n", typestr ) ;
67 exit( 1 ) ;
68 }
69
70 if ( (header = ReadHeader(fp)) == (char *) 0 ) {
71 fprintf(stderr,"integrate: header not found\n");
72 exit(1);
73 }
74
75 frameheight = HeaderInt( header, "frameheight" );
76 framewidth = HeaderInt( header, "framewidth" );
77 framebytes = HeaderInt( header, "framebytes" );
78
79 if ( ison( infostr ) )
80 fprintf( stderr,"frameheight=%d framewidth=%d\n", frameheight, framewidth ) ;
81
82 /* Allocate space for framebytes of data */
83
84 if ( (frame = (short *)malloc( framebytes )) == (short *)0 ) {
85 fprintf(stderr,"integrate: malloc out of space\n");
86 exit(1);
87 }
88
89 if ( iststr( varstr, "time" ) ) {
90 if ( (buf = (float *)malloc( frameheight * sizeof( float ) )) == (float *)0 ) {
91 fprintf(stderr,"integrate: malloc out of space\n");
92 exit(1);
93 }
94 if ( average ) {
95 if ( (avbuf = (float *)malloc( frameheight * sizeof( float ) )) == (float *)0 ) {
96 fprintf(stderr,"integrate: malloc out of space\n");
97 exit(1);
98 }
99 for ( i=0 ; i<frameheight ; i++ )
100 avbuf[i] = 0 ;
101 }
102 for ( n = 0 ; fread( frame,framebytes,1,fp ) ; n++ ) {
103 for ( i=0, j=0 ; i<frameheight ; i++, j+=framewidth )
104 buf[i] = frame[j] ;
105 for ( k=1 ; k<framewidth ; k++)
106 for ( i=0, j=k ; i<frameheight ; i++, j+=framewidth )
107 buf[i] += frame[j] ;
108 if ( average )
109 for ( i=0 ; i<frameheight ; i++ )
110 avbuf[i] += ( buf[i] / framewidth ) ;
111 else {
112 for ( i=0 ; i<frameheight ; i++ ) {
113 buf[i] = buf[i] / framewidth ;
114 writeitem( &buf[i], type, 1, stdout ) ;
115 }
116 }
117 }
118 if ( average && n > 0 ) {
119 for ( i=0 ; i<frameheight ; i++ ) {
120 avbuf[i] = avbuf[i] / n ;
121 writeitem( &avbuf[i], type, 1, stdout ) ;
122 }
123 }
124 }
125
126 else if ( iststr( varstr, "frequency" ) ) {
127 if ( (buf = (float *)malloc( framewidth * sizeof( float ) )) == (float *)0 ) {
128 fprintf(stderr,"integrate: malloc out of space\n");
129 exit(1);
130 }
131 if ( average ) {
132 if ( (avbuf = (float *)malloc( framewidth * sizeof( float ) )) == (float *)0 ) {
133 fprintf(stderr,"integrate: malloc out of space\n");
134 exit(1);
135 }
136 for ( i=0 ; i<framewidth ; i++ )
137 avbuf[i] = 0 ;
138 }
139 for ( n = 0 ; fread( frame,framebytes,1,fp ) ; n++ ) {
140 for ( i=0 ; i<framewidth ; i++ )
141 buf[i] = frame[i] ;
142 for ( k=1 ; k<frameheight ; k++)
143 for ( i=0, j=k*framewidth ; i<framewidth ; i++, j++ )
144 buf[i] += frame[j] ;
145 if ( average ) {
146 for ( i=0 ; i<framewidth ; i++ )
147 avbuf[i] += ( buf[i] / frameheight ) ;
148 }
149 else {
150 for ( i=0 ; i<framewidth ; i++ ) {
151 buf[i] = buf[i] / frameheight ;
152 writeitem( &buf[i], type, 1, stdout ) ;
153 }
154 }
155 }
156 if ( average && n > 0 ) {
157 for ( i=0 ; i<framewidth ; i++ ) {
158 avbuf[i] = avbuf[i] / n ;
159 writeitem( &avbuf[i], type, 1, stdout ) ;
160 }
161 }
162 }
163
164 else {
165 fprintf( stderr,"unknown variable of integration [%s]\n", varstr ) ;
166 exit( 1 ) ;
167 }
168
169 if ( ison( infostr ) )
170 fprintf( stderr,"frames=%d\n", n ) ;
171
172 fclose( fp ) ;
173 }
174
175