Mercurial > hg > aim92
view tools/ptrain.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
/* ptrain.c generate a pulse train of binary shorts. -------- Generate samples of a pulse train at a given sample rate. Specify pulse amplitude and width (in s, ms, or p (sample points) ), and also the period of the waveform (in s, ms, or p (sample points) ). The pulse amplitude is the pulse height above the zero level, which is set by the offset option. The offset is "0" by default so that pulses of any given amplitude are non-negative by default. Output samples in binary shorts for the given waveform duration. Non-fixed period pulse trains: If a filename is given, then the period of the output pulse train is taken from the file. Binary shorts read from the file are subsequent periods in samples. Periods are output until the file is empty. Examples: 1. A pulse train with period 100 samples and pulse width 1 sample: ptrain period=100p width=1p 2. A pulse train with period 8ms at 20KHz and pulse width 4 samples ptrain -p8ms -s20000 -w4 3. A square wave (pulse width = half period) of period 4ms, sampled at 10kHz ptrain period=4ms width=2ms samplerate=10kHz 3. A square wave of period 4ms with zero mean value ptrain period=4ms width=2ms offset=0 */ #include <stdio.h> #include <math.h> #include "options.h" #include "units.h" #include "strmatch.h" char applic[] = "generate a pulse train of binary shorts. " ; char usage[] = "ptrain [options]" ; static char *helpstr, *debugstr, *sampstr, *pstr, *astr, *ostr, *wstr, *dstr, *phstr ; static Options option[] = { { "help" , "off" , &helpstr , "help" , DEBUG }, { "debug" , "off" , &debugstr , "debugging switch" , DEBUG }, { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL }, { "duration" , "500ms" , &dstr , "duration of waveform" , VAL }, { "period" , "8ms" , &pstr , "period of pulse train" , VAL }, { "amplitude" , "1024" , &astr , "pulse amplitude" , VAL }, { "width" , "4p" , &wstr , "pulse width" , VAL }, { "offset" , "0" , &ostr , "dc offset" , VAL }, { "phase" , "0" , &phstr , "phase offset" , VAL }, ( char * ) 0 } ; main(argc, argv) int argc ; char *argv[] ; { FILE *fp, *fopen() ; short a, z = 0; /* Zero */ int i, j, samplerate ; int T, w, n, phase ; i = getopts( option,argc,argv ) ; if ( !isoff( helpstr ) ) helpopts3( helpstr, argv[0], applic, usage, option ) ; samplerate = to_Hz( sampstr ) ; T = (int)to_p( pstr, samplerate ) ; /* period */ w = (int)to_p( wstr, samplerate ) ; /* width */ n = to_p( dstr, samplerate ) ; /* duration in points */ z = atoi( ostr ) ; /* zero level set to the dc offset (default 0) */ a = atoi( astr ) + z ; /* amplitude (pulse height above zero level) */ if ( ( phase = (int)to_p( pstr, samplerate ) ) > 0 ) { /* initial phase offset */ for (j=0 ; j<phase ; j++ ) fwrite(&z, sizeof(short), 1, stdout); } /* Using given arg T to generate fixed periods */ if ( i == 0 ) { for (i=0 ; i<n ; ) { for (j=0 ; j<w && i<n ; j++, i++) fwrite(&a, sizeof(short), 1, stdout); for ( ; j<T && i<n ; j++, i++) fwrite(&z, sizeof(short), 1, stdout); } } /* Reading periods T from file argv[argc-i] */ else { if ( ( fp = fopen( argv[argc-i], "r" ) ) == NULL ) { fprintf(stderr,"can't open %s\n", argv[argc-i] ); exit(1); } while ( fread( &T, sizeof(short), 1, fp ) ) { for (j=0 ; j<w ; j++) fwrite(&a, sizeof(short), 1, stdout); for ( ; j<T ; j++) fwrite(&z, sizeof(short), 1, stdout); } fclose( fp ) ; } }