2 * Copyright (C) 2004-2010 NXP Software
3 * Copyright (C) 2010 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 /****************************************************************************************/
22 /****************************************************************************************/
24 #include "LVEQNB_Private.h"
27 /****************************************************************************************/
31 /****************************************************************************************/
33 #define PI 3.14159265358979
35 /****************************************************************************************/
37 /* FUNCTION: LVEQNB_DoublePrecCoefs */
40 /* Calculate double precision coefficients for a peaking filter */
43 /* Fs Sampling frequency index */
44 /* pFilterDefinition Pointer to the filter definition */
45 /* pCoefficients Pointer to the coefficients */
48 /* LVEQNB_SUCCESS Always succeeds */
51 /* 1. The equations used are as follows: */
53 /* G = 10^(GaindB/20) - 1 */
54 /* t0 = 2 * Pi * Fc / Fs */
55 /* D = 1 if GaindB >= 0 */
56 /* D = 1 / (1 + G) if GaindB < 0 */
58 /* b2 = -0.5 * (2Q - D * t0) / (2Q + D * t0) */
59 /* b1 = (0.5 - b2) * (1 - coserr(t0)) */
60 /* a0 = (0.5 + b2) / 2 */
63 /* GaindB is the gain in dBs, range -15dB to +15dB */
64 /* Fc is the centre frequency, DC to Fs/50 */
65 /* Fs is the sample frequency, 8000 to 48000 in descrete steps */
66 /* Q is the Q factor, 0.25 to 12 (represented by 25 to 1200) */
68 /* 2. The double precision coefficients are only used when fc is less than fs/85, so */
69 /* the cosine of t0 is always close to 1.0. Instead of calculating the cosine */
70 /* itself the difference from the value 1.0 is calculated, this can be done with */
71 /* lower precision maths. */
73 /* 3. The value of the B2 coefficient is only calculated as a single precision value, */
74 /* small errors in this value have a combined effect on the Q and Gain but not the */
75 /* the frequency of the filter. */
77 /****************************************************************************************/
80 LVEQNB_ReturnStatus_en LVEQNB_DoublePrecCoefs(LVM_UINT16 Fs,
81 LVEQNB_BandDef_t *pFilterDefinition,
82 PK_C32_Coefs_t *pCoefficients)
85 extern LVM_INT16 LVEQNB_GainTable[];
86 extern LVM_INT16 LVEQNB_TwoPiOnFsTable[];
87 extern LVM_INT16 LVEQNB_DTable[];
88 extern LVM_INT16 LVEQNB_DPCosCoef[];
91 * Get the filter definition
93 LVM_INT16 Gain = pFilterDefinition->Gain;
94 LVM_UINT16 Frequency = pFilterDefinition->Frequency;
95 LVM_UINT16 QFactor = pFilterDefinition->QFactor;
98 * Intermediate variables and temporary values
115 * Calculating the intermediate values
117 T0 = (LVM_INT32)Frequency * LVEQNB_TwoPiOnFsTable[Fs]; /* T0 = 2 * Pi * Fc / Fs */
120 D = LVEQNB_DTable[15]; /* D = 1 if GaindB >= 0 */
124 D = LVEQNB_DTable[Gain+15]; /* D = 1 / (1 + G) if GaindB < 0 */
128 * Calculate the B2 coefficient
130 Dt0 = D * (T0 >> 10);
131 B2_Den = ((LVM_INT32)QFactor << 19) + (Dt0 >> 2);
132 B2_Num = (Dt0 >> 3) - ((LVM_INT32)QFactor << 18);
133 B2 = (B2_Num / (B2_Den >> 16)) << 15;
136 * Calculate the cosine error by a polynomial expansion using the equation:
138 * CosErr += coef(n) * t0^n For n = 0 to 4
140 T0 = (T0 >> 6) * 0x7f53; /* Scale to 1.0 in 16-bit for range 0 to fs/50 */
141 t0 = (LVM_INT16)(T0 >> 16);
142 factor = 0x7fff; /* Initialise to 1.0 for the a0 coefficient */
143 CosErr = 0; /* Initialise the error to zero */
146 coef = LVEQNB_DPCosCoef[i]; /* Get the nth coefficient */
147 CosErr += (factor * coef) >> 5; /* The nth partial sum */
148 factor = (factor * t0) >> 15; /* Calculate t0^n */
150 CosErr = CosErr << (LVEQNB_DPCosCoef[0]); /* Correct the scaling */
153 * Calculate the B1 and A0 coefficients
155 B1 = (0x40000000 - B2); /* B1 = (0.5 - b2/2) */
156 A0 = ((B1 >> 16) * (CosErr >> 10)) >> 6; /* Temporary storage for (0.5 - b2/2) * coserr(t0) */
157 B1 -= A0; /* B1 = (0.5 - b2/2) * (1 - coserr(t0)) */
158 A0 = (0x40000000 + B2) >> 1; /* A0 = (0.5 + b2) */
161 * Write coeff into the data structure
163 pCoefficients->A0 = A0;
164 pCoefficients->B1 = B1;
165 pCoefficients->B2 = B2;
166 pCoefficients->G = LVEQNB_GainTable[Gain+15];
168 return(LVEQNB_SUCCESS);
173 /****************************************************************************************/
175 /* FUNCTION: LVEQNB_SinglePrecCoefs */
178 /* Calculate single precision coefficients for a peaking filter */
181 /* Fs Sampling frequency index */
182 /* pFilterDefinition Pointer to the filter definition */
183 /* pCoefficients Pointer to the coefficients */
186 /* LVEQNB_SUCCESS Always succeeds */
189 /* 1. The equations used are as follows: */
191 /* G = 10^(GaindB/20) - 1 */
192 /* t0 = 2 * Pi * Fc / Fs */
193 /* D = 1 if GaindB >= 0 */
194 /* D = 1 / (1 + G) if GaindB < 0 */
196 /* b2 = -0.5 * (2Q - D * t0) / (2Q + D * t0) */
197 /* b1 = (0.5 - b2) * cos(t0) */
198 /* a0 = (0.5 + b2) / 2 */
201 /* GaindB is the gain in dBs, range -15dB to +15dB */
202 /* Fc is the centre frequency, DC to Nyquist */
203 /* Fs is the sample frequency, 8000 to 48000 in descrete steps */
204 /* Q is the Q factor, 0.25 to 12 */
206 /****************************************************************************************/
209 LVEQNB_ReturnStatus_en LVEQNB_SinglePrecCoefs(LVM_UINT16 Fs,
210 LVEQNB_BandDef_t *pFilterDefinition,
211 PK_C16_Coefs_t *pCoefficients)
214 extern LVM_INT16 LVEQNB_GainTable[];
215 extern LVM_INT16 LVEQNB_TwoPiOnFsTable[];
216 extern LVM_INT16 LVEQNB_DTable[];
217 extern LVM_INT16 LVEQNB_CosCoef[];
221 * Get the filter definition
223 LVM_INT16 Gain = pFilterDefinition->Gain;
224 LVM_UINT16 Frequency = pFilterDefinition->Frequency;
225 LVM_UINT16 QFactor = pFilterDefinition->QFactor;
229 * Intermediate variables and temporary values
246 * Calculating the intermediate values
248 T0 = (LVM_INT32)Frequency * LVEQNB_TwoPiOnFsTable[Fs]; /* T0 = 2 * Pi * Fc / Fs */
251 D = LVEQNB_DTable[15]; /* D = 1 if GaindB >= 0 */
255 D = LVEQNB_DTable[Gain+15]; /* D = 1 / (1 + G) if GaindB < 0 */
259 * Calculate the B2 coefficient
261 Dt0 = D * (T0 >> 10);
262 B2_Den = ((LVM_INT32)QFactor << 19) + (Dt0 >> 2);
263 B2_Num = (Dt0 >> 3) - ((LVM_INT32)QFactor << 18);
264 B2 = (B2_Num / (B2_Den >> 16)) << 15;
267 * Calculate the cosine by a polynomial expansion using the equation:
269 * Cos += coef(n) * t0^n For n = 0 to 6
271 T0 = (T0 >> 10) * 20859; /* Scale to 1.0 in 16-bit for range 0 to fs/2 */
272 t0 = (LVM_INT16)(T0 >> 16);
273 factor = 0x7fff; /* Initialise to 1.0 for the a0 coefficient */
274 COS_T0 = 0; /* Initialise the error to zero */
277 coef = LVEQNB_CosCoef[i]; /* Get the nth coefficient */
278 COS_T0 += (factor * coef) >> 5; /* The nth partial sum */
279 factor = (factor * t0) >> 15; /* Calculate t0^n */
281 COS_T0 = COS_T0 << (LVEQNB_CosCoef[0]+6); /* Correct the scaling */
284 B1 = ((0x40000000 - B2) >> 16) * (COS_T0 >> 16); /* B1 = (0.5 - b2/2) * cos(t0) */
285 A0 = (0x40000000 + B2) >> 1; /* A0 = (0.5 + b2/2) */
288 * Write coeff into the data structure
290 pCoefficients->A0 = (LVM_INT16)(A0>>16);
291 pCoefficients->B1 = (LVM_INT16)(B1>>15);
292 pCoefficients->B2 = (LVM_INT16)(B2>>16);
293 pCoefficients->G = LVEQNB_GainTable[Gain+15];
296 return(LVEQNB_SUCCESS);