vpredictor.c 2.81 KB
#include "vadpcm.h"

int
readcodebook(FILE *fhandle, int ****table, int *order, int *npredictors)

{
    int **table_entry;
    int i, j, k;
    
    fscanf(fhandle, "%d",order);
    fscanf(fhandle, "%d",npredictors);

    *table = (int ***) malloc(*npredictors*sizeof(int **));
    for (i=0; i < *npredictors; i++){
        (*table)[i] = (int **) malloc(VECTORSIZE*sizeof(int *));
        for (j=0; j<VECTORSIZE; j++)
            (*table)[i][j] = (int *) malloc((VECTORSIZE + *order)*sizeof(int));
    }
    for (i=0; i < *npredictors; i++){
        table_entry = (*table)[i];
        for (j=0; j < *order; j++)
            for (k=0; k<VECTORSIZE; k++)
                fscanf(fhandle,"%d",&table_entry[k][j]);
        /* Now copy the right stuff to the rest of the matrix */
        for (k=1; k<VECTORSIZE; k++)
            table_entry[k][*order] = table_entry[k-1][*order-1];
        table_entry[0][*order] = VSCALING;
        for (k=1; k<VECTORSIZE; k++){
            for (j=0; j<k; j++)
                table_entry[j][k + *order] = 0.0;
            for (; j<VECTORSIZE; j++)
                table_entry[j][k + *order] = table_entry[j-k][*order];
        }
    }
    return 0;
}

int
readaifccodebook(FILE *fhandle, int ****table, short *order, short *npredictors)

{
    int **table_entry;
    int i, j, k;
    short ts;
    
    fread(order, sizeof(short), 1, fhandle);
    fread(npredictors, sizeof(short), 1, fhandle);
    *order = htons(*order);
    *npredictors = htons(*npredictors);

    *table = (int ***) malloc(*npredictors*sizeof(int **));
    for (i=0; i < *npredictors; i++){
        (*table)[i] = (int **) malloc(VECTORSIZE*sizeof(int *));
        for (j=0; j<VECTORSIZE; j++)
            (*table)[i][j] = (int *) malloc((VECTORSIZE + *order)*sizeof(int));
    }
    for (i=0; i < *npredictors; i++){
        table_entry = (*table)[i];
        for (j=0; j < *order; j++) {
            for (k=0; k<VECTORSIZE; k++){
                fread(&ts, sizeof(short), 1, fhandle);
                table_entry[k][j] = (short) htons(ts);
            }
        }
        /* Now copy the right stuff to the rest of the matrix */
        for (k=1; k<VECTORSIZE; k++)
            table_entry[k][*order] = table_entry[k-1][*order-1];
        table_entry[0][*order] = VSCALING;
        for (k=1; k<VECTORSIZE; k++){
            for (j=0; j<k; j++)
                table_entry[j][k + *order] = 0.0;
            for (; j<VECTORSIZE; j++)
                table_entry[j][k + *order] = table_entry[j-k][*order];
        }
    }
    return 0;
}

int
inner_product(int length, int *v1, int *v2)
{
  int j = 0;
  int dout, fiout, out = 0;
  for (; j<length; j++)
    out += *v1++ * *v2++;

  /* Mimic truncation on the RSP - ie always towards more negative */

  dout = (int) out/VSCALING;
  fiout = VSCALING * dout;
  if ((out-fiout) < 0)
    return(dout - 1);
  else
    return(dout);
}