stats

Capsim Block Documentation

Short Description

This star calculates the statistics of the incoming signal. The parameter is a filename for storage of the results.

Top
Input Connections
Port Type Name
0 float x
Top
Parameters
Num Description Type Name Default Value
0 Points to skip int skip
1 File to store results file stat_file stat.dat
Top
Result Variables (TCL)
Num Type Name Description
0 float BlockName_sigma The standard deviation of the input stream
1 float BlockName_var The variance of the input stream
2 float BlockName_mean The mean of the input stream
3 float BlockName_min The minimum value of the input stream
4 float BlockName_max The maximum value of the input stream
Top
States
Num Type Name Initial Value Description
0 FILE* file_ptr
1 int obufs
2 int count 0
3 int totalCount 0
4 float sum_x 0.0
5 float sum_x2 0.0
6 float max -1e30
7 float min 1e30
Top

Declarations


 

	int i,j;
	float xSample;	/* current sample of input signal	*/
	float mu;	/* mean = sum_x/count			*/
	float var;	/* variance = (sum_x2/count - mu**2)	*/
	float sigma;	/* std dev  = square root of variance	*/
	double sqrt();
	char theVar[100];
	char theName[100];
#ifdef TCL_SUPPORT
        Tcl_Obj *varNameObj_P;
        Tcl_Obj *objVar_P;
#endif	



Top

Initialization Code



 

	if( (obufs = NO_OUTPUT_BUFFERS()) > 1) {
		fprintf(stderr,"stats: only one output allowed\n");
		return(2);
	}




Top

Main Code



 


	/* note the minimum number of samples on the input 	*/
	/* buffers and iterate that many times 			*/
	for(i=MIN_AVAIL();i>0; --i) {
    	     IT_IN(0);
	     for(j=0; j skip) {
		count++;
		xSample = x(0);
		if (xSample > max)
			max = xSample;
		if (xSample < min)
			min = xSample;

		sum_x += xSample;
		sum_x2 += xSample * xSample;
	     }
	}
	return(0);




Top

Wrapup Code



 

	mu = sum_x/count;
	var = (sum_x2/count) - (mu*mu);
	sigma = sqrt(var);
	fprintf(stderr,"samples   \t%d       \tmean    \t%g\n",count,mu);
	fprintf(stderr,"maximum   \t%g  \tminimum \t%g\n",max,min);
	fprintf(stderr,"variance  \t%g  \tsigma   \t%g\n",var,sigma);
	fprintf(stderr,"samples   \t%d       \tmean    \t%g\n",count,mu); 
	fprintf(stderr,"maximum   \t%g  \tminimum \t%g\n",max,min);
	fprintf(stderr,"variance  \t%g  \tsigma   \t%g\n",var,sigma);
	{
		if( (file_ptr = fopen(stat_file,"w")) == NULL) {
			fprintf(stderr,"stats: can't open results file %s \n",
				stat_file);
			return(3);
	}
	{
		fprintf(file_ptr,"samples   %d  mean      %e \n",count,mu);
		fprintf(file_ptr,"maximum   %e  minimum %e \n",max,min);
		fprintf(file_ptr,"variance  %e  sigma   %e \n",var,sigma);
	}
	
#ifdef TCL_SUPPORT
       if(!krn_TCL_Interp) {
          
	  return(0);
       }
       
       sprintf(theName,"%s_sigma",STAR_NAME);

	varNameObj_P=Tcl_NewStringObj(theName, strlen(theName));
	objVar_P=Tcl_NewObj();
	Tcl_SetDoubleObj(objVar_P,sigma);
	Tcl_ObjSetVar2(krn_TCL_Interp,varNameObj_P,NULL,objVar_P,TCL_NAMESPACE_ONLY);
        
       
       sprintf(theName,"%s_mean",STAR_NAME);
	varNameObj_P=Tcl_NewStringObj(theName, strlen(theName));
	objVar_P=Tcl_NewObj();
	Tcl_SetDoubleObj(objVar_P,mu);
	Tcl_ObjSetVar2(krn_TCL_Interp,varNameObj_P,NULL,objVar_P,TCL_NAMESPACE_ONLY);
       
       
       
        sprintf(theName,"%s_max",STAR_NAME);
	varNameObj_P=Tcl_NewStringObj(theName, strlen(theName));
	objVar_P=Tcl_NewObj();
	Tcl_SetDoubleObj(objVar_P,max);
	Tcl_ObjSetVar2(krn_TCL_Interp,varNameObj_P,NULL,objVar_P,TCL_NAMESPACE_ONLY);
        
      
       
       sprintf(theName,"%s_min",STAR_NAME);
	varNameObj_P=Tcl_NewStringObj(theName, strlen(theName));
	objVar_P=Tcl_NewObj();
	Tcl_SetDoubleObj(objVar_P,min);
	Tcl_ObjSetVar2(krn_TCL_Interp,varNameObj_P,NULL,objVar_P,TCL_NAMESPACE_ONLY);
 
     

       
       
       
       
       sprintf(theName,"%s_var",STAR_NAME);
	varNameObj_P=Tcl_NewStringObj(theName, strlen(theName));
	objVar_P=Tcl_NewObj();
	Tcl_SetDoubleObj(objVar_P,var);
	Tcl_ObjSetVar2(krn_TCL_Interp,varNameObj_P,NULL,objVar_P,TCL_NAMESPACE_ONLY);


       
              
#endif

}
fclose(file_ptr);




Top

License



/*  Capsim (r) Text Mode Kernel (TMK) Star Library (Blocks)
    Copyright (C) 1989-2002  XCAD Corporation 

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    http://capsimtmk.sourceforge.net
    XCAD Corporation
    Raleigh, North Carolina */


Top

Description



 

/* stats.s */
/***************************************************************
			stats() 
*******************************************************************
	Input:		x, the signal of interest
	Output:		optional:  terminate signal or flow through
	Parameters:	1: file sig_name, signal identifier
			2: file stat_file, statistics file name
				default => no file created.
*******************************************************************
This star calculates the statistics of the incoming signal.  
The parameter is a filename for storage of the results.

stats


This star calculates the statistics of the incoming signal.  
The parameter is a filename for storage of the results.
	Input:		x, the signal of interest
	Output:		optional:  terminate signal or flow through
	Parameters:	1: file sig_name, signal identifier
			2: file stat_file, statistics file name
				default => no file created.
*******************************************************************


Programmer: 	Prayson W. Pate
Date:		December 8, 1987
Modified:	February 22, 1987
		April 1988
		May 1988 ljfaber: add 'flow-thru' capability
				: add signal identifier

*/