comparison tools/gate.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 gate.c gate numbers from input stream
3 ------
4 Read and write numbers of a given type.
5 All numbers within the given gate ranges are replaced by the
6 given gate <value>, (a real ie. float number).
7 Gate ranges are: xrange (a time range, with optional time units)
8 and: yrange (an amplitude range, in real ie. float numbers).
9 If the gate is not a <value>, but is an <operation>, then the numbers
10 within the gate ranges are operated on as appropriate.
11 Abbreviated forms of the operation names are allowed.
12
13 <operation> comment
14 ----------- -----------------------------
15 exclude exclude numbers from output
16 negate negate numbers
17 count print a count of the numbers found in the gate range
18 on the stderr.
19
20 Examples:
21
22 1. Replace all numbers <=0 by value 0 (ie. half-wave rectification).
23
24 gate yrange=min-0 val=0 file
25
26 2. Replace all numbers <=0 by their inverse (ie. full-wave rectification).
27
28 gate yrange=min-0 val=neg file
29
30 3. Gate the onset of a signal: replace the first 20ms with zeroes.
31
32 gate xrange=0-20ms yrange=min-max val=0 file
33
34 4. Exclude all numbers <=0
35
36 gate yrange=min-0 val=exclude file
37
38 5. Exclude all numbers >0
39
40 gate yrange=1-max val=exclude file
41
42 6. Delete lines 4 to 8 inclusive from ascii input (lines numbered 0,1,2,...)
43
44 gate type=ASCII xrange=4-8 yrange=min-max val=exclude file
45
46 7. Replace all numbers in the yrange -1 to +1 inclusive by 0
47
48 gate yrange=-1-1 val=0 file
49
50 8. Replace all instances of number 10 by -10
51
52 gate yrange=10 val=-10 file
53
54 9. Print a count of all numbers = 0
55
56 gate yrange=0 val=count file
57
58 10. Print a count of all numbers < 0
59
60 gate yrange=min--1 val=count file
61
62 */
63
64
65 #include <stdio.h>
66 #include <math.h>
67 #include "options.h"
68 #include "units.h"
69 #include "strmatch.h"
70
71 char applic[] = "gate specific numbers in input stream using given value." ;
72
73 static char *helpstr, *debugstr, *sampstr, *xrstr, *yrstr, *gstr, *typestr ;
74
75 static Options option[] = {
76 { "help" , "off" , &helpstr , "help" , DEBUG },
77 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
78 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL },
79 { "xrange" , "min-max" , &xrstr , "time range" , VAL },
80 { "yrange" , "min-0" , &yrstr , "amplitude range" , VAL },
81 { "value" , "0" , &gstr , "replacement value or <operation>", VAL },
82 { "type" , "short" , &typestr , "datatype" , VAL },
83 ( char * ) 0 } ;
84
85
86 int samplerate ;
87 int type ; /* datatype index */
88
89 main(argc, argv)
90 int argc;
91 char **argv;
92 {
93 FILE *fp ;
94 int i, n=0, ax, bx, real_range(), helpoperations() ;
95 float ay, by, x, val ;
96
97 fp = openopts( option, argc, argv ) ;
98 if ( !isoff( helpstr ) )
99 helpopts1( helpstr, argv[0], applic, option, helpoperations ) ;
100
101 samplerate = to_Hz( sampstr, 0 ) ;
102
103 if ( ( type = typeindex( typestr ) ) < 0 ) {
104 fprintf( stderr, "gate: bad type [%s]\n", typestr ) ;
105 exit( 1 ) ;
106 }
107
108 if ( range( xrstr, &ax, &bx, samplerate ) == 0 ) {
109 fprintf(stderr,"gate: bad xrange [%s]\n", xrstr ) ;
110 exit( 1 ) ;
111 }
112
113 if ( real_range( yrstr, &ay, &by ) == 0 ) {
114 fprintf(stderr,"gate: bad yrange [%s]\n", yrstr ) ;
115 exit( 1 ) ;
116 }
117
118 if ( iststr( gstr, "count" ) ) { /* gate=count */
119
120 for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ )
121 if ( i >= ax && ( i <= bx || bx == (-1) ) )
122 if ( x >= ay && x <= by )
123 n++ ;
124 fprintf(stderr,"%d numbers in gate\n", n);
125 }
126
127
128 else if ( iststr( gstr, "exclude" ) ) { /* gate=exclude */
129
130 for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ ) {
131 if ( i < ax || ( i > bx && bx != (-1) ) )
132 writeitem( &x, type, 1, stdout ) ;
133 else if ( x < ay || x > by )
134 writeitem( &x, type, 1, stdout ) ;
135 }
136 }
137
138 else if ( iststr( gstr, "negate" ) ) { /* gate=negate */
139
140 for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ ) {
141 if ( i < ax || ( i > bx && bx != (-1) ) )
142 writeitem( &x, type, 1, stdout ) ;
143 else if ( x < ay || x > by )
144 writeitem( &x, type, 1, stdout ) ;
145 else {
146 x = ( -x ) ;
147 writeitem( &x, type, 1, stdout ) ;
148 }
149 }
150 }
151
152 else { /* gate=<value> */
153 val = atof( gstr ) ;
154 for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ ) {
155 if ( i < ax || ( i > bx && bx != (-1) ) )
156 writeitem( &x, type, 1, stdout ) ;
157 else if ( x < ay || x > by )
158 writeitem( &x, type, 1, stdout ) ;
159 else
160 writeitem( &val, type, 1, stdout ) ;
161 }
162 }
163
164 fclose(fp);
165 }
166
167
168 /*
169 Parse a range specifier into real numbers representing amplitude.
170 */
171
172 int real_range( s, a, b )
173 char *s ;
174 float *a, *b ;
175 {
176 char *val1, *val2 ;
177
178 if ( getvals( s, &val1, &val2, "-" ) == (-3) )
179 return ( 0 ) ;
180 if ( ismin( val1 ) ) *a = (-1e10) ; /* minimum */
181 else if ( ismax( val1 ) ) *a = 1e10 ; /* maximum */
182 else *a = atof( val1 ) ;
183
184 if ( isempty( val2 ) ) *b = *a ; /* single object */
185 else if ( ismin( val2 ) ) *b = (-1e10) ;
186 else if ( ismax( val2 ) ) *b = 1e10 ;
187 else *b = atof( val2 ) ;
188
189 if ( *a > *b ) return 0 ;
190 return 1 ;
191 }
192
193
194 helpoperations()
195 {
196 fprintf(stderr,"\n<operation>: \n");
197 fprintf(stderr," exclude exclude numbers in gate range from output \n");
198 fprintf(stderr," negate negate numbers in gate range \n");
199 fprintf(stderr," count print count of numbers in gate range \n");
200 exit( 1 ) ;
201 }