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_D16F16Css_TRC_WRA_01_Private.h"
202c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent#include "LVM_Macros.h"
212c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
222c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
232c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent/**************************************************************************
242c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent ASSUMPTIONS:
252c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent COEFS-
262c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->coefs[0] is A2, pBiquadState->coefs[1] is A1
272c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->coefs[2] is A0, pBiquadState->coefs[3] is -B2
282c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->coefs[4] is -B1, these are in Q14 format
292c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
302c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent DELAYS-
312c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[0] is x(n-1)L in Q0 format
322c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[1] is x(n-1)R in Q0 format
332c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[2] is x(n-2)L in Q0 format
342c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[3] is x(n-2)R in Q0 format
352c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[4] is y(n-1)L in Q0 format
362c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[5] is y(n-1)R in Q0 format
372c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[6] is y(n-2)L in Q0 format
382c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent pBiquadState->pDelays[7] is y(n-2)R in Q0 format
392c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent***************************************************************************/
402c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
412c87e9c923b0362fabf8c97ff63997542394c428Eric Laurentvoid BQ_2I_D16F16C14_TRC_WRA_01 ( Biquad_Instance_t       *pInstance,
422c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                  LVM_INT16               *pDataIn,
432c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                  LVM_INT16               *pDataOut,
442c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                                  LVM_INT16               NrSamples)
452c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    {
462c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        LVM_INT32  ynL,ynR;
472c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        LVM_INT16 ii;
482c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        PFilter_State pBiquadState = (PFilter_State) pInstance;
492c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
502c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent         for (ii = NrSamples; ii != 0; ii--)
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 (Q0) ) in Q14
662c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL+=(LVM_INT32)pBiquadState->coefs[3]*pBiquadState->pDelays[6];
672c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
682c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            // ynL+=( -B1 (Q14) * y(n-1)L (Q0) ) in Q14
692c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL+=(LVM_INT32)pBiquadState->coefs[4]*pBiquadState->pDelays[4];
702c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
712c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynL=ynL>>14; // ynL in Q0 format
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 (Q0) ) in Q14
862c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR+=(LVM_INT32)pBiquadState->coefs[3]*pBiquadState->pDelays[7];
872c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
882c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            // ynR+=( -B1 (Q14) * y(n-1)R (Q0) ) in Q14
892c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR+=(LVM_INT32)pBiquadState->coefs[4]*pBiquadState->pDelays[5];
902c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
912c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ynR=ynR>>14; // ynL in Q0 format
922c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /**************************************************************************
932c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                            UPDATING THE DELAYS
942c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ***************************************************************************/
952c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[7]=pBiquadState->pDelays[5];  // y(n-2)R=y(n-1)R
962c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[6]=pBiquadState->pDelays[4];  // y(n-2)L=y(n-1)L
972c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[3]=pBiquadState->pDelays[1];  // x(n-2)R=x(n-1)R
982c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[2]=pBiquadState->pDelays[0];  // x(n-2)L=x(n-1)L
992c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[5]=ynR;                       // Update y(n-1)R in Q0
1002c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[4]=ynL;                       // Update y(n-1)L in Q0
1012c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[0]=(*pDataIn++);              // Update x(n-1)L in Q0
1022c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            pBiquadState->pDelays[1]=(*pDataIn++);              // Update x(n-1)R in Q0
1032c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1042c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            /**************************************************************************
1052c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent                            WRITING THE OUTPUT
1062c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            ***************************************************************************/
1072c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            *pDataOut++=(LVM_INT16)ynL; // Write Left output in Q0
1082c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent            *pDataOut++=(LVM_INT16)ynR; // Write Right ouput in Q0
1092c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1102c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1112c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent        }
1122c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
1132c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent    }
1142c87e9c923b0362fabf8c97ff63997542394c428Eric Laurent
115