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