view tools/integframe.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
/*
    integrate.c    Integrate (ie average) SAI frames over time or frequency.
    -----------

    Sequence of frames must have an SAI header.
    The whole of every frame read in is integrated (in either time or freq).
    Use edframe to set integration limits and select frames.
    The output is a sequence of arrays without a header, one per input frame,
    each of length `framewidth'.
    If `average=on' the output is one array of length `framewidth' which is
    the average of the sequence of arrays.

    Example:

1. Integrate the 2nd SAI frame over time from 10ms to 20ms.
  gensai output=stdout ... | select frame=2 time=10ms-20ms  |  integrate var=time  >  ...

2. Integrate the 2nd SAI frame over frequency from channels 5 to 8 inclusive.
  gensai output=stdout ... | select frame=2 freq=5-8  |  integrate var=time  >  ...

*/

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


char applic[]     = "Integrate SAI frames over time or frequency. " ;

static char *helpstr, *debugstr, *varstr, *avstr, *typestr, *infostr ;

static Options option[] = {
    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                                          , DEBUG },
    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"                              , DEBUG },
    {   "variable"  ,   "time"      ,  &varstr      ,   "variable of integration (time or frequency)"   , VAL   },
    {   "average"   ,   "off"       ,  &avstr       ,   "average all integrated frames"                 , VAL   },
    {   "type"      ,   "short"     ,  &typestr     ,   "output data type"                              , VAL   },
    {   "info"      ,   "off"       ,  &infostr     ,   "print frame size info"                         , VAL   },
   ( char * ) 0 } ;


int     frameheight, framewidth ;       /* Parameters read from header */
int     framebytes ;
int     average    ;
int     type       ;


main(argc, argv)
int   argc ;
char *argv[] ;
{
    FILE   *fp ;
    char   *header, *SaiHeader();
    short  *frame ;
    float  *buf, *avbuf ;
    int     i, j, k, n ;

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

    average = ison( avstr ) ;
    if ( ( type = typeindex( typestr ) ) < 0 ) {
	fprintf( stderr, "integframe: bad type [%s]\n", typestr ) ;
	exit( 1 ) ;
    }

    if ( (header = ReadHeader(fp)) == (char *) 0 ) {
	fprintf(stderr,"integrate: header not found\n");
	exit(1);
    }

    frameheight = HeaderInt( header, "frameheight" );
    framewidth =  HeaderInt( header, "framewidth"  );
    framebytes =  HeaderInt( header, "framebytes"  );

    if ( ison( infostr ) )
	fprintf( stderr,"frameheight=%d  framewidth=%d\n", frameheight, framewidth ) ;

    /* Allocate space for framebytes of data */

    if ( (frame = (short *)malloc( framebytes )) == (short *)0 ) {
	fprintf(stderr,"integrate: malloc out of space\n");
	exit(1);
    }

    if ( iststr( varstr, "time" ) ) {
	if ( (buf = (float *)malloc( frameheight * sizeof( float ) )) == (float *)0 ) {
	    fprintf(stderr,"integrate: malloc out of space\n");
	    exit(1);
	}
	if ( average ) {
	    if ( (avbuf = (float *)malloc( frameheight * sizeof( float ) )) == (float *)0 ) {
		fprintf(stderr,"integrate: malloc out of space\n");
		exit(1);
	    }
	    for ( i=0 ; i<frameheight ; i++ )
		avbuf[i] = 0 ;
	}
	for ( n = 0 ; fread( frame,framebytes,1,fp ) ; n++ ) {
	    for ( i=0, j=0 ; i<frameheight ; i++, j+=framewidth )
		buf[i] = frame[j] ;
	    for ( k=1 ; k<framewidth ; k++)
		for ( i=0, j=k ; i<frameheight ; i++, j+=framewidth )
		    buf[i] += frame[j] ;
	    if ( average )
		for ( i=0 ; i<frameheight ; i++ )
		    avbuf[i] += ( buf[i] / framewidth ) ;
	    else {
		for ( i=0 ; i<frameheight ; i++ ) {
		    buf[i] = buf[i] / framewidth ;
		    writeitem( &buf[i], type, 1, stdout ) ;
		}
	    }
	}
	if ( average && n > 0 )  {
	    for ( i=0 ; i<frameheight ; i++ ) {
		avbuf[i] = avbuf[i] / n ;
		writeitem( &avbuf[i], type, 1, stdout ) ;
	    }
	}
    }

    else if ( iststr( varstr, "frequency" ) ) {
	if ( (buf = (float *)malloc( framewidth * sizeof( float ) )) == (float *)0 ) {
	    fprintf(stderr,"integrate: malloc out of space\n");
	    exit(1);
	}
	if ( average ) {
	    if ( (avbuf = (float *)malloc( framewidth * sizeof( float ) )) == (float *)0 ) {
		fprintf(stderr,"integrate: malloc out of space\n");
		exit(1);
	    }
	    for ( i=0 ; i<framewidth ; i++ )
		avbuf[i] = 0 ;
	}
	for ( n = 0 ; fread( frame,framebytes,1,fp ) ; n++ ) {
	    for ( i=0 ; i<framewidth ; i++ )
		buf[i] = frame[i] ;
	    for ( k=1 ; k<frameheight ; k++)
		for ( i=0, j=k*framewidth ; i<framewidth ; i++, j++ )
		    buf[i] += frame[j] ;
	    if ( average ) {
		for ( i=0 ; i<framewidth ; i++ )
		    avbuf[i] += ( buf[i] / frameheight ) ;
	    }
	    else {
		for ( i=0 ; i<framewidth ; i++ ) {
		    buf[i] = buf[i] / frameheight ;
		    writeitem( &buf[i], type, 1, stdout ) ;
		}
	    }
	}
	if ( average && n > 0 )  {
	    for ( i=0 ; i<framewidth ; i++ ) {
		avbuf[i] = avbuf[i] / n ;
		writeitem( &avbuf[i], type, 1, stdout ) ;
	    }
	}
    }

    else {
	fprintf( stderr,"unknown variable of integration [%s]\n", varstr ) ;
	exit( 1 ) ;
    }

    if ( ison( infostr ) )
	fprintf( stderr,"frames=%d\n", n ) ;

    fclose( fp ) ;
}