1 /*
2  * Copyright (C) 2004-2010 NXP Software
3  * Copyright (C) 2010 The Android Open Source Project
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 #include "LVM_Types.h"
19 #include "LVM_Macros.h"
20 #include "ScalarArithmetic.h"
21 #include "BIQUAD.h"
22 #include "Filter.h"
23 
24 /*-------------------------------------------------------------------------*/
25 /* FUNCTION:                                                               */
26 /*   void LVM_FO_LPF(   LVM_INT32       w ,                                */
27 /*                      FO_C32_Coefs_t  *pCoeffs);                         */
28 /*                                                                         */
29 /*                                                                         */
30 /* DESCRIPTION:                                                            */
31 /*    This function calculates the coefficient of first order low pass     */
32 /*    filter. It uses the equations:                                       */
33 /*                                                                         */
34 /*    B1    = (tan(w/2) - 1 )  /  (tan(w/2) + 1 )                          */
35 /*    A0    = (1 - B1) / 2                                                 */
36 /*    A1    = A0                                                           */
37 /*                                                                         */
38 /*    The value of B1 is then calculated directly from the value w by a    */
39 /*    polynomial expansion using a 9th order polynomial. It uses the       */
40 /*    following table of 32-bit integer polynomial coefficients:           */
41 /*                                                                         */
42 /*   Coefficient    Value                                                  */
43 /*   A0             -8388571                                               */
44 /*   A1             33547744                                               */
45 /*   A2             -66816791                                              */
46 /*   A3             173375308                                              */
47 /*   A4             -388437573                                             */
48 /*   A5             752975383                                              */
49 /*   A6             -1103016663                                            */
50 /*   A7             1121848567                                             */
51 /*   A8             -688078159                                             */
52 /*   A9             194669577                                              */
53 /*   A10            8                                                      */
54 /*                                                                         */
55 /*  Y = (A0 + A1*X + A2*X2 + A3*X3 + �.. + AN*xN) << AN+1                  */
56 /*                                                                         */
57 /*                                                                         */
58 /* PARAMETERS:                                                             */
59 /*                                                                         */
60 /*  w               Sample rate in radians,  where:                        */
61 /*                  w = 2 * Pi * Fc / Fs                                   */
62 /*                  Fc   is the corner frequency in Hz                     */
63 /*                  Fs   is the sample rate in Hz                          */
64 /*                  w is in Q2.29 format and data range is [0 Pi]          */
65 /*  pCoeffs         Points to the filter coefficients calculated here      */
66 /*                  in Q1.30 format                                        */
67 /* RETURNS:                                                                */
68 /*                                                                         */
69 /*-------------------------------------------------------------------------*/
LVM_FO_HPF(LVM_FLOAT w,FO_FLOAT_Coefs_t * pCoeffs)70 LVM_FLOAT LVM_FO_HPF(   LVM_FLOAT       w,
71                         FO_FLOAT_Coefs_t  *pCoeffs)
72 {
73     LVM_FLOAT Y,Coefficients[13] = {-0.999996f,
74                                     0.999801f,
75                                     -0.497824f,
76                                     0.322937f,
77                                     -0.180880f,
78                                     0.087658f,
79                                     -0.032102f,
80                                     0.008163f,
81                                     -0.001252f,
82                                     0.000089f,
83                                     0,
84                                     0,
85                                     0};
86     Y=LVM_Polynomial((LVM_UINT16)9, Coefficients, w);
87 
88     pCoeffs->B1 = -Y;         /* Store -B1 in filter structure instead of B1!*/
89                             /* A0=(1-B1)/2= B1/2 - 0.5*/
90     Y = Y / 2.0f;                 /* A0=Y=B1/2*/
91     Y = Y - 0.5f;         /* A0=Y=(B1/2 - 0.5)*/
92 
93     pCoeffs->A0 = Y * FILTER_LOSS_FLOAT;                  /* Apply loss to avoid overflow*/
94     pCoeffs->A1 = -pCoeffs->A0;                           /* Store A1=-A0*/
95 
96     return 1;
97 }
98