fxcasfil

Capsim Block Documentation

Short Description

Block implements fixed point cascade form IIR digital filter.

Top
Input Connections
Port Type Name
0 int x
Top
Output Connections
Port Type Name
0 int y
Top
Parameters
Num Description Type Name Default Value
0 File with cascade filter parameters file filename tmp.cas
1 Number of bits to represent fraction int qbits 8
2 Word length int size 32
3 Accumulator Roundoff bits int roundoff_bits 8
4 Accumulator Word length int accumSizeRound 32
5 saturation mode int saturation_mode 1
Top
States
Num Type Name Initial Value Description
0 int ycas_A[MAX_SECTIONS]
1 int xs_A[3]
2 int ys_A[3]
3 int factorOne1
4 int factorOne0
5 int factorOneLess
6 int poleSec1FxFact0_A[MAX_SECTIONS]
7 int poleSec1FxFact1_A[MAX_SECTIONS]
8 int poleSec1LessFlag_A[MAX_SECTIONS]
9 int poleSec2FxFact0_A[MAX_SECTIONS]
10 int poleSec2FxFact1_A[MAX_SECTIONS]
11 int poleSec2LessFlag_A[MAX_SECTIONS]
12 int zeroSec1FxFact0_A[MAX_SECTIONS]
13 int zeroSec1FxFact1_A[MAX_SECTIONS]
14 int zeroSec1LessFlag_A[MAX_SECTIONS]
15 int zeroSec2FxFact0_A[MAX_SECTIONS]
16 int zeroSec2FxFact1_A[MAX_SECTIONS]
17 int zeroSec2LessFlag_A[MAX_SECTIONS]
18 double fs
19 double wnorminv
20 int ns
21 int n
22 int maxv
Top

Declarations


 

   	int i,j,jj,jt;
	float wnorm;
        FILE *ird_F;
	int  val;
	float  factor;
	int    fxfactor;
	int xs01,xs00,xs0less;
	int xs11,xs10,xs1less;
	int xs21,xs20,xs2less;
	int ys11,ys10,ys1less;
	int ys21,ys20,ys2less;
		
        int sum0,sum1;
	int out0,out1;

        double poleSec1_A[MAX_SECTIONS];
        double poleSec2_A[MAX_SECTIONS];
        double zeroSec1_A[MAX_SECTIONS];
	double zeroSec2_A[MAX_SECTIONS];
	



Top

Initialization Code



 

	/*
	 * open file containing filter coefficients. Check 
	 * to see if it exists.
	 *
	 */
        if( (ird_F = fopen(filename,"r")) == NULL) {
		fprintf(stderr,"Casfil: File could not be opened  \n");
		return(4);
	}
	/*
	 * Read in the filter coefficients and filter parameters.
	 *
	 */
         fscanf(ird_F,"%d",&ns);
         if( ns > MAX_SECTIONS) {
		fprintf(stderr,"Casfil: Too many sections  \n");
		return(5);
	}	 
	 
      	 for (i=0; i0.0)
		   fxfactor = (int)(factor * val + 0.5);
              else
		   fxfactor = (int)(factor * val - 0.5);
         if (fxfactor > maxv || (-fxfactor) > maxv) {
        	    fprintf(stderr,"fxcasfil: gain can not be represented by size bits\n");
        	    return(1);
         }	 
	 Fx_Part(size,fxfactor,&factorOne1,&factorOne0,&factorOneLess);
	      
     	 for (i=0; i0.0)
		   fxfactor = (int)(factor * val + 0.5);
              else
		   fxfactor = (int)(factor * val - 0.5);
              if (fxfactor > maxv || (-fxfactor) > maxv) {
        	    fprintf(stderr,"fxcasfil: gain can not be represented by size bits\n");
        	    return(2);
              }	 
	      Fx_Part(size,fxfactor,&zeroSec1FxFact1_A[i],&zeroSec1FxFact0_A[i],&zeroSec1LessFlag_A[i]);
          

              factor=zeroSec2_A[i];	
              if (factor>0.0)
		   fxfactor = (int)(factor * val + 0.5);
              else
		   fxfactor = (int)(factor * val - 0.5);
              if (fxfactor > maxv || (-fxfactor) > maxv) {
        	    fprintf(stderr,"fxcasfil: gain can not be represented by size bits\n");
        	    return(3);
              }	 
	      Fx_Part(size,fxfactor,&zeroSec2FxFact1_A[i],&zeroSec2FxFact0_A[i],&zeroSec2LessFlag_A[i]);

	      
	      /* use negative so we can just accumulate */
              factor= -poleSec1_A[i];	
              if (factor>0.0)
		   fxfactor = (int)(factor * val + 0.5);
              else
		   fxfactor = (int)(factor * val - 0.5);
              if (fxfactor > maxv || (-fxfactor) > maxv) {
        	    fprintf(stderr,"fxcasfil: gain can not be represented by size bits\n");
        	    return(4);
              }	 
	      Fx_Part(size,fxfactor,&poleSec1FxFact1_A[i],&poleSec1FxFact0_A[i],&poleSec1LessFlag_A[i]);
	      
	      /* use negative so we can just accumulate */
              factor= -poleSec2_A[i];	
              if (factor>0.0)
		   fxfactor = (int)(factor * val + 0.5);
              else
		   fxfactor = (int)(factor * val - 0.5);
              if (fxfactor > maxv || (-fxfactor) > maxv) {
        	    fprintf(stderr,"fxcasfil: gain can not be represented by size bits\n");
        	    return(5);
              }	 
	      Fx_Part(size,fxfactor,&poleSec2FxFact1_A[i],&poleSec2FxFact0_A[i],&poleSec2LessFlag_A[i]);
	      
	}      
	      



