comparison tools/ramp.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 ramp.c generate an exponential sawtooth waveform
3 ------ (in binary shorts or floats).
4
5 Generate samples of a sawtooth waveform at a given sample rate.
6
7 Decaying exponential: A.exp(-t) 0<=t<=T
8 Growing exponential: A.exp(t) -T<=t<=0
9 or (shifting): A.exp(t-T) 0<=t<=T
10
11 To arrange for a decay factor to correspond to the half life, so that
12 the wave grows/decays to half the given amplitude in the given decay time,
13 the argument of the exponential must be calibrated to result in 0.5.
14 If exp(-x) = 0.5, then we have -x = ln(0.5) = -0.693147
15 so that: x = 0.693147
16 For a given decay factor, the damped exponential decays to half the given
17 amplitude in this time. The ramped exponential is a time-reversed damped
18 exponential.
19
20
21 Examples:
22
23 1. Growing exponentials
24
25 ramp polarity=ramp dec=1ms | x11plot
26
27 2. Decaying exponentials
28
29 ramp polarity=damp dec=1ms | x11plot
30
31 3. Half a cycle of an 8ms decaying exponential.
32
33 ramp polarity=damp dec=1ms dur=4ms | x11plot
34
35 4. Modulating a tone with a damped exponential.
36
37 tone period=.5ms amp=500 type=float > foo1
38 ramp pol=damp dec=1ms amp=1 type=float > foo2
39 merge op=mult type=float foo1 foo2 | ftos | x11plot -n512
40
41 5. Modulating a tone with a ramped exponential.
42
43 tone period=.5ms amp=500 type=float > foo1
44 ramp pol=ramp dec=1ms amp=1 type=float > foo2
45 merge op=mult type=float foo1 foo2 | ftos | x11plot -n512
46
47 6. Modulating a tone with a damped exponential, and half-wave rectifying to
48 generate damped pulses.
49
50 tone period=.5ms amp=500 type=float > foo1
51 ramp pol=damp dec=1ms amp=1 type=float > foo2
52 merge op=mult type=float foo1 foo2 | ftos | gate range=min-0 op=0 | x11plot -n512
53
54 7. Modulating white noise with a ramped exponential.
55
56 noise type=float > foo1
57 ramp pol=ramp dec=1ms amp=1 type=float > foo2
58 merge op=mult type=float foo1 foo2 | ftos | x11plot -n512
59
60
61 */
62
63 #include <stdio.h>
64 #include <math.h>
65 #include "options.h"
66 #include "units.h"
67 #include "strmatch.h"
68
69 char applic[] = "generate an exponential sawtooth waveform. " ;
70 char usage[] = "ramp [options]" ;
71
72 static char *helpstr, *debugstr, *sampstr, *perstr, *astr, *decstr, *polstr, *dstr, *datastr ;
73
74 static Options option[] = {
75 { "help" , "off" , &helpstr , "help" , DEBUG },
76 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
77 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL },
78 { "period" , "8ms" , &perstr , "period [s,ms,p] of ramp" , VAL },
79 { "amplitude" , "1024" , &astr , "amplitude of waveform" , VAL },
80 { "decay" , "2ms" , &decstr , "half-life of ramp" , VAL },
81 { "polarity" , "ramp" , &polstr , "ramp (growing) / damp (decaying)", VAL },
82 { "duration" , "500ms" , &dstr , "duration of waveform" , VAL },
83 { "type" , "short" , &datastr , "o/p datatype (short/float)", VAL },
84 ( char * ) 0 } ;
85
86
87 #define DECAYFACTOR ( 0.693147 )
88
89 int samplerate ;
90 float amplitude ;
91 int duration ;
92 int period ;
93 int decay ;
94 double decayrate ;
95
96 main(argc, argv)
97 int argc ;
98 char *argv[] ;
99 {
100 int i, n, t ;
101 short s ;
102 float f ;
103
104 getopts( option,argc,argv ) ;
105 if ( !isoff( helpstr ) )
106 helpopts3( helpstr, argv[0], applic, usage, option ) ;
107
108 samplerate = to_Hz( sampstr, 0 ) ;
109 amplitude = atof( astr ) ;
110 period = (int)to_p( perstr, samplerate ) ;
111 decay = (int)to_p( decstr, samplerate ) ;
112 duration = to_p( dstr, samplerate ) ;
113
114 /*
115 if ( decay >= period ) {
116 fprintf(stderr,"ramp: half-life period [%dp] must be less than waveform period [%dp]\n", decay, period);
117 exit( 1 ) ;
118 }
119 */
120 decayrate = DECAYFACTOR / (double)decay ;
121
122 if ( iststr( polstr, "damped" ) ) {
123
124 /* Decaying exponential */
125
126 if ( iststr( datastr, "short" ) )
127 for ( i=0 ; i<duration ; )
128 for ( t=0 ; t<period && i<duration ; t++, i++ ) {
129 s = amplitude * exp( - (double)( t * decayrate ) ) ;
130 fwrite( &s, sizeof(short), 1, stdout ) ;
131 }
132 else if ( iststr( datastr, "float" ) )
133 for ( i=0 ; i<duration ; )
134 for ( t=0 ; t<period && i<duration ; t++, i++ ) {
135 f = amplitude * exp( - (double)( t * decayrate ) ) ;
136 fwrite( &f, sizeof(float), 1, stdout ) ;
137 }
138 else
139 fprintf(stderr,"unknown datatype [%s]\n", datastr) ;
140 }
141 else if ( iststr( polstr, "ramped" ) ) {
142
143 /* Growing exponential */
144
145 if ( iststr( datastr, "short" ) )
146 for ( i=0 ; i<duration ; )
147 for ( t=(-period) ; t<=0 && i<duration ; t++, i++ ) {
148 s = amplitude * exp( (double)( t * decayrate ) ) ;
149 fwrite( &s, sizeof(short), 1, stdout ) ;
150 }
151 else if ( iststr( datastr, "float" ) )
152 for ( i=0 ; i<duration ; )
153 for ( t=(-period) ; t<=0 && i<duration ; t++, i++ ) {
154 f = amplitude * exp( (double)( t * decayrate ) ) ;
155 fwrite( &f, sizeof(float), 1, stdout ) ;
156 }
157 else
158 fprintf(stderr,"unknown datatype [%s]\n", datastr) ;
159 }
160 else
161 fprintf( stderr, "unknown polarity [%s]\n", polstr ) ;
162 }