/*
***********************************************************************
*
* Copyright (C) 1999 Ericsson Radio Systems AB
* 
* The Association of Radio Industries and Businesses (ARIB) has the
* licence to reproduce, distribute and publish the Bit Exact C Code
* programs in this CD-ROM together with the other parts of the RCR
* STD-27 (Personal Digital Cellular Telecommunication System)
* specification.
* 
* Ericsson has declared Option 1 (royalty-free licence) to ARIB for
* its Essential Industrial Property Rights related to the ACELP
* specification and the Bit Exact C Code programs. The royalty-free
* licence is limited to the development and manufacturing of
* products complying with the RCR STD-27H specification (or
* subsequent versions) for sale and use in Japan. Copying of the
* program(s) for any other purpose requires the prior written
* permission from Ericsson.
* 
* The user of the Bit Exact C Code hereby acknowledges the
* following:
* 
* 1.  Ericsson does not provide the user with any warranties,
*     whether express or implied, including but not limited to
*     warranties of merchantability and fitness for a particular
*     purpose relating to the Bit Exact C Code.
* 
* 2.  Ericsson shall have no responsibility whatsoever to indemnify
*     the user against actions or claims for infringement of any
*     intellectual property rights, e.g. patents, copyrights,
*     registered designs or any other rights, by reason of the
*     user's use of the Bit Exact C Code.
* 
***********************************************************************
*/

/*************************************************************************
 *
 *  FUNCTION:  Syn_filt:
 *
 *  PURPOSE:  Perform synthesis filtering through 1/A(z).
 *
 *************************************************************************/

#include "typedef.h"
#include "basic_op.h"
#include "cnst.h"


void Syn_filt(
    Word16 a[],   /* (i)  : a[m+1] prediction coefficients   (m=10)  */
    Word16 x[],   /* (i)  : input signal                             */
    Word16 y[],   /* (o)  : output signal                            */
    Word16 lg,    /* (i)  : size of filtering                        */
    Word16 mem[], /* (i/o): memory associated with this filtering.   */
    Word16 update /* (i)  : 0=no update, 1=update of memory.         */
)
{
    Word16 i, j;
    Word32 s;
    Word16 tmp[80];    /* This is usually done by memory allocation (lg+m) */
    Word16 *yy;

    /* Copy mem[] to yy[] */

    yy = tmp;

    for (i = 0; i < M; i++) {
        *yy++ = mem[i];
    }

    /* Do the filtering. */

    for (i = 0; i < lg; i++) {
        s = L_mult(x[i], a[0]);
        for (j = 1; j <= M; j++)
            s = L_msu(s, a[j], yy[-j]);

        s = L_shl(s, 3);
        *yy++ = round(s);
    }

    for (i = 0; i < lg; i++) {
        y[i] = tmp[i + M];
    }

    /* Update of memory if update==1 */

    if (update != 0)
        for (i = 0; i < M; i++) {
            mem[i] = y[lg - M + i];
        }
    return;
}
