Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | |
| 2 | /****************************************************************** |
| 3 | |
| 4 | iLBC Speech Coder ANSI-C Source Code |
| 5 | |
| 6 | filter.c |
| 7 | |
| 8 | Copyright (C) The Internet Society (2004). |
| 9 | All Rights Reserved. |
| 10 | |
| 11 | ******************************************************************/ |
| 12 | |
| 13 | #include "iLBC_define.h" |
| 14 | |
| 15 | /*----------------------------------------------------------------* |
| 16 | * all-pole filter |
| 17 | *---------------------------------------------------------------*/ |
| 18 | |
| 19 | void AllPoleFilter( |
| 20 | float *InOut, /* (i/o) on entrance InOut[-orderCoef] to |
| 21 | InOut[-1] contain the state of the |
| 22 | filter (delayed samples). InOut[0] to |
| 23 | InOut[lengthInOut-1] contain the filter |
| 24 | input, on en exit InOut[-orderCoef] to |
| 25 | InOut[-1] is unchanged and InOut[0] to |
| 26 | InOut[lengthInOut-1] contain filtered |
| 27 | samples */ |
| 28 | float *Coef,/* (i) filter coefficients, Coef[0] is assumed |
| 29 | to be 1.0 */ |
| 30 | int lengthInOut,/* (i) number of input/output samples */ |
| 31 | int orderCoef /* (i) number of filter coefficients */ |
| 32 | ){ |
| 33 | int n,k; |
| 34 | |
| 35 | for(n=0;n<lengthInOut;n++){ |
| 36 | for(k=1;k<=orderCoef;k++){ |
| 37 | *InOut -= Coef[k]*InOut[-k]; |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | } |
| 44 | InOut++; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /*----------------------------------------------------------------* |
| 49 | * all-zero filter |
| 50 | *---------------------------------------------------------------*/ |
| 51 | |
| 52 | void AllZeroFilter( |
| 53 | float *In, /* (i) In[0] to In[lengthInOut-1] contain |
| 54 | filter input samples */ |
| 55 | float *Coef,/* (i) filter coefficients (Coef[0] is assumed |
| 56 | to be 1.0) */ |
| 57 | int lengthInOut,/* (i) number of input/output samples */ |
| 58 | int orderCoef, /* (i) number of filter coefficients */ |
| 59 | float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1] |
| 60 | contain the filter state, on exit Out[0] |
| 61 | to Out[lengthInOut-1] contain filtered |
| 62 | samples */ |
| 63 | ){ |
| 64 | int n,k; |
| 65 | |
| 66 | for(n=0;n<lengthInOut;n++){ |
| 67 | *Out = Coef[0]*In[0]; |
| 68 | for(k=1;k<=orderCoef;k++){ |
| 69 | *Out += Coef[k]*In[-k]; |
| 70 | } |
| 71 | Out++; |
| 72 | In++; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /*----------------------------------------------------------------* |
| 77 | * pole-zero filter |
| 78 | *---------------------------------------------------------------*/ |
| 79 | |
| 80 | void ZeroPoleFilter( |
| 81 | float *In, /* (i) In[0] to In[lengthInOut-1] contain |
| 82 | filter input samples In[-orderCoef] to |
| 83 | In[-1] contain state of all-zero |
| 84 | section */ |
| 85 | float *ZeroCoef,/* (i) filter coefficients for all-zero |
| 86 | section (ZeroCoef[0] is assumed to |
| 87 | be 1.0) */ |
| 88 | float *PoleCoef,/* (i) filter coefficients for all-pole section |
| 89 | (ZeroCoef[0] is assumed to be 1.0) */ |
| 90 | int lengthInOut,/* (i) number of input/output samples */ |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | int orderCoef, /* (i) number of filter coefficients */ |
| 97 | float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1] |
| 98 | contain state of all-pole section. On |
| 99 | exit Out[0] to Out[lengthInOut-1] |
| 100 | contain filtered samples */ |
| 101 | ){ |
| 102 | AllZeroFilter(In,ZeroCoef,lengthInOut,orderCoef,Out); |
| 103 | AllPoleFilter(Out,PoleCoef,lengthInOut,orderCoef); |
| 104 | } |
| 105 | |
| 106 | /*----------------------------------------------------------------* |
| 107 | * downsample (LP filter and decimation) |
| 108 | *---------------------------------------------------------------*/ |
| 109 | |
| 110 | void DownSample ( |
| 111 | float *In, /* (i) input samples */ |
| 112 | float *Coef, /* (i) filter coefficients */ |
| 113 | int lengthIn, /* (i) number of input samples */ |
| 114 | float *state, /* (i) filter state */ |
| 115 | float *Out /* (o) downsampled output */ |
| 116 | ){ |
| 117 | float o; |
| 118 | float *Out_ptr = Out; |
| 119 | float *Coef_ptr, *In_ptr; |
| 120 | float *state_ptr; |
| 121 | int i, j, stop; |
| 122 | |
| 123 | /* LP filter and decimate at the same time */ |
| 124 | |
| 125 | for (i = DELAY_DS; i < lengthIn; i+=FACTOR_DS) |
| 126 | { |
| 127 | Coef_ptr = &Coef[0]; |
| 128 | In_ptr = &In[i]; |
| 129 | state_ptr = &state[FILTERORDER_DS-2]; |
| 130 | |
| 131 | o = (float)0.0; |
| 132 | |
| 133 | stop = (i < FILTERORDER_DS) ? i + 1 : FILTERORDER_DS; |
| 134 | |
| 135 | for (j = 0; j < stop; j++) |
| 136 | { |
| 137 | o += *Coef_ptr++ * (*In_ptr--); |
| 138 | } |
| 139 | for (j = i + 1; j < FILTERORDER_DS; j++) |
| 140 | { |
| 141 | o += *Coef_ptr++ * (*state_ptr--); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | *Out_ptr++ = o; |
| 150 | } |
| 151 | |
| 152 | /* Get the last part (use zeros as input for the future) */ |
| 153 | |
| 154 | for (i=(lengthIn+FACTOR_DS); i<(lengthIn+DELAY_DS); |
| 155 | i+=FACTOR_DS) { |
| 156 | |
| 157 | o=(float)0.0; |
| 158 | |
| 159 | if (i<lengthIn) { |
| 160 | Coef_ptr = &Coef[0]; |
| 161 | In_ptr = &In[i]; |
| 162 | for (j=0; j<FILTERORDER_DS; j++) { |
| 163 | o += *Coef_ptr++ * (*Out_ptr--); |
| 164 | } |
| 165 | } else { |
| 166 | Coef_ptr = &Coef[i-lengthIn]; |
| 167 | In_ptr = &In[lengthIn-1]; |
| 168 | for (j=0; j<FILTERORDER_DS-(i-lengthIn); j++) { |
| 169 | o += *Coef_ptr++ * (*In_ptr--); |
| 170 | } |
| 171 | } |
| 172 | *Out_ptr++ = o; |
| 173 | } |
| 174 | } |
| 175 | |