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