tomwalters@0: /* tomwalters@0: gate.c gate numbers from input stream tomwalters@0: ------ tomwalters@0: Read and write numbers of a given type. tomwalters@0: All numbers within the given gate ranges are replaced by the tomwalters@0: given gate , (a real ie. float number). tomwalters@0: Gate ranges are: xrange (a time range, with optional time units) tomwalters@0: and: yrange (an amplitude range, in real ie. float numbers). tomwalters@0: If the gate is not a , but is an , then the numbers tomwalters@0: within the gate ranges are operated on as appropriate. tomwalters@0: Abbreviated forms of the operation names are allowed. tomwalters@0: tomwalters@0: comment tomwalters@0: ----------- ----------------------------- tomwalters@0: exclude exclude numbers from output tomwalters@0: negate negate numbers tomwalters@0: count print a count of the numbers found in the gate range tomwalters@0: on the stderr. tomwalters@0: tomwalters@0: Examples: tomwalters@0: tomwalters@0: 1. Replace all numbers <=0 by value 0 (ie. half-wave rectification). tomwalters@0: tomwalters@0: gate yrange=min-0 val=0 file tomwalters@0: tomwalters@0: 2. Replace all numbers <=0 by their inverse (ie. full-wave rectification). tomwalters@0: tomwalters@0: gate yrange=min-0 val=neg file tomwalters@0: tomwalters@0: 3. Gate the onset of a signal: replace the first 20ms with zeroes. tomwalters@0: tomwalters@0: gate xrange=0-20ms yrange=min-max val=0 file tomwalters@0: tomwalters@0: 4. Exclude all numbers <=0 tomwalters@0: tomwalters@0: gate yrange=min-0 val=exclude file tomwalters@0: tomwalters@0: 5. Exclude all numbers >0 tomwalters@0: tomwalters@0: gate yrange=1-max val=exclude file tomwalters@0: tomwalters@0: 6. Delete lines 4 to 8 inclusive from ascii input (lines numbered 0,1,2,...) tomwalters@0: tomwalters@0: gate type=ASCII xrange=4-8 yrange=min-max val=exclude file tomwalters@0: tomwalters@0: 7. Replace all numbers in the yrange -1 to +1 inclusive by 0 tomwalters@0: tomwalters@0: gate yrange=-1-1 val=0 file tomwalters@0: tomwalters@0: 8. Replace all instances of number 10 by -10 tomwalters@0: tomwalters@0: gate yrange=10 val=-10 file tomwalters@0: tomwalters@0: 9. Print a count of all numbers = 0 tomwalters@0: tomwalters@0: gate yrange=0 val=count file tomwalters@0: tomwalters@0: 10. Print a count of all numbers < 0 tomwalters@0: tomwalters@0: gate yrange=min--1 val=count file tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "options.h" tomwalters@0: #include "units.h" tomwalters@0: #include "strmatch.h" tomwalters@0: tomwalters@0: char applic[] = "gate specific numbers in input stream using given value." ; tomwalters@0: tomwalters@0: static char *helpstr, *debugstr, *sampstr, *xrstr, *yrstr, *gstr, *typestr ; tomwalters@0: tomwalters@0: static Options option[] = { tomwalters@0: { "help" , "off" , &helpstr , "help" , DEBUG }, tomwalters@0: { "debug" , "off" , &debugstr , "debugging switch" , DEBUG }, tomwalters@0: { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL }, tomwalters@0: { "xrange" , "min-max" , &xrstr , "time range" , VAL }, tomwalters@0: { "yrange" , "min-0" , &yrstr , "amplitude range" , VAL }, tomwalters@0: { "value" , "0" , &gstr , "replacement value or ", VAL }, tomwalters@0: { "type" , "short" , &typestr , "datatype" , VAL }, tomwalters@0: ( char * ) 0 } ; tomwalters@0: tomwalters@0: tomwalters@0: int samplerate ; tomwalters@0: int type ; /* datatype index */ tomwalters@0: tomwalters@0: main(argc, argv) tomwalters@0: int argc; tomwalters@0: char **argv; tomwalters@0: { tomwalters@0: FILE *fp ; tomwalters@0: int i, n=0, ax, bx, real_range(), helpoperations() ; tomwalters@0: float ay, by, x, val ; tomwalters@0: tomwalters@0: fp = openopts( option, argc, argv ) ; tomwalters@0: if ( !isoff( helpstr ) ) tomwalters@0: helpopts1( helpstr, argv[0], applic, option, helpoperations ) ; tomwalters@0: tomwalters@0: samplerate = to_Hz( sampstr, 0 ) ; tomwalters@0: tomwalters@0: if ( ( type = typeindex( typestr ) ) < 0 ) { tomwalters@0: fprintf( stderr, "gate: bad type [%s]\n", typestr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: if ( range( xrstr, &ax, &bx, samplerate ) == 0 ) { tomwalters@0: fprintf(stderr,"gate: bad xrange [%s]\n", xrstr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: if ( real_range( yrstr, &ay, &by ) == 0 ) { tomwalters@0: fprintf(stderr,"gate: bad yrange [%s]\n", yrstr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: if ( iststr( gstr, "count" ) ) { /* gate=count */ tomwalters@0: tomwalters@0: for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ ) tomwalters@0: if ( i >= ax && ( i <= bx || bx == (-1) ) ) tomwalters@0: if ( x >= ay && x <= by ) tomwalters@0: n++ ; tomwalters@0: fprintf(stderr,"%d numbers in gate\n", n); tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: else if ( iststr( gstr, "exclude" ) ) { /* gate=exclude */ tomwalters@0: tomwalters@0: for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ ) { tomwalters@0: if ( i < ax || ( i > bx && bx != (-1) ) ) tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: else if ( x < ay || x > by ) tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: else if ( iststr( gstr, "negate" ) ) { /* gate=negate */ tomwalters@0: tomwalters@0: for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ ) { tomwalters@0: if ( i < ax || ( i > bx && bx != (-1) ) ) tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: else if ( x < ay || x > by ) tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: else { tomwalters@0: x = ( -x ) ; tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: else { /* gate= */ tomwalters@0: val = atof( gstr ) ; tomwalters@0: for ( i = 0 ; readitem( &x, type, 1, fp ) ; i++ ) { tomwalters@0: if ( i < ax || ( i > bx && bx != (-1) ) ) tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: else if ( x < ay || x > by ) tomwalters@0: writeitem( &x, type, 1, stdout ) ; tomwalters@0: else tomwalters@0: writeitem( &val, type, 1, stdout ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: fclose(fp); tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: /* tomwalters@0: Parse a range specifier into real numbers representing amplitude. tomwalters@0: */ tomwalters@0: tomwalters@0: int real_range( s, a, b ) tomwalters@0: char *s ; tomwalters@0: float *a, *b ; tomwalters@0: { tomwalters@0: char *val1, *val2 ; tomwalters@0: tomwalters@0: if ( getvals( s, &val1, &val2, "-" ) == (-3) ) tomwalters@0: return ( 0 ) ; tomwalters@0: if ( ismin( val1 ) ) *a = (-1e10) ; /* minimum */ tomwalters@0: else if ( ismax( val1 ) ) *a = 1e10 ; /* maximum */ tomwalters@0: else *a = atof( val1 ) ; tomwalters@0: tomwalters@0: if ( isempty( val2 ) ) *b = *a ; /* single object */ tomwalters@0: else if ( ismin( val2 ) ) *b = (-1e10) ; tomwalters@0: else if ( ismax( val2 ) ) *b = 1e10 ; tomwalters@0: else *b = atof( val2 ) ; tomwalters@0: tomwalters@0: if ( *a > *b ) return 0 ; tomwalters@0: return 1 ; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: helpoperations() tomwalters@0: { tomwalters@0: fprintf(stderr,"\n: \n"); tomwalters@0: fprintf(stderr," exclude exclude numbers in gate range from output \n"); tomwalters@0: fprintf(stderr," negate negate numbers in gate range \n"); tomwalters@0: fprintf(stderr," count print count of numbers in gate range \n"); tomwalters@0: exit( 1 ) ; tomwalters@0: }