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