filter.c

Go to the documentation of this file.
00001 
00002    /******************************************************************
00003 
00004        iLBC Speech Coder ANSI-C Source Code
00005 
00006        filter.c
00007 
00008        Copyright (C) The Internet Society (2004).
00009        All Rights Reserved.
00010 
00011    ******************************************************************/
00012 
00013    #include "iLBC_define.h"
00014 
00015    /*----------------------------------------------------------------*
00016     *  all-pole filter
00017     *---------------------------------------------------------------*/
00018 
00019    void AllPoleFilter(
00020        float *InOut,   /* (i/o) on entrance InOut[-orderCoef] to
00021                               InOut[-1] contain the state of the
00022                               filter (delayed samples). InOut[0] to
00023                               InOut[lengthInOut-1] contain the filter
00024                               input, on en exit InOut[-orderCoef] to
00025                               InOut[-1] is unchanged and InOut[0] to
00026                               InOut[lengthInOut-1] contain filtered
00027                               samples */
00028        float *Coef,/* (i) filter coefficients, Coef[0] is assumed
00029                               to be 1.0 */
00030        int lengthInOut,/* (i) number of input/output samples */
00031        int orderCoef   /* (i) number of filter coefficients */
00032    ){
00033        int n,k;
00034 
00035        for(n=0;n<lengthInOut;n++){
00036            for(k=1;k<=orderCoef;k++){
00037                *InOut -= Coef[k]*InOut[-k];
00038 
00039 
00040 
00041 
00042 
00043            }
00044            InOut++;
00045        }
00046    }
00047 
00048    /*----------------------------------------------------------------*
00049     *  all-zero filter
00050     *---------------------------------------------------------------*/
00051 
00052    void AllZeroFilter(
00053        float *In,      /* (i) In[0] to In[lengthInOut-1] contain
00054                               filter input samples */
00055        float *Coef,/* (i) filter coefficients (Coef[0] is assumed
00056                               to be 1.0) */
00057        int lengthInOut,/* (i) number of input/output samples */
00058        int orderCoef,  /* (i) number of filter coefficients */
00059        float *Out      /* (i/o) on entrance Out[-orderCoef] to Out[-1]
00060                               contain the filter state, on exit Out[0]
00061                               to Out[lengthInOut-1] contain filtered
00062                               samples */
00063    ){
00064        int n,k;
00065 
00066        for(n=0;n<lengthInOut;n++){
00067            *Out = Coef[0]*In[0];
00068            for(k=1;k<=orderCoef;k++){
00069                *Out += Coef[k]*In[-k];
00070            }
00071            Out++;
00072            In++;
00073        }
00074    }
00075 
00076    /*----------------------------------------------------------------*
00077     *  pole-zero filter
00078     *---------------------------------------------------------------*/
00079 
00080    void ZeroPoleFilter(
00081        float *In,      /* (i) In[0] to In[lengthInOut-1] contain
00082                               filter input samples In[-orderCoef] to
00083                               In[-1] contain state of all-zero
00084                               section */
00085        float *ZeroCoef,/* (i) filter coefficients for all-zero
00086                               section (ZeroCoef[0] is assumed to
00087                               be 1.0) */
00088        float *PoleCoef,/* (i) filter coefficients for all-pole section
00089                               (ZeroCoef[0] is assumed to be 1.0) */
00090        int lengthInOut,/* (i) number of input/output samples */
00091 
00092 
00093 
00094 
00095 
00096        int orderCoef,  /* (i) number of filter coefficients */
00097        float *Out      /* (i/o) on entrance Out[-orderCoef] to Out[-1]
00098                               contain state of all-pole section. On
00099                               exit Out[0] to Out[lengthInOut-1]
00100                               contain filtered samples */
00101    ){
00102        AllZeroFilter(In,ZeroCoef,lengthInOut,orderCoef,Out);
00103        AllPoleFilter(Out,PoleCoef,lengthInOut,orderCoef);
00104    }
00105 
00106    /*----------------------------------------------------------------*
00107     * downsample (LP filter and decimation)
00108     *---------------------------------------------------------------*/
00109 
00110    void DownSample (
00111        float  *In,     /* (i) input samples */
00112        float  *Coef,   /* (i) filter coefficients */
00113        int lengthIn,   /* (i) number of input samples */
00114        float  *state,  /* (i) filter state */
00115        float  *Out     /* (o) downsampled output */
00116    ){
00117        float   o;
00118        float *Out_ptr = Out;
00119        float *Coef_ptr, *In_ptr;
00120        float *state_ptr;
00121        int i, j, stop;
00122 
00123        /* LP filter and decimate at the same time */
00124 
00125        for (i = DELAY_DS; i < lengthIn; i+=FACTOR_DS)
00126        {
00127            Coef_ptr = &Coef[0];
00128            In_ptr = &In[i];
00129            state_ptr = &state[FILTERORDER_DS-2];
00130 
00131            o = (float)0.0;
00132 
00133            stop = (i < FILTERORDER_DS) ? i + 1 : FILTERORDER_DS;
00134 
00135            for (j = 0; j < stop; j++)
00136            {
00137                o += *Coef_ptr++ * (*In_ptr--);
00138            }
00139            for (j = i + 1; j < FILTERORDER_DS; j++)
00140            {
00141                o += *Coef_ptr++ * (*state_ptr--);
00142            }
00143 
00144 
00145 
00146 
00147 
00148 
00149            *Out_ptr++ = o;
00150        }
00151 
00152        /* Get the last part (use zeros as input for the future) */
00153 
00154        for (i=(lengthIn+FACTOR_DS); i<(lengthIn+DELAY_DS);
00155                i+=FACTOR_DS) {
00156 
00157            o=(float)0.0;
00158 
00159            if (i<lengthIn) {
00160                Coef_ptr = &Coef[0];
00161                In_ptr = &In[i];
00162                for (j=0; j<FILTERORDER_DS; j++) {
00163                        o += *Coef_ptr++ * (*Out_ptr--);
00164                }
00165            } else {
00166                Coef_ptr = &Coef[i-lengthIn];
00167                In_ptr = &In[lengthIn-1];
00168                for (j=0; j<FILTERORDER_DS-(i-lengthIn); j++) {
00169                        o += *Coef_ptr++ * (*In_ptr--);
00170                }
00171            }
00172            *Out_ptr++ = o;
00173        }
00174    }
00175 

Generated on Thu Apr 16 06:27:34 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6