tomwalters@0: /* tomwalters@0: cosine.c generate a cosine window tomwalters@0: -------- tomwalters@0: tomwalters@0: 1. Hann (or raised cosine) window tomwalters@0: tomwalters@0: w[n] = 0.5 [ 1 - cos( TWOPI . n/(N-1) ) ] tomwalters@0: tomwalters@0: 2. Hamming window tomwalters@0: tomwalters@0: w[n] = 0.54 - 0.46 cos( TWOPI . n/(N-1) ) tomwalters@0: tomwalters@0: Both windows are defined for 0 <= n <= N-1 and are zero otherwise. tomwalters@0: tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "options.h" tomwalters@0: #include "units.h" tomwalters@0: #include "strmatch.h" tomwalters@0: #include "sigproc.h" tomwalters@0: tomwalters@0: tomwalters@0: char applic[] = "generate a cosine window." ; tomwalters@0: tomwalters@0: static char *helpstr, *debugstr, *sampstr, *hammstr ; tomwalters@0: static char *scalestr, *typestr, *sizestr ; tomwalters@0: tomwalters@0: static Options option[] = { tomwalters@0: { "help" , "off" , &helpstr , "help" , DEBUG }, tomwalters@0: { "debug" , "off" , &debugstr , "debugging switch" , DEBUG }, tomwalters@0: { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL }, tomwalters@0: { "size" , "10ms" , &sizestr , "Size of window" , VAL }, tomwalters@0: { "Hamming" , "off" , &hammstr , "Hamming window (default raised cosine)" , SETFLAG }, tomwalters@0: { "scale" , "1000" , &scalestr , "Scale factor for output" , VAL }, tomwalters@0: { "type" , "short" , &typestr , "Output datatype (short/float)" , VAL }, tomwalters@0: ( char * ) 0 } ; tomwalters@0: tomwalters@0: tomwalters@0: int samplerate ; tomwalters@0: tomwalters@0: float *window ; tomwalters@0: int n ; tomwalters@0: tomwalters@0: short *buf ; tomwalters@0: tomwalters@0: tomwalters@0: main (argc, argv) tomwalters@0: int argc; tomwalters@0: char **argv; tomwalters@0: { tomwalters@0: float f ; tomwalters@0: tomwalters@0: getopts( option,argc,argv ) ; tomwalters@0: if ( !isoff( helpstr ) ) tomwalters@0: helpopts( helpstr, argv[0], applic, option ) ; tomwalters@0: tomwalters@0: samplerate = to_Hz( sampstr ) ; tomwalters@0: n = to_p( sizestr, samplerate ) ; tomwalters@0: tomwalters@0: if ( ison( hammstr ) ) tomwalters@0: window = hamming( n ) ; tomwalters@0: tomwalters@0: else if ( isoff( hammstr ) ) tomwalters@0: window = raised_cosine( n ) ; tomwalters@0: tomwalters@0: else { tomwalters@0: fprintf(stderr,"cosine: unknown window [%s]\n", hammstr) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: if ( iststr( typestr, "float" ) ) { tomwalters@0: scalar( window, n, atof( scalestr ) ) ; tomwalters@0: fwrite( window, sizeof(float), n, stdout ) ; tomwalters@0: } tomwalters@0: tomwalters@0: else if ( iststr( typestr, "short" ) ) { tomwalters@0: buf = (short *)malloc( n * sizeof(short) ) ; tomwalters@0: if ( ( f = ftos( window, buf, n, atof( scalestr ) ) ) < 1. ) tomwalters@0: fprintf( stderr,"Warning: 16-bit overflow. Try scale factor < %f\n", f ) ; tomwalters@0: fwrite( buf, sizeof(short), n, stdout ) ; tomwalters@0: } tomwalters@0: tomwalters@0: else tomwalters@0: fprintf(stderr,"cosine: unknown datatype [%s]\n", typestr) ; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: scalar( y, n, scale ) tomwalters@0: float *y ; tomwalters@0: int n ; tomwalters@0: float scale ; tomwalters@0: { tomwalters@0: int i ; tomwalters@0: tomwalters@0: if ( scale != 1. ) { tomwalters@0: for ( i=0 ; i < n ; i++ ) tomwalters@0: y[i] *= scale ; tomwalters@0: } tomwalters@0: }