如何使该过程更快?好吧,首先,如果您觉得有时间可以检查实施,我现在正在这样做。(警告:我需要截断链接,因为 stackexchange 不允许我拥有超过 2 个链接)
代码在这里:汇编。com /code/PySpectrum/subversion/nodes/37/trunk/src/spectrum
看看这个包,它已经包含一些 .c 代码来加快一些操作。如果需要,这可能会简化某些部分在 C 中的实现,以便稍后通过 Python 导入它。
我做了 svn checkout https://subversion.assembla.com/svn/PySpectrum/
我得到了:~/spectrumlib/PySpectrum/trunk/src/cpp$ ls
init .py mydpss.c
mydpss.c 实现了一些子例程。也许可以在那里添加一些东西。
但是让我们看一下实际正在执行的代码: assembla 。com /code/PySpectrum/subversion/nodes/37/trunk/src/spectrum/yulewalker.py
from correlation import CORRELATION
from levinson import LEVINSON
r = CORRELATION(X, maxlags=order, norm=norm)
A, P, k = LEVINSON(r, allow_singularity=allow_singularity)
return A, P, k
这就是正在执行的。让我们检查一下这里的慢速部分是什么。
所以我修改了 yulewalker.py 如下:
import time
print "Correlation calculation"
t0 = time.time()
r = CORRELATION(X, maxlags=order, norm=norm)
print "Time elapsed: ", time.time()-t0
print "Levinson calculation"
t1 = time.time()
A, P, k = LEVINSON(r, allow_singularity=allow_singularity)
print "Time elapsed: ", time.time()-t1
return A, P, k
我运行了你的 stackexchange 代码,我得到了:
./aryulespeed.py
Correlation calculation
Time elapsed: 1.50509214401
Levinson calculation
Time elapsed: 0.000110864639282
Time elapsed: 1.50538802147
[-2.7616739 3.81292317 -2.65578205 0.9248034 ]
所以相关性计算是缓慢的部分!啊哈!这个人:汇编。com /code/PySpectrum/subversion/nodes/37/trunk/src/spectrum/correlation.py 似乎是胖子。
哦,看,代码中有一个有趣的注释:
Provides two correlation functions. :func:`CORRELATION` is slower than
:func:`xcorr`. However, the output is as expected by some other functions.
Ultimately, it should be replaced by :func:`xcorr`.
For real data, the behaviour of the 2 functions is identical. However, for
complex data, xcorr returns a 2-sides correlation.
也许我们应该尝试将这个 CORRELATION 交换为 xcorr!
所以我尝试了:
#! /usr/bin/env python
from pylab import *
import scipy.signal
from spectrum import *
import time
# The original imports
from spectrum.correlation import CORRELATION
from spectrum.levinson import LEVINSON
# The faster corr
from spectrum.correlation import xcorr
def superawesomearyule(X, order, norm='biased', allow_singularity=True):
assert norm in ['biased', 'unbiased']
import time
print "Correlation calculation in superawesomearyule"
t0 = time.time()
#r = CORRELATION(X, maxlags=order, norm=norm) # Commenting you, slowwww code!
r = xcorr(X, maxlags=order, norm=norm)
print "Time elapsed: ", time.time()-t0
print "Levinson calculation"
t1 = time.time()
A, P, k = LEVINSON(r, allow_singularity=allow_singularity)
print "Time elapsed: ", time.time()-t1
return A, P, k
# Create an AR model
a = [1, -2.7607, 3.8106, -2.6535, 0.9238]
# create some data based on these AR parameters
X = randn(1, 256000)
y = scipy.signal.lfilter([1], a, X)
# now, let us try to estimate the original AR parameters
t0 = time.time()
#AR, P, k = aryule(y[0], 4)
AR, P, k = superawesomearyule(y[0], 4)
print "Time elapsed: ", time.time()-t0
#%compare the results in ar_coeffs to the vector A.
print(AR)
并执行:
./aryulespeed.py
Correlation calculation in superawesomearyule
Time elapsed: 69.9396679401
Levinson calculation
Traceback (most recent call last):
File "./aryulespeed.py", line 38, in <module>
AR, P, k = superawesomearyule(y[0], 4)
File "./aryulespeed.py", line 24, in superawesomearyule
A, P, k = LEVINSON(r, allow_singularity=allow_singularity)
File "/usr/local/lib/python2.7/dist-packages/spectrum/levinson.py", line 112, in LEVINSON
if P <= 0 and allow_singularity==False:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
而且......哦,不:(花了69秒!!然后在莱文森上也坠毁了。
我认为给它一点爱可以在这里做一些事情。也许给它的开发者 Thomas Cokelaer 发一封邮件会有所帮助。
很抱歉,我现在不能在这个问题上投入更多时间。我希望这对您有所帮助:)
我还发现了这个 github 仓库:
https ://github.com/RhysU/ar
其中有一个带有一些 python 绑定的 ar 实现。它也可能对您有所帮助。
祝你好运!