我正尝试着手研究线性预测,并想用 Python 编写一个基本示例来测试我的理解。线性预测编码背后的思想是根据过去样本的线性组合来估计信号的 future 样本。
我在 scikits.talkbox 中使用 lpc 模块,所以我不必自己编写任何算法。这是我的代码:
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)])
输出为
[5.618790615323507, 6.316875690307965, 7.0149607652924235,
7.713045840276882, 8.411130915261339, 9.109215990245799, 9.807301065230257,
10.505386140214716]
[ 4. 5. 6. 7. 8. 9. 10. 11.]
我担心的是我以某种方式错误地实现了这一点,因为我认为如果我的输入数组是线性信号,那么根据过去的值预测 future 值应该没有问题。但是,它似乎确实有特别高的误差,尤其是前几个值。谁能告诉我我是否正确地实现了这个,或者给我指出了几个用 Python 完成的例子?非常感谢任何帮助,谢谢!
最佳答案
线性预测算法在两个方向上使用无限量的零扩展原始序列。因此,除非您的输入信号恒定为零,否则扩展序列不是线性的,您应该期望出现非零错误。 这是我的 Python 实现:
def lpc(y, m):
"Return m linear predictive coefficients for sequence y using Levinson-Durbin prediction algorithm"
#step 1: compute autoregression coefficients R_0, ..., R_m
R = [y.dot(y)]
if R[0] == 0:
return [1] + [0] * (m-2) + [-1]
else:
for i in range(1, m + 1):
r = y[i:].dot(y[:-i])
R.append(r)
R = np.array(R)
#step 2:
A = np.array([1, -R[1] / R[0]])
E = R[0] + R[1] * A[1]
for k in range(1, m):
if (E == 0):
E = 10e-17
alpha = - A[:k+1].dot(R[k+1:0:-1]) / E
A = np.hstack([A,0])
A = A + alpha * A[::-1]
E *= (1 - alpha**2)
return A
关于python - 基本线性预测示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53081956/