tomwalters@0: /* tomwalters@0: ptrain.c generate a pulse train of binary shorts. tomwalters@0: -------- tomwalters@0: Generate samples of a pulse train at a given sample rate. tomwalters@0: Specify pulse amplitude and width (in s, ms, or p (sample points) ), tomwalters@0: and also the period of the waveform (in s, ms, or p (sample points) ). tomwalters@0: The pulse amplitude is the pulse height above the zero level, which is tomwalters@0: set by the offset option. The offset is "0" by default so that pulses tomwalters@0: of any given amplitude are non-negative by default. tomwalters@0: tomwalters@0: Output samples in binary shorts for the given waveform duration. tomwalters@0: tomwalters@0: Non-fixed period pulse trains: tomwalters@0: If a filename is given, then the period of the output pulse train is tomwalters@0: taken from the file. Binary shorts read from the file are subsequent tomwalters@0: periods in samples. Periods are output until the file is empty. tomwalters@0: tomwalters@0: tomwalters@0: Examples: tomwalters@0: tomwalters@0: 1. A pulse train with period 100 samples and pulse width 1 sample: tomwalters@0: tomwalters@0: ptrain period=100p width=1p tomwalters@0: tomwalters@0: 2. A pulse train with period 8ms at 20KHz and pulse width 4 samples tomwalters@0: tomwalters@0: ptrain -p8ms -s20000 -w4 tomwalters@0: tomwalters@0: 3. A square wave (pulse width = half period) of period 4ms, sampled at 10kHz tomwalters@0: tomwalters@0: ptrain period=4ms width=2ms samplerate=10kHz tomwalters@0: tomwalters@0: 3. A square wave of period 4ms with zero mean value tomwalters@0: tomwalters@0: ptrain period=4ms width=2ms offset=0 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[] = "generate a pulse train of binary shorts. " ; tomwalters@0: char usage[] = "ptrain [options]" ; tomwalters@0: tomwalters@0: static char *helpstr, *debugstr, *sampstr, *pstr, *astr, *ostr, *wstr, *dstr, *phstr ; 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: { "duration" , "500ms" , &dstr , "duration of waveform" , VAL }, tomwalters@0: { "period" , "8ms" , &pstr , "period of pulse train" , VAL }, tomwalters@0: { "amplitude" , "1024" , &astr , "pulse amplitude" , VAL }, tomwalters@0: { "width" , "4p" , &wstr , "pulse width" , VAL }, tomwalters@0: { "offset" , "0" , &ostr , "dc offset" , VAL }, tomwalters@0: { "phase" , "0" , &phstr , "phase offset" , VAL }, tomwalters@0: ( char * ) 0 } ; tomwalters@0: tomwalters@0: tomwalters@0: main(argc, argv) tomwalters@0: int argc ; tomwalters@0: char *argv[] ; tomwalters@0: { tomwalters@0: FILE *fp, *fopen() ; tomwalters@0: short a, z = 0; /* Zero */ tomwalters@0: int i, j, samplerate ; tomwalters@0: int T, w, n, phase ; tomwalters@0: tomwalters@0: i = getopts( option,argc,argv ) ; tomwalters@0: if ( !isoff( helpstr ) ) tomwalters@0: helpopts3( helpstr, argv[0], applic, usage, option ) ; tomwalters@0: tomwalters@0: samplerate = to_Hz( sampstr ) ; tomwalters@0: T = (int)to_p( pstr, samplerate ) ; /* period */ tomwalters@0: w = (int)to_p( wstr, samplerate ) ; /* width */ tomwalters@0: n = to_p( dstr, samplerate ) ; /* duration in points */ tomwalters@0: tomwalters@0: z = atoi( ostr ) ; /* zero level set to the dc offset (default 0) */ tomwalters@0: a = atoi( astr ) + z ; /* amplitude (pulse height above zero level) */ tomwalters@0: tomwalters@0: tomwalters@0: if ( ( phase = (int)to_p( pstr, samplerate ) ) > 0 ) { /* initial phase offset */ tomwalters@0: for (j=0 ; j