/*
***********************************************************************
*
* 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:   Pred_lt_3()
 *
 *  PURPOSE:  Compute the result of long term prediction with fractionnal
 *            interpolation of resolution 1/3.
 *
 *  DESCRIPTION:
 *       The past excitation signal at the given delay is interpolated at
 *       the given fraction to build the adaptive codebook excitation.
 *       On return exc[0..L_subfr-1] contains the interpolated signal
 *       (adaptive codebook excitation).
 *
 *************************************************************************/

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

#define UP_SAMP      3
#define L_INTER10   10
#define FIR_SIZE    (UP_SAMP*L_INTER10+1)

/* 1/3 resolution interpolation filter  (-3 dB at 3600 Hz) */

static Word16 inter_3l[FIR_SIZE] = {
    29443,
    25207, 14701, 3143,
    -4402, -5850, -2783,
    1211, 3130, 2259,
    0, -1652, -1666,
    -464, 756, 1099,
    550, -245, -634,
    -451, 0, 308,
    296, 78, -120,
    -165, -79, 34,
    91, 70, 0};


void Pred_lt_3(
    Word16 exc[],  /* in/out: excitation buffer */
    Word16 T0,     /* input : integer pitch lag */
    Word16 frac,   /* input : fraction of lag   */
    Word16 L_subfr /* input : subframe size     */
)
{
    Word16 i, j, k;
    Word16 *x0, *x1, *x2, *c1, *c2;
    Word32 s;

    x0 = &exc[-T0];

    frac = negate(frac);
    if (frac < 0) {
        frac = add(frac, UP_SAMP);
        x0--;
    }
    for (j = 0; j < L_subfr; j++) {
        x1 = x0++;
        x2 = x0;
        c1 = &inter_3l[frac];
        c2 = &inter_3l[sub(UP_SAMP, frac)];

        s = 0;
        for (i = 0, k = 0; i < L_INTER10; i++, k += UP_SAMP) {
            s = L_mac(s, x1[-i], c1[k]);
            s = L_mac(s, x2[i], c2[k]);
        }

        exc[j] = round(s);
    }

    return;
}
