annotate 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
rev   line source
tomwalters@0 1 /*
tomwalters@0 2 ptrain.c generate a pulse train of binary shorts.
tomwalters@0 3 --------
tomwalters@0 4 Generate samples of a pulse train at a given sample rate.
tomwalters@0 5 Specify pulse amplitude and width (in s, ms, or p (sample points) ),
tomwalters@0 6 and also the period of the waveform (in s, ms, or p (sample points) ).
tomwalters@0 7 The pulse amplitude is the pulse height above the zero level, which is
tomwalters@0 8 set by the offset option. The offset is "0" by default so that pulses
tomwalters@0 9 of any given amplitude are non-negative by default.
tomwalters@0 10
tomwalters@0 11 Output samples in binary shorts for the given waveform duration.
tomwalters@0 12
tomwalters@0 13 Non-fixed period pulse trains:
tomwalters@0 14 If a filename is given, then the period of the output pulse train is
tomwalters@0 15 taken from the file. Binary shorts read from the file are subsequent
tomwalters@0 16 periods in samples. Periods are output until the file is empty.
tomwalters@0 17
tomwalters@0 18
tomwalters@0 19 Examples:
tomwalters@0 20
tomwalters@0 21 1. A pulse train with period 100 samples and pulse width 1 sample:
tomwalters@0 22
tomwalters@0 23 ptrain period=100p width=1p
tomwalters@0 24
tomwalters@0 25 2. A pulse train with period 8ms at 20KHz and pulse width 4 samples
tomwalters@0 26
tomwalters@0 27 ptrain -p8ms -s20000 -w4
tomwalters@0 28
tomwalters@0 29 3. A square wave (pulse width = half period) of period 4ms, sampled at 10kHz
tomwalters@0 30
tomwalters@0 31 ptrain period=4ms width=2ms samplerate=10kHz
tomwalters@0 32
tomwalters@0 33 3. A square wave of period 4ms with zero mean value
tomwalters@0 34
tomwalters@0 35 ptrain period=4ms width=2ms offset=0
tomwalters@0 36
tomwalters@0 37 */
tomwalters@0 38
tomwalters@0 39
tomwalters@0 40 #include <stdio.h>
tomwalters@0 41 #include <math.h>
tomwalters@0 42 #include "options.h"
tomwalters@0 43 #include "units.h"
tomwalters@0 44 #include "strmatch.h"
tomwalters@0 45
tomwalters@0 46 char applic[] = "generate a pulse train of binary shorts. " ;
tomwalters@0 47 char usage[] = "ptrain [options]" ;
tomwalters@0 48
tomwalters@0 49 static char *helpstr, *debugstr, *sampstr, *pstr, *astr, *ostr, *wstr, *dstr, *phstr ;
tomwalters@0 50
tomwalters@0 51 static Options option[] = {
tomwalters@0 52 { "help" , "off" , &helpstr , "help" , DEBUG },
tomwalters@0 53 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
tomwalters@0 54 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL },
tomwalters@0 55 { "duration" , "500ms" , &dstr , "duration of waveform" , VAL },
tomwalters@0 56 { "period" , "8ms" , &pstr , "period of pulse train" , VAL },
tomwalters@0 57 { "amplitude" , "1024" , &astr , "pulse amplitude" , VAL },
tomwalters@0 58 { "width" , "4p" , &wstr , "pulse width" , VAL },
tomwalters@0 59 { "offset" , "0" , &ostr , "dc offset" , VAL },
tomwalters@0 60 { "phase" , "0" , &phstr , "phase offset" , VAL },
tomwalters@0 61 ( char * ) 0 } ;
tomwalters@0 62
tomwalters@0 63
tomwalters@0 64 main(argc, argv)
tomwalters@0 65 int argc ;
tomwalters@0 66 char *argv[] ;
tomwalters@0 67 {
tomwalters@0 68 FILE *fp, *fopen() ;
tomwalters@0 69 short a, z = 0; /* Zero */
tomwalters@0 70 int i, j, samplerate ;
tomwalters@0 71 int T, w, n, phase ;
tomwalters@0 72
tomwalters@0 73 i = getopts( option,argc,argv ) ;
tomwalters@0 74 if ( !isoff( helpstr ) )
tomwalters@0 75 helpopts3( helpstr, argv[0], applic, usage, option ) ;
tomwalters@0 76
tomwalters@0 77 samplerate = to_Hz( sampstr ) ;
tomwalters@0 78 T = (int)to_p( pstr, samplerate ) ; /* period */
tomwalters@0 79 w = (int)to_p( wstr, samplerate ) ; /* width */
tomwalters@0 80 n = to_p( dstr, samplerate ) ; /* duration in points */
tomwalters@0 81
tomwalters@0 82 z = atoi( ostr ) ; /* zero level set to the dc offset (default 0) */
tomwalters@0 83 a = atoi( astr ) + z ; /* amplitude (pulse height above zero level) */
tomwalters@0 84
tomwalters@0 85
tomwalters@0 86 if ( ( phase = (int)to_p( pstr, samplerate ) ) > 0 ) { /* initial phase offset */
tomwalters@0 87 for (j=0 ; j<phase ; j++ )
tomwalters@0 88 fwrite(&z, sizeof(short), 1, stdout);
tomwalters@0 89 }
tomwalters@0 90
tomwalters@0 91 /* Using given arg T to generate fixed periods */
tomwalters@0 92
tomwalters@0 93 if ( i == 0 ) {
tomwalters@0 94 for (i=0 ; i<n ; ) {
tomwalters@0 95 for (j=0 ; j<w && i<n ; j++, i++)
tomwalters@0 96 fwrite(&a, sizeof(short), 1, stdout);
tomwalters@0 97 for ( ; j<T && i<n ; j++, i++)
tomwalters@0 98 fwrite(&z, sizeof(short), 1, stdout);
tomwalters@0 99 }
tomwalters@0 100 }
tomwalters@0 101
tomwalters@0 102
tomwalters@0 103 /* Reading periods T from file argv[argc-i] */
tomwalters@0 104
tomwalters@0 105 else {
tomwalters@0 106 if ( ( fp = fopen( argv[argc-i], "r" ) ) == NULL ) {
tomwalters@0 107 fprintf(stderr,"can't open %s\n", argv[argc-i] );
tomwalters@0 108 exit(1);
tomwalters@0 109 }
tomwalters@0 110 while ( fread( &T, sizeof(short), 1, fp ) ) {
tomwalters@0 111 for (j=0 ; j<w ; j++)
tomwalters@0 112 fwrite(&a, sizeof(short), 1, stdout);
tomwalters@0 113 for ( ; j<T ; j++)
tomwalters@0 114 fwrite(&z, sizeof(short), 1, stdout);
tomwalters@0 115 }
tomwalters@0 116 fclose( fp ) ;
tomwalters@0 117 }
tomwalters@0 118
tomwalters@0 119 }