I'm trying wrap my head around linear prediction and figured I'd code up a basic example in Python to test my understanding. The idea behind linear predictive coding is to estimate future samples of a signal based on linear combinations of past samples.
I'm using the lpc module in scikits.talkbox so I don't have to write any of the algorithm myself. Here's my code:
import math
import numpy as np
from scikits.talkbox.linpred.levinson_lpc import levinson, acorr_lpc, lpc
x = np.linspace(0,11,12)
order = 5
"""
a = solution of the inversion
e = prediction error
k = reflection coefficients
"""
(a,e,k) = lpc(x,order,axis=-1)
recon = []
for i in range(order,len(x)):
sum = 0
for j in range(order):
sum += -k[j]*x[i-j-1]
sum += math.sqrt(e)
recon.append(sum)
print(recon)
print(x[order:len(x)])
which gives an output of
[5.618790615323507, 6.316875690307965, 7.0149607652924235,
7.713045840276882, 8.411130915261339, 9.109215990245799, 9.807301065230257,
10.505386140214716]
[ 4. 5. 6. 7. 8. 9. 10. 11.]
My concern is that I'm implementing this incorrectly somehow because I figured that if my input array is a linear signal, it should have no issue predicting future values based on past values. However, it does seem to have a particularly high error, especially for the first few values. Would anyone be able to tell me if I'm implementing this correctly or point me to a few examples where this is done in Python? Any help is greatly appreciated, thanks!