annotate tools/chi.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
rev   line source
tomwalters@0 1 /*
tomwalters@0 2 chi.c repetitative chi-squared curve.
tomwalters@0 3
tomwalters@0 4 Examples:
tomwalters@0 5
tomwalters@0 6 chi amp=500 asym=1 per=4ms | x11plot
tomwalters@0 7 chi amp=500 asym=1 per=16ms | x11plot -n512
tomwalters@0 8 chi amp=500 asym=4 per=16ms | x11plot -n512
tomwalters@0 9
tomwalters@0 10 */
tomwalters@0 11
tomwalters@0 12 #include <stdio.h>
tomwalters@0 13 #include <math.h>
tomwalters@0 14 #include "options.h"
tomwalters@0 15 #include "units.h"
tomwalters@0 16 #include "strmatch.h"
tomwalters@0 17
tomwalters@0 18 char applic[] = "generate a repeated chi-squared waveform. " ;
tomwalters@0 19 char usage[] = "chi [options]" ;
tomwalters@0 20
tomwalters@0 21 static char *helpstr, *debugstr, *sampstr, *perstr, *astr, *asymstr, *dstr, *datastr ;
tomwalters@0 22
tomwalters@0 23 static Options option[] = {
tomwalters@0 24 { "help" , "off" , &helpstr , "help" , DEBUG },
tomwalters@0 25 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
tomwalters@0 26 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL },
tomwalters@0 27 { "period" , "8ms" , &perstr , "period [s,ms,p] of repetition" , VAL },
tomwalters@0 28 { "amplitude" , "1024" , &astr , "amplitude of waveform" , VAL },
tomwalters@0 29 { "asymmetry" , "1.0" , &asymstr , "parameter controlling assymmetry (>=1)" , VAL },
tomwalters@0 30 { "duration" , "500ms" , &dstr , "duration of waveform" , VAL },
tomwalters@0 31 { "datatype" , "short" , &datastr , "o/p datatype (short/float)", VAL },
tomwalters@0 32 ( char * ) 0 } ;
tomwalters@0 33
tomwalters@0 34
tomwalters@0 35 #define e ( 2.718 )
tomwalters@0 36
tomwalters@0 37 int samplerate ;
tomwalters@0 38 int amplitude ;
tomwalters@0 39 int duration ;
tomwalters@0 40 int period ;
tomwalters@0 41 float a ;
tomwalters@0 42
tomwalters@0 43 main(argc, argv)
tomwalters@0 44 int argc ;
tomwalters@0 45 char *argv[] ;
tomwalters@0 46 {
tomwalters@0 47 int i, n, t ;
tomwalters@0 48 short s ;
tomwalters@0 49 float f ;
tomwalters@0 50 float b ;
tomwalters@0 51 float r = 4 ;
tomwalters@0 52
tomwalters@0 53 getopts( option,argc,argv ) ;
tomwalters@0 54 if ( !isoff( helpstr ) )
tomwalters@0 55 helpopts3( helpstr, argv[0], applic, usage, option ) ;
tomwalters@0 56
tomwalters@0 57 samplerate = to_Hz( sampstr, 0 ) ;
tomwalters@0 58 amplitude = atoi( astr ) ;
tomwalters@0 59 period = (int)to_p( perstr, samplerate ) ;
tomwalters@0 60 a = atof( asymstr ) ;
tomwalters@0 61 duration = to_p( dstr, samplerate ) ;
tomwalters@0 62
tomwalters@0 63 b = ( 2. * a ) / period ; /* sets mode at half-way to period */
tomwalters@0 64
tomwalters@0 65 if ( iststr( datastr, "short" ) )
tomwalters@0 66 for ( i=0 ; i<duration ; )
tomwalters@0 67 for ( t=0 ; t<period*r && i<duration ; t+=r, i++ ) {
tomwalters@0 68 s = amplitude * pow( (double)( ( e * b * t ) / a ), (double)a ) * exp( -(double)( b * t ) ) ;
tomwalters@0 69 fwrite( &s, sizeof(short), 1, stdout ) ;
tomwalters@0 70 }
tomwalters@0 71 else if ( iststr( datastr, "float" ) )
tomwalters@0 72 for ( i=0 ; i<duration ; )
tomwalters@0 73 for ( t=0 ; t<period && i<duration ; t++, i++ ) {
tomwalters@0 74 f = amplitude * pow( (double)( ( e * b * t ) / a ), (double)a ) * exp( -(double)( b * t ) ) ;
tomwalters@0 75 fwrite( &f, sizeof(float), 1, stdout ) ;
tomwalters@0 76 }
tomwalters@0 77 else
tomwalters@0 78 fprintf(stderr,"unknown datatype [%s]\n", datastr) ;
tomwalters@0 79 }