view tools/step.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
/*
  step.c    generate a step up or down.
  ------

*/

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

char applic[]  = "generate a step waveform. " ;
char usage[]   = "step [options]" ;

static char *helpstr, *debugstr, *sampstr, *astr,  *dstr, *typestr ;
static char *shiftstr, *polstr ;

static Options option[] = {
    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                              , DEBUG   },
    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"                  , DEBUG   },
    {   "samplerate",   "20kHz"     ,  &sampstr     ,   "samplerate "                       , VAL     },
    {   "amplitude" ,   "1"         ,  &astr        ,   "step height"                       , VAL     },
    {   "shift"     ,   "10ms"      ,  &shiftstr    ,   "position of step relative to start", VAL     },
    {   "polarity"  ,   "up"        ,  &polstr      ,   "step up or down"                   , VAL     },
    {   "duration"  ,   "500ms"     ,  &dstr        ,   "duration of waveform"              , VAL     },
    {   "type"      ,   "short"     ,  &typestr     ,   "output datatype"                   , VAL     },
   ( char * ) 0 } ;


int     samplerate ;
float   amplitude  ;
int     duration   ;
int     shift      ;
int     type       ;

main(argc, argv)
int   argc ;
char *argv[] ;
{
    int   i ;
    float y0, y1 ;

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

    samplerate = to_Hz( sampstr, 0 ) ;
    amplitude  = atof( astr ) ;
    shift      = to_p( shiftstr, samplerate ) ;
    duration   = to_p( dstr, samplerate ) ;

    if ( ( type = typeindex( typestr ) ) < 0 ) {
	fprintf(stderr,"step: unknown datatype [%s]\n", typestr) ;
	exit( 1 ) ;
    }

    if ( isstr( polstr, "up" ) ) {
	y0 = 0 ;         y1 = amplitude ;
    }
    else if ( isstr( polstr, "down" ) ) {
	y0 = amplitude ; y1 = 0 ;
    }
    else {
	fprintf(stderr,"step: unknown polarity [%s]\n", polstr) ;
	exit( 1 ) ;
    }

    for ( i = 0 ; i < shift ; i++ )
	writeitem( &y0, type, 1, stdout ) ;
    for (  ; i < duration ; i++ )
	writeitem( &y1, type, 1, stdout ) ;

}