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