Mercurial > hg > aim92
comparison tools/cosine.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 cosine.c generate a cosine window | |
3 -------- | |
4 | |
5 1. Hann (or raised cosine) window | |
6 | |
7 w[n] = 0.5 [ 1 - cos( TWOPI . n/(N-1) ) ] | |
8 | |
9 2. Hamming window | |
10 | |
11 w[n] = 0.54 - 0.46 cos( TWOPI . n/(N-1) ) | |
12 | |
13 Both windows are defined for 0 <= n <= N-1 and are zero otherwise. | |
14 | |
15 | |
16 */ | |
17 | |
18 #include <stdio.h> | |
19 #include <math.h> | |
20 #include "options.h" | |
21 #include "units.h" | |
22 #include "strmatch.h" | |
23 #include "sigproc.h" | |
24 | |
25 | |
26 char applic[] = "generate a cosine window." ; | |
27 | |
28 static char *helpstr, *debugstr, *sampstr, *hammstr ; | |
29 static char *scalestr, *typestr, *sizestr ; | |
30 | |
31 static Options option[] = { | |
32 { "help" , "off" , &helpstr , "help" , DEBUG }, | |
33 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG }, | |
34 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL }, | |
35 { "size" , "10ms" , &sizestr , "Size of window" , VAL }, | |
36 { "Hamming" , "off" , &hammstr , "Hamming window (default raised cosine)" , SETFLAG }, | |
37 { "scale" , "1000" , &scalestr , "Scale factor for output" , VAL }, | |
38 { "type" , "short" , &typestr , "Output datatype (short/float)" , VAL }, | |
39 ( char * ) 0 } ; | |
40 | |
41 | |
42 int samplerate ; | |
43 | |
44 float *window ; | |
45 int n ; | |
46 | |
47 short *buf ; | |
48 | |
49 | |
50 main (argc, argv) | |
51 int argc; | |
52 char **argv; | |
53 { | |
54 float f ; | |
55 | |
56 getopts( option,argc,argv ) ; | |
57 if ( !isoff( helpstr ) ) | |
58 helpopts( helpstr, argv[0], applic, option ) ; | |
59 | |
60 samplerate = to_Hz( sampstr ) ; | |
61 n = to_p( sizestr, samplerate ) ; | |
62 | |
63 if ( ison( hammstr ) ) | |
64 window = hamming( n ) ; | |
65 | |
66 else if ( isoff( hammstr ) ) | |
67 window = raised_cosine( n ) ; | |
68 | |
69 else { | |
70 fprintf(stderr,"cosine: unknown window [%s]\n", hammstr) ; | |
71 exit( 1 ) ; | |
72 } | |
73 | |
74 if ( iststr( typestr, "float" ) ) { | |
75 scalar( window, n, atof( scalestr ) ) ; | |
76 fwrite( window, sizeof(float), n, stdout ) ; | |
77 } | |
78 | |
79 else if ( iststr( typestr, "short" ) ) { | |
80 buf = (short *)malloc( n * sizeof(short) ) ; | |
81 if ( ( f = ftos( window, buf, n, atof( scalestr ) ) ) < 1. ) | |
82 fprintf( stderr,"Warning: 16-bit overflow. Try scale factor < %f\n", f ) ; | |
83 fwrite( buf, sizeof(short), n, stdout ) ; | |
84 } | |
85 | |
86 else | |
87 fprintf(stderr,"cosine: unknown datatype [%s]\n", typestr) ; | |
88 } | |
89 | |
90 | |
91 scalar( y, n, scale ) | |
92 float *y ; | |
93 int n ; | |
94 float scale ; | |
95 { | |
96 int i ; | |
97 | |
98 if ( scale != 1. ) { | |
99 for ( i=0 ; i < n ; i++ ) | |
100 y[i] *= scale ; | |
101 } | |
102 } |