2
|
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(float *InOut, /* (i/o) on entrance InOut[-orderCoef] to
|
|
20 InOut[-1] contain the state of the
|
|
21 filter (delayed samples). InOut[0] to
|
|
22 InOut[lengthInOut-1] contain the filter
|
|
23 input, on en exit InOut[-orderCoef] to
|
|
24 InOut[-1] is unchanged and InOut[0] to
|
|
25 InOut[lengthInOut-1] contain filtered
|
|
26 samples */
|
|
27 float *Coef, /* (i) filter coefficients, Coef[0] is assumed
|
|
28 to be 1.0 */
|
|
29 int lengthInOut, /* (i) number of input/output samples */
|
|
30 int orderCoef /* (i) number of filter coefficients */
|
|
31 )
|
|
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(float *In, /* (i) In[0] to In[lengthInOut-1] contain
|
|
53 filter input samples */
|
|
54 float *Coef, /* (i) filter coefficients (Coef[0] is assumed
|
|
55 to be 1.0) */
|
|
56 int lengthInOut, /* (i) number of input/output samples */
|
|
57 int orderCoef, /* (i) number of filter coefficients */
|
|
58 float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
|
|
59 contain the filter state, on exit Out[0]
|
|
60 to Out[lengthInOut-1] contain filtered
|
|
61 samples */
|
|
62 )
|
|
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(float *In, /* (i) In[0] to In[lengthInOut-1] contain
|
|
81 filter input samples In[-orderCoef] to
|
|
82 In[-1] contain state of all-zero
|
|
83 section */
|
|
84 float *ZeroCoef, /* (i) filter coefficients for all-zero
|
|
85 section (ZeroCoef[0] is assumed to
|
|
86 be 1.0) */
|
|
87 float *PoleCoef, /* (i) filter coefficients for all-pole section
|
|
88 (ZeroCoef[0] is assumed to be 1.0) */
|
|
89 int lengthInOut, /* (i) number of input/output samples */
|
|
90 int orderCoef, /* (i) number of filter coefficients */
|
|
91 float *Out /* (i/o) on entrance Out[-orderCoef] to Out[-1]
|
|
92 contain state of all-pole section. On
|
|
93 exit Out[0] to Out[lengthInOut-1]
|
|
94 contain filtered samples */
|
|
95 )
|
|
96 {
|
|
97 AllZeroFilter(In, ZeroCoef, lengthInOut, orderCoef, Out);
|
|
98 AllPoleFilter(Out, PoleCoef, lengthInOut, orderCoef);
|
|
99 }
|
|
100
|
|
101 /*----------------------------------------------------------------*
|
|
102 * downsample (LP filter and decimation)
|
|
103 *---------------------------------------------------------------*/
|
|
104
|
|
105 void DownSample(float *In, /* (i) input samples */
|
|
106 float *Coef, /* (i) filter coefficients */
|
|
107 int lengthIn, /* (i) number of input samples */
|
|
108 float *state, /* (i) filter state */
|
|
109 float *Out /* (o) downsampled output */
|
|
110 )
|
|
111 {
|
|
112 float o;
|
|
113 float *Out_ptr = Out;
|
|
114 float *Coef_ptr, *In_ptr;
|
|
115 float *state_ptr;
|
|
116 int i, j, stop;
|
|
117
|
|
118 /* LP filter and decimate at the same time */
|
|
119
|
|
120 for (i = DELAY_DS; i < lengthIn; i += FACTOR_DS) {
|
|
121 Coef_ptr = &Coef[0];
|
|
122 In_ptr = &In[i];
|
|
123 state_ptr = &state[FILTERORDER_DS - 2];
|
|
124
|
|
125 o = (float) 0.0;
|
|
126
|
|
127 stop = (i < FILTERORDER_DS) ? i + 1 : FILTERORDER_DS;
|
|
128
|
|
129 for (j = 0; j < stop; j++) {
|
|
130 o += *Coef_ptr++ * (*In_ptr--);
|
|
131 }
|
|
132 for (j = i + 1; j < FILTERORDER_DS; j++) {
|
|
133 o += *Coef_ptr++ * (*state_ptr--);
|
|
134 }
|
|
135
|
|
136
|
|
137
|
|
138
|
|
139
|
|
140
|
|
141 *Out_ptr++ = o;
|
|
142 }
|
|
143
|
|
144 /* Get the last part (use zeros as input for the future) */
|
|
145
|
|
146 for (i = (lengthIn + FACTOR_DS); i < (lengthIn + DELAY_DS);
|
|
147 i += FACTOR_DS) {
|
|
148
|
|
149 o = (float) 0.0;
|
|
150
|
|
151 if (i < lengthIn) {
|
|
152 Coef_ptr = &Coef[0];
|
|
153 In_ptr = &In[i];
|
|
154 for (j = 0; j < FILTERORDER_DS; j++) {
|
|
155 o += *Coef_ptr++ * (*Out_ptr--);
|
|
156 }
|
|
157 } else {
|
|
158 Coef_ptr = &Coef[i - lengthIn];
|
|
159 In_ptr = &In[lengthIn - 1];
|
|
160 for (j = 0; j < FILTERORDER_DS - (i - lengthIn); j++) {
|
|
161 o += *Coef_ptr++ * (*In_ptr--);
|
|
162 }
|
|
163 }
|
|
164 *Out_ptr++ = o;
|
|
165 }
|
|
166 }
|