view 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
line wrap: on
line source
/*
    chi.c       repetitative chi-squared curve.

Examples:

chi amp=500 asym=1 per=4ms | x11plot
chi amp=500 asym=1 per=16ms | x11plot -n512
chi amp=500 asym=4 per=16ms | x11plot -n512

*/

#include <stdio.h>
#include <math.h>
#include "options.h"
#include "units.h"
#include "strmatch.h"

char applic[]  = "generate a repeated chi-squared waveform. " ;
char usage[]   = "chi [options]" ;

static char *helpstr, *debugstr, *sampstr, *perstr, *astr, *asymstr, *dstr, *datastr ;

static Options option[] = {
    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                     , DEBUG   },
    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"         , DEBUG   },
    {   "samplerate",   "20kHz"     ,  &sampstr     ,   "samplerate "              , VAL     },
    {   "period"    ,   "8ms"       ,  &perstr      ,   "period [s,ms,p] of repetition"  , VAL     },
    {   "amplitude" ,   "1024"      ,  &astr        ,   "amplitude of waveform"    , VAL     },
    {   "asymmetry" ,   "1.0"       ,  &asymstr     ,   "parameter controlling assymmetry (>=1)"      , VAL     },
    {   "duration"  ,   "500ms"     ,  &dstr        ,   "duration of waveform"     , VAL     },
    {   "datatype"  ,   "short"     ,  &datastr     ,   "o/p datatype (short/float)", VAL     },
   ( char * ) 0 } ;


#define e   ( 2.718 )

int     samplerate ;
int     amplitude  ;
int     duration   ;
int     period     ;
float   a          ;

main(argc, argv)
int   argc ;
char *argv[] ;
{
    int    i, n, t ;
    short  s ;
    float  f ;
    float  b ;
    float  r = 4 ;

    getopts( option,argc,argv ) ;
    if ( !isoff( helpstr ) )
	helpopts3( helpstr, argv[0], applic, usage, option ) ;

    samplerate = to_Hz( sampstr, 0 ) ;
    amplitude  = atoi( astr ) ;
    period     = (int)to_p( perstr, samplerate ) ;
    a          = atof( asymstr ) ;
    duration   = to_p( dstr, samplerate )   ;

    b = ( 2. * a ) / period ;   /* sets mode at half-way to period */

    if ( iststr( datastr, "short" ) )
	for ( i=0 ; i<duration ;  )
	    for ( t=0 ; t<period*r && i<duration ; t+=r, i++ ) {
		s = amplitude * pow( (double)( ( e * b * t ) / a ), (double)a ) * exp( -(double)( b * t ) ) ;
		fwrite( &s, sizeof(short), 1, stdout ) ;
	    }
    else if ( iststr( datastr, "float" ) )
	for ( i=0 ; i<duration ;  )
	    for ( t=0 ; t<period && i<duration ; t++, i++ ) {
		f = amplitude * pow( (double)( ( e * b * t ) / a ), (double)a ) * exp( -(double)( b * t ) ) ;
		fwrite( &f, sizeof(float), 1, stdout ) ;
	    }
    else
	fprintf(stderr,"unknown datatype [%s]\n", datastr) ;
}