Top

Main Code



 


        while (IT_IN(0))
        {
           for (j=0; j< ns; j++){
                    if (j==0){
                              xs_A[1]=0;
                              xs_A[2]=0;
                              xs_A[0]=x(0);
                              if (n>0) xs_A[1]=x(1);
                              if (n>1) xs_A[2]=x(2);
                    }
                    if (j>0) { 
                             for (jj=0; jj<3; jj++)
                                           xs_A[jj] = ys_A[jj];
                    }

                    jt = j*3;
                    for (jj=0; jj<2; jj++)  
                        ys_A[jj+1] = ycas_A[jt+jj];

		     
		     
	       /* 
	        * Implementing:
                * ys_A[0]=xs_A[0]+(zeroSec1_A[j]*xs_A[1])+(zeroSec2_A[j]*xs_A[2])-(poleSec1_A[j]*ys_A[1])-(poleSec2_A[j]*ys_A[2]);
                */
		Fx_Part(size,xs_A[0],&xs01,&xs00,&xs0less);
		Fx_Part(size,xs_A[1],&xs11,&xs10,&xs1less);
		Fx_Part(size,xs_A[2],&xs21,&xs20,&xs2less);
		Fx_Part(size,ys_A[1],&ys11,&ys10,&ys1less);
		Fx_Part(size,ys_A[2],&ys21,&ys20,&ys2less);
		
		Fx_MultVar(xs0less,factorOneLess,size,xs01,xs00,
                                    factorOne1,factorOne0,&sum1,&sum0);
		

		Fx_MultVar(zeroSec1LessFlag_A[j],xs1less,size,zeroSec1FxFact1_A[j],zeroSec1FxFact0_A[j],
                                    xs11,xs10,&out1,&out0);

                Fx_AddVar(size,saturation_mode,sum1,sum0,out1,out0,&sum1,&sum0); 

		Fx_MultVar(zeroSec2LessFlag_A[j],xs2less,size,zeroSec2FxFact1_A[j],zeroSec2FxFact0_A[j],
                                    xs21,xs20,&out1,&out0);

                Fx_AddVar(size,saturation_mode,out1,out0,sum1,sum0,&sum1,&sum0); 

		Fx_MultVar(poleSec1LessFlag_A[j],ys1less,size,poleSec1FxFact1_A[j],poleSec1FxFact0_A[j],
                                    ys11,ys10,&out1,&out0);
				    
                Fx_AddVar(size,saturation_mode,out1,out0,sum1,sum0,&sum1,&sum0); 

		Fx_MultVar(poleSec2LessFlag_A[j],ys2less,size,poleSec2FxFact1_A[j],poleSec2FxFact0_A[j],
                                    ys21,ys20,&out1,&out0);
		
                Fx_AddVar(size,saturation_mode,out1,out0,sum1,sum0,&sum1,&sum0); 
		

		Fx_RoundVar(size,accumSizeRound,roundoff_bits,sum1,sum0,&ys_A[0]);

		
                for (jj=0; jj<2; jj++)   
                    ycas_A[jt+jj] = ys_A[jj];
          }

          if(IT_OUT(0)) {
			KrnOverflow("casfil",0);
			return(99);
	  }
          //y(0) = ys_A[0]*wnorminv;
	  y(0) = ys_A[0];
          n = n+1;
        
    }
    return(0);




Top

Wrapup Code



 





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://www.xcad.com
    XCAD Corporation
    Raleigh, North Carolina */


Top

Description



 

/* casfil.s */
/***********************************************************************
                             fxcasfil()
************************************************************************
Star implements cascade form IIR digital filter.
Parameter: (file) File with the filter coefficients and parameters
The inputs from the file are as follows;
           ns: Number of sections
           zeroSec_A[i] zeroSec2_A[i] i=1 to ns the numerator coefficients
           poleSec1_A[i] poleSec2_A[i] i=1 to ns the denominator coefficients
           in the Z-domain.
Date:  October 15, 1988 
Programmer: Tulay Adali
Modified: Sasan Ardalan
Modified: Sasan Ardalan, Fixed Point June 17, 2007

fxcasfil


Block implements fixed point cascade form IIR digital filter.
Parameter: (file) File with the filter coefficients and parameters
The inputs from the file are as follows;
           ns: Number of sections
           zeroSec_A[i] zeroSec2_A[i] i=1 to ns the numerator coefficients
           poleSec1_A[i] poleSec2_A[i] i=1 to ns the denominator coefficients
           in the Z-domain.


Date:  October 15, 1988 
Programmer: Tulay Adali
Modified: Sasan Ardalan

*/