| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 
 | package org.xiph.speex;
 
/**
 * LPC- and Reflection Coefficients
 *
 * The next two functions calculate linear prediction coefficients
 * and/or the related reflection coefficients from the first P_MAX+1
 * values of the autocorrelation function.
 * 
 * Invented by N. Levinson in 1947, modified by J. Durbin in 1959.
 */
public class Lpc
{
  /**
   * Returns minimum mean square error
   * @param lpc - float[0...p-1] LPC coefficients
   * @param ac -  in: float[0...p] autocorrelation values
   * @param ref - out: float[0...p-1] reflection coef's
   * @param p
   */
  public static float wld(float[] lpc, float[] ac, float[] ref, int p)
  {
    int i, j;
    float r, error = ac[0];
 
    if (ac[0] == 0) 
    {
      for (i=0; i<p; i++)
        ref[i] = 0;
      return 0;
    }
 
    for (i = 0; i < p; i++) 
    {
      /* Sum up this iteration's reflection coefficient. */
      r = -ac[i + 1];
      for (j = 0; j < i; j++) r -= lpc[j] * ac[i - j];
      ref[i] = r /= error;
      /*  Update LPC coefficients and total error. */
      lpc[i] = r;
      for (j = 0; j < i/2; j++) 
      {
        float tmp  = lpc[j];
        lpc[j]     += r * lpc[i-1-j];
        lpc[i-1-j] += r * tmp;
      }
      if ((i % 2) != 0)
        lpc[j] += lpc[j] * r;
      error *= 1.0 - r * r;
    }
    return error;
  }
 
  /**
   * Compute the autocorrelation
   *         ,--,
   * ac(i) = >  x(n) * x(n-i)  for all n
   *         `--'
   * for lags between 0 and lag-1, and x == 0 outside 0...n-1
   * @param x - in: float[0...n-1] samples x
   * @param ac - out: float[0...lag-1] ac values
   * @param lag
   * @param n
   */
  public static void autocorr(float[] x, float[] ac, int lag, int n)
  {
    float d;
    int i;
    while (lag-- > 0) 
    {
      for (i=lag, d=0; i<n; i++)
        d += x[i] * x[i-lag];
      ac[lag] = d;
    }
  }
} | 
Partager