ftp.nice.ch/pub/next/unix/audio/shorten.1.12.s.tar.gz#/shorten-1.12/lpc.c

This is lpc.c in view mode; [Download] [Up]

/******************************************************************************
*                                                                             *
*       Copyright (C) 1992,1993,1994 Tony Robinson                            *
*                                                                             *
*       See the file LICENSE for conditions on distribution and usage         *
*                                                                             *
******************************************************************************/

# include <math.h>
# include <stdio.h>
# include "shorten.h"

/* watch out, these are all 0 .. order inclusive arrays */

# define E_BITS_PER_COEF (2 + LPCQUANT)

int wav2lpc(buf, nbuf, qlpc, nlpc, presn) long *buf; int nbuf, *qlpc, nlpc,
     *presn; {
  int   i, j, bestnbit, bestnlpc;
  float e = 0.0, ci, esize;
  float acf[MAX_LPC_ORDER];
  float ref[MAX_LPC_ORDER];
  float lpc[MAX_LPC_ORDER];
  float tmp[MAX_LPC_ORDER];
  float escale = 0.5 * M_LN2 * M_LN2 / nbuf;

  if(nlpc >= nbuf) nlpc = nbuf - 1;

  for(j = 0; j < nbuf; j++)
    e += (float) buf[j] * (float) buf[j];

  if(e == 0.0)
    *presn = 0;
  else {
    acf[0] = e;
    esize = 0.5 * log(escale * e) / M_LN2;
    bestnbit = nbuf * esize;
    *presn = floor(esize + 0.5);
  }
  bestnlpc = 0;

  for(i = 1; i <= nlpc && e > 0.0; i++) {
    float sum = 0.0;

    for(j = i; j < nbuf; j++)
      sum += (float) buf[j] * (float) buf[j - i];
    acf[i] = sum;

    ci = 0.0;
    for(j = 1; j < i; j++) ci += lpc[j] * acf[i - j];
    ref[i] = ci = (acf[i] - ci) / e;
    lpc[i] = ci;
    for(j = 1; j < i; j++) tmp[j] = lpc[j] - ci * lpc[i - j];
    for(j = 1; j < i; j++) lpc[j] = tmp[j];

    e = (1 - ci * ci) * e;
    if(e > 0.0)
      esize = 0.5 * log(escale * e) / M_LN2;

    if(e == 0.0 || nbuf * esize + i * E_BITS_PER_COEF < bestnbit) {

      /* store best model order */
      bestnlpc = i;

      /* quantise lpc coefficients */
      for(j = 0; j < bestnlpc; j++)
	qlpc[j] = lpc[j + 1] * (1 << LPCQUANT);

      if(e == 0.0)
	*presn = 0;
      else {
	bestnbit = nbuf * esize + i * E_BITS_PER_COEF;
	*presn = floor(esize + 0.5);
      }
    }
  }
  if(*presn < 0) *presn = 0;
  
  return(bestnlpc);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.