Mercurial > hg > aim92
comparison tools/tone.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 tone.c generate a pure tone. | |
3 ------ | |
4 Generate samples of a sine wave at a given sample rate. | |
5 Specify wave amplitude, and frequency (in Hz or kHz), or alternatively | |
6 period (in s, ms, or p (sample points) ). If both period and frequency | |
7 are specified, then the given period takes precedence. | |
8 If S is the samplerate, and Ts=1/S is the sample interval, | |
9 then each sample of a sine wave of period T samples is given by: | |
10 sin(n*(Ts/T)*TWOPI) for n=0,1,2,... | |
11 Output samples in the given datatype for the given waveform duration. | |
12 | |
13 | |
14 Examples: | |
15 | |
16 1. Sine wave with period 10ms sampled at 10kHz, (100 sample points per period) | |
17 | |
18 tone period=10ms samplerate=10kHz | x11plot | |
19 | |
20 2. Sine wave with frequency 100Hz sampled at 20kHz | |
21 | |
22 tone frequency=100Hz | x11plot | |
23 | |
24 3. Sine wave with period 100 sample points, with dc-offset set equal to | |
25 amplitude of 500 so that waveform is just non-negative. | |
26 | |
27 tone period=100p amplitude=500 offset=500 | x11plot | |
28 | |
29 4. Quarter cycle of a sine wave with 8ms period. | |
30 | |
31 tone period=8ms duration=2ms | x11plot | |
32 | |
33 */ | |
34 | |
35 #include <stdio.h> | |
36 #include <math.h> | |
37 #include "options.h" | |
38 #include "units.h" | |
39 #include "strmatch.h" | |
40 | |
41 #define TWOPI 6.28318530717 | |
42 | |
43 char applic[] = "generate a pure tone. " ; | |
44 char usage[] = "tone [options]" ; | |
45 | |
46 static char *helpstr, *debugstr, *sampstr, *perstr, *freqstr, *astr, *ostr, *dstr, *typestr ; | |
47 static char *phasestr ; | |
48 | |
49 static Options option[] = { | |
50 { "help" , "off" , &helpstr , "help" , DEBUG }, | |
51 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG }, | |
52 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL }, | |
53 { "period" , "8ms" , &perstr , "period of waveform" , VAL }, | |
54 { "frequency" , "125Hz" , &freqstr , "frequency of waveform" , VAL }, | |
55 { "phase" , "0" , &phasestr , "phase offset" , VAL }, | |
56 { "amplitude" , "512" , &astr , "amplitude of waveform" , VAL }, | |
57 { "mean" , "0" , &ostr , "mean value of waveform" , VAL }, | |
58 { "duration" , "500ms" , &dstr , "duration of waveform" , VAL }, | |
59 { "type" , "short" , &typestr , "output datatype" , VAL }, | |
60 ( char * ) 0 } ; | |
61 | |
62 | |
63 int samplerate ; | |
64 float amplitude ; | |
65 float offset ; | |
66 int type ; /* datatype index */ | |
67 | |
68 main(argc, argv) | |
69 int argc ; | |
70 char *argv[] ; | |
71 { | |
72 float T=0, Tr, r, y ; | |
73 int i, n ; | |
74 | |
75 getopts( option,argc,argv ) ; | |
76 if ( !isoff( helpstr ) ) | |
77 helpopts3( helpstr, argv[0], applic, usage, option ) ; | |
78 | |
79 samplerate = to_Hz( sampstr, 0 ) ; | |
80 amplitude = atof( astr ) ; | |
81 offset = atof( ostr ) ; | |
82 | |
83 if ( ( type = typeindex( typestr ) ) < 0 ) { | |
84 fprintf( stderr, "tone: bad type [%s]\n", typestr ) ; | |
85 exit( 1 ) ; | |
86 } | |
87 | |
88 n = to_p( dstr, samplerate ) ; | |
89 | |
90 if ( isstr( perstr, optdflt( option, "period" ) ) ) | |
91 T = 1.0 / to_Hz( freqstr, samplerate ) ; | |
92 else | |
93 T = to_s( perstr, samplerate ) ; | |
94 | |
95 Tr = TWOPI / ( samplerate * T ) ; /* period of max resolution */ | |
96 | |
97 if ( iststr( phasestr, "sine" ) ) /* start */ | |
98 r = 0 ; | |
99 else if ( iststr( phasestr, "cosine" ) ) | |
100 r = TWOPI / 4 ; | |
101 else if ( iststr( phasestr, "antisine" ) ) | |
102 r = TWOPI / 2 ; | |
103 else if ( iststr( phasestr, "anticosine" ) ) | |
104 r = 3 * TWOPI / 4 ; | |
105 else if ( isstr( phasestr + strlen( phasestr ) - 3, "deg" ) ) | |
106 r = TWOPI * ( atof( phasestr ) / 360. ) ; | |
107 else | |
108 r = TWOPI * ( to_s( phasestr, samplerate ) / T ) ; | |
109 | |
110 for ( i = 0 ; i < n ; i++, r += Tr ) { | |
111 y = amplitude * sin(r) + offset ; | |
112 writeitem( &y, type, 1, stdout ) ; | |
113 } | |
114 } |