12c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/*
22c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Copyright (C) 2004-2010 NXP Software
32c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Copyright (C) 2010 The Android Open Source Project
42c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *
52c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
62c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * you may not use this file except in compliance with the License.
72c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * You may obtain a copy of the License at
82c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *
92c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
102c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent *
112c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * Unless required by applicable law or agreed to in writing, software
122c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
132c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
142c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * See the License for the specific language governing permissions and
152c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent * limitations under the License.
162c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent */
172c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
182c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent#include "BIQUAD.h"
192c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent#include "BQ_2I_D16F32Css_TRC_WRA_01_Private.h"
202c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent#include "LVM_Macros.h"
212c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
222c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/**************************************************************************
232c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent ASSUMPTIONS:
242c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent COEFS-
252c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->coefs[0] is A2, pBiquadState->coefs[1] is A1
262c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->coefs[2] is A0, pBiquadState->coefs[3] is -B2
272c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->coefs[4] is -B1, these are in Q14 format
282c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
292c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent DELAYS-
302c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[0] is x(n-1)L in Q0 format
312c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[1] is x(n-1)R in Q0 format
322c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[2] is x(n-2)L in Q0 format
332c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[3] is x(n-2)R in Q0 format
342c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[4] is y(n-1)L in Q16 format
352c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[5] is y(n-1)R in Q16 format
362c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[6] is y(n-2)L in Q16 format
372c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[7] is y(n-2)R in Q16 format
382c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent***************************************************************************/
392c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
402c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid BQ_2I_D16F32C14_TRC_WRA_01 (           Biquad_Instance_t       *pInstance,
412c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                            LVM_INT16                    *pDataIn,
422c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                            LVM_INT16                    *pDataOut,
432c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                            LVM_INT16                    NrSamples)
442c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    {
452c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        LVM_INT32  ynL,ynR,templ;
462c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        LVM_INT16 ii;
472c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        PFilter_State pBiquadState = (PFilter_State) pInstance;
482c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
492c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        for (ii = NrSamples; ii != 0; ii--)
502c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        {
512c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
522c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
532c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /**************************************************************************
542c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                            PROCESSING OF THE LEFT CHANNEL
552c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ***************************************************************************/
562c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynL=A2 (Q14) * x(n-2)L (Q0) in Q14*/
572c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL=(LVM_INT32)pBiquadState->coefs[0]* pBiquadState->pDelays[2];
582c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
592c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynL+=A1 (Q14) * x(n-1)L (Q0) in Q14*/
602c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL+=(LVM_INT32)pBiquadState->coefs[1]* pBiquadState->pDelays[0];
612c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
622c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynL+=A0 (Q14) * x(n)L (Q0) in Q14*/
632c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL+=(LVM_INT32)pBiquadState->coefs[2]* (*pDataIn);
642c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
652c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynL+= ( (-B2 (Q14) * y(n-2)L (Q16) )>>16) in Q14 */
662c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            MUL32x16INTO32(pBiquadState->pDelays[6],pBiquadState->coefs[3],templ,16)
672c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL+=templ;
682c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
692c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynL+=( (-B1 (Q14) * y(n-1)L (Q16) )>>16) in Q14 */
702c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            MUL32x16INTO32(pBiquadState->pDelays[4],pBiquadState->coefs[4],templ,16)
712c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL+=templ;
722c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
732c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /**************************************************************************
742c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                            PROCESSING OF THE RIGHT CHANNEL
752c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ***************************************************************************/
762c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynR=A2 (Q14) * x(n-2)R (Q0) in Q14*/
772c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR=(LVM_INT32)pBiquadState->coefs[0]*pBiquadState->pDelays[3];
782c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
792c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynR+=A1 (Q14) * x(n-1)R (Q0) in Q14*/
802c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR+=(LVM_INT32)pBiquadState->coefs[1]*pBiquadState->pDelays[1];
812c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
822c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynR+=A0 (Q14) * x(n)R (Q0) in Q14*/
832c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR+=(LVM_INT32)pBiquadState->coefs[2]*(*(pDataIn+1));
842c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
852c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynR+= ( (-B2 (Q14) * y(n-2)R (Q16) )>>16) in Q14*/
862c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            MUL32x16INTO32(pBiquadState->pDelays[7],pBiquadState->coefs[3],templ,16)
872c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR+=templ;
882c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
892c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /* ynR+=( (-B1 (Q14) * y(n-1)R (Q16) )>>16) in Q14 */
902c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            MUL32x16INTO32(pBiquadState->pDelays[5],pBiquadState->coefs[4],templ,16)
912c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR+=templ;
922c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
932c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /**************************************************************************
942c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                            UPDATING THE DELAYS
952c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ***************************************************************************/
962c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[7]=pBiquadState->pDelays[5];  /* y(n-2)R=y(n-1)R*/
972c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[6]=pBiquadState->pDelays[4];  /* y(n-2)L=y(n-1)L*/
982c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[3]=pBiquadState->pDelays[1];  /* x(n-2)R=x(n-1)R*/
992c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[2]=pBiquadState->pDelays[0];  /* x(n-2)L=x(n-1)L*/
1002c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[5]=ynR<<2;                    /* Update y(n-1)R in Q16*/
1012c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[4]=ynL<<2;                    /* Update y(n-1)L in Q16*/
1022c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[0]=(*pDataIn);                /* Update x(n-1)L in Q0*/
1032c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pDataIn++;
1042c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[1]=(*pDataIn);                /* Update x(n-1)R in Q0*/
1052c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pDataIn++;
1062c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1072c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /**************************************************************************
1082c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                            WRITING THE OUTPUT
1092c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ***************************************************************************/
1102c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            *pDataOut=(LVM_INT16)(ynL>>14); /* Write Left output in Q0*/
1112c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pDataOut++;
1122c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            *pDataOut=(LVM_INT16)(ynR>>14); /* Write Right ouput in Q0*/
1132c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pDataOut++;
1142c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        }
1152c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1162c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    }
1172c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
118