view 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
line wrap: on
line source
/*
    cosine.c    generate a cosine window
    --------

    1. Hann (or raised cosine) window

	w[n] = 0.5 [ 1 - cos( TWOPI . n/(N-1) ) ]

    2. Hamming window

	w[n] = 0.54 - 0.46 cos( TWOPI . n/(N-1) )

    Both windows are defined for 0 <= n <= N-1 and are zero otherwise.


*/

#include <stdio.h>
#include <math.h>
#include "options.h"
#include "units.h"
#include "strmatch.h"
#include "sigproc.h"


char applic[]     = "generate a cosine window." ;

static char *helpstr,  *debugstr, *sampstr,  *hammstr ;
static char *scalestr, *typestr,  *sizestr ;

static Options option[] = {
    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                                      , DEBUG   },
    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"                          , DEBUG   },
    {   "samplerate",   "20kHz"     ,  &sampstr     ,   "samplerate "                               , VAL     },
    {   "size"      ,   "10ms"      ,  &sizestr     ,   "Size of window"                            , VAL     },
    {   "Hamming"   ,   "off"       ,  &hammstr     ,   "Hamming window (default raised cosine)"    , SETFLAG },
    {   "scale"     ,   "1000"      ,  &scalestr    ,   "Scale factor for output"                   , VAL     },
    {   "type"      ,   "short"     ,  &typestr     ,   "Output datatype (short/float)"             , VAL     },
   ( char * ) 0 } ;


int     samplerate  ;

float  *window ;
int     n      ;

short  *buf    ;


main (argc, argv)
int     argc;
char  **argv;
{
    float f ;

    getopts( option,argc,argv ) ;
    if ( !isoff( helpstr ) )
	helpopts( helpstr, argv[0], applic, option ) ;

    samplerate = to_Hz( sampstr ) ;
    n          = to_p( sizestr, samplerate ) ;

    if ( ison( hammstr ) )
	window = hamming( n ) ;

    else if ( isoff( hammstr ) )
	window = raised_cosine( n ) ;

    else {
	fprintf(stderr,"cosine: unknown window [%s]\n", hammstr) ;
	exit( 1 ) ;
    }

    if ( iststr( typestr, "float" ) ) {
	scalar( window, n, atof( scalestr ) ) ;
	fwrite( window, sizeof(float), n, stdout ) ;
    }

    else if ( iststr( typestr, "short" ) ) {
	buf = (short *)malloc( n * sizeof(short) ) ;
	if ( ( f = ftos( window, buf, n, atof( scalestr ) ) ) < 1. )
	    fprintf( stderr,"Warning: 16-bit overflow. Try scale factor < %f\n", f ) ;
	fwrite( buf, sizeof(short), n, stdout ) ;
    }

    else
	fprintf(stderr,"cosine: unknown datatype [%s]\n", typestr) ;
}


scalar( y, n, scale )
float  *y ;
int     n ;
float   scale ;
{
    int  i ;

    if ( scale != 1. ) {
	for ( i=0 ; i < n ; i++ )
	    y[i] *= scale ;
    }
}