#include <math.h>
#include <stdlib.h>
#include <stdio.h>
/*
Calculation of LPC filter koeffs using Levinson - Durbin recursion.
*acf = ponter to array of autocorrelation function values acf[0], acf[1]....acf[lpc_order]
*lpc = pointer to filter koeffs calculated by function a[0],a[1]...a[lpc_order-1]
lpc_order = the order of LPC
*/
float lpc(float *acf, float *lpc, int lpc_order)
{
int i,j;
float tmp0,E,Pk;
float *tmp;
tmp=(float *)malloc(lpc_order*sizeof(float));
E=acf[0];
for(i=0;i<lpc_order;i++) lpc[i]=0.0;
for(i=0;i<lpc_order;i++)
{
tmp0=acf[i+1];
for(j=0;j<i;j++) tmp0-=lpc[j]*acf[i-j];
if(fabs(tmp0)>=E) break;
lpc[i]=Pk=tmp0/E;
E-=tmp0*Pk;
for(j=0;j<i;j++) tmp[j]=lpc[j];
for(j=0;j<i;j++) lpc[j]-=Pk*tmp[i-j-1];
}
free(tmp);
return E;
}
/*
LPC analyse FIR filter (whitening filter)
*data = pointer to input/output data
*lpc = pointer to filter koeffs
*z = pointer to filter memory
data_length = the length of the data to be processed
lpc_order = the order of LPC
*/
void lpc_analyse_filter(float *data,float *lpc,float *z,int data_length,int lpc_order)
{
int i,j;
float tmp;
for(i=0;i<data_length;i++)
{
tmp=data[i];
for(j=0;j<lpc_order;j++) tmp-=z[j]*lpc[j];
for(j=lpc_order-1;j>0;j--) z[j]=z[j-1];
z[0]=data[i];
data[i]=tmp;
}
return;
}
/*
LPC synthesis IIR filter (modeling filter)
*data = pointer to input/output data
*lpc = pointer to filter koeffs
*z = pointer to filter memory
data_length = the length of the data to be processed
lpc_order = the order of LPC
*/
void lpc_synthesis_filter(float *data,float *lpc,float *z,int data_length,int lpc_order)
{
int i,j;
float tmp;
for(i=0;i<data_length;i++)
{
tmp=data[i];
for(j=0;j<lpc_order;j++) tmp+=z[j]*lpc[j];
for(j=lpc_order-1;j>0;j--) z[j]=z[j-1];
z[0]=data[i]=tmp;
}
return;
}