/*
***********************************************************************
*
* 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:  autocorr
 *
 *  PURPOSE:   Compute autocorrelations of signal with windowing
 *
 *  DESCRIPTION:
 *       - Windowing of input speech:   s'(n) = s(n) * w(n)
 *       - Autocorrelations of input speech:
 *             r(k) = sum_{i=k}^{239} s'(i)*s'(i-k)    k=0,...,10
 *         The autocorrelations are expressed in normalized double precision
 *         format.
 *
 *************************************************************************/

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


void Autocorr(
     Word16 x[],        /* (i)    : Input signal                      */
     Word16 m,          /* (i)    : LPC order                         */
     Word16 r_h[],      /* (o)    : Autocorrelations  (msb)           */
     Word16 r_l[],      /* (o)    : Autocorrelations  (lsb)           */
     Word16 wind[]      /* (i)    : window for LPC analysis.          */
)
{
    Word16 i, j, norm;
    Word16 y[L_WINDOW];
    Word32 sum;

    extern Flag Overflow;

    /* Windowing of signal */

    for (i = 0; i < L_WINDOW; i++) {
        y[i] = mult_r(x[i], wind[i]);
    }
    /* Compute r[0] and test for overflow */

    do {
        Overflow = 0;
        sum = 1;    /* Avoid case of all zeros */

        for (i = 0; i < L_WINDOW; i++)
            sum = L_mac(sum, y[i], y[i]);

        /* If overflow divide y[] by 4 */

        if (Overflow != 0) {
            for (i = 0; i < L_WINDOW; i++) {
                y[i] = shr(y[i], 2);
            }
        }
    }
    while (Overflow != 0);

    /* Normalization of r[0] */

    norm = norm_l(sum);
    sum = L_shl(sum, norm);
    L_Extract(sum, &r_h[0], &r_l[0]);   /* Put in DPF format (see oper_32b) */

    /* r[1] to r[m] */

    for (i = 1; i <= m; i++) {
        sum = 0;
        for (j = 0; j < L_WINDOW - i; j++)
            sum = L_mac(sum, y[j], y[j + i]);

        sum = L_shl(sum, norm);
        L_Extract(sum, &r_h[i], &r_l[i]);
    }

    return;
}
