When learning signal processing, linear prediction is a difficult point to understand, in order to speed up the understanding of many friends, this gives the linear prediction of the Levinson-durbin algorithm and a test demo,demo the input signal, prediction signal, prediction error is clearly printed out, In this way, the realization and function of linear prediction can be displayed in the most intuitive manner. Words not much to say, directly on the code!
1typedeffloatOsFlt32;2typedefintOsInt32;3 4OsFlt32 LPC (ConstOsFlt32 *r,osint32 P,osflt32 *a)5 {6 OsInt32 i,j;7 OsFlt32 err;8 9 if(0= = r[0])Ten { One for(i =0; I < P; i++) A[i] =0; A return 0; - } -a[0] =1.0; theErr = r[0]; - for(i =0; I < P; i++) - { -OsFlt32 lambda =0.0; + for(j =0; J <= I; J + +) -Lambda-= a[j]*r[i+1-j]; +Lambda/=err; A //Update LPC coefficients and total error at for(j =0; J <= (i+1)/2; J + +) - { -OsFlt32 temp = a[i+1-J] + lambda *A[j]; -A[J] = A[j] + lambda * a[i+1-j]; -a[i+1-J] =temp; - } inErr *= (1.0-lambda*Lambda); - } to returnerr; + } - the voidAutocorr (ConstOsFlt32 *x,osint32 N,osflt32 *r,osint32 k) * { $ OsFlt32 D;Panax Notoginseng OsInt32 i,p; - the for(P =0; P <= k; p++) + { A for(i =0, d =0.0; i < n-p; i++) theD + = x[i] * x[i+p]; +R[P] =D; - } $}
1#include"lpc.h"2 3 intMainintArgc,char * *argv)4 {5OsInt32 Nlen = -;6OsFlt32 *poriginal,*ppredicted;7 OsInt32 i,j;8 ConstOsInt32 order =4;9OsFlt32 r[order+1] = {0.0};TenOsFlt32 a[order+1] = {0.0}; One OsFlt32 error; A -Poriginal = (osflt32*)calloc(Nlen,sizeof(OsFlt32)); -ppredicted = (osflt32*)calloc(Nlen,sizeof(OsFlt32)); the - for(i =0; i < Nlen; i++) -Poriginal[i] = sin (i*0.01) +0.75* Sin (i*0.03) +0.5* Sin (i*0.05) +0.25* Sin (i*0.11); - + Autocorr (poriginal,nlen,r,order); - LPC (r,order,a); + A for(i =1; I <= order; i++) ata[i-1] =A[i]; - - for(i = order; i < Nlen; i++) - { -Ppredicted[i] =0.0; - for(j =0; J < Order; J + +) inPpredicted[i] = a[j] * poriginal[i-1-j]; - } to +Error =0; - for(i = order; i < Nlen; i++) the { * DoubleDelta = ppredicted[i]-Poriginal[i]; $printf"Index:%.2d/original:%.6f/predicted:%.6f\n", I,poriginal[i],ppredicted[i]);Panax NotoginsengError + = Delta *Delta; - } theprintf"Forward Linear Prediction approximation Error:%f\n", error); + A Free(ppredicted); the Free(poriginal); + return 0; -}
This article is from Icoolmedia, welcome to join the audio and video algorithm discussion QQ Group (374737122) in Exchange together!
Linear prediction and Levinson-durbin algorithm implementation