[英]How to vectorize this loop in python?
How can I vectorize this segment of pseudocode: 如何将这部分伪代码向量化:
for i from 1 to n
y[i] := y[i-1] + α * (x[i] - y[i-1])
Your original equation can be written: 您的原始等式可以写成:
y[i] = alpha*x[i] + (1-alpha)*y[i-1]
This is an infinite impulse response (IIR) filter. 这是一个无限脉冲响应(IIR)滤波器。 You can take the Z transform to get:
您可以采用Z变换来获得:
Y(z) = alpha*X(z) + (1-alpha)*Y(z^-1)
which can be manipulated to give the transfer function of the filter 可以对其进行操作以提供滤波器的传递函数
Y(z)/X(z) = alpha/(1 + (alpha-1)*z^-1)
You can use the lfilter
function in scipy.signal
(docs) to speed up your calculation: 您可以在
scipy.signal
(docs)中使用lfilter
函数来加快计算速度:
import scipy.signal as sig
y = sig.lfilter([alpha], [1, alpha-1], x)
This is not, strictly speaking, vectorization. 严格来讲,这不是矢量化。 But it has the same effect: it speeds up the calculation by performing the loop in C.
但是它具有相同的效果:通过在C中执行循环来加快计算速度。
It can not be vectorized. 不能向量化。 Your induction is simply y[i] = a * x[i] + (1-a) * y[i-1]
您的归纳就是y [i] = a * x [i] +(1-a)* y [i-1]
Think of y[n] like a polynomial, a * x[i] and y[0] will be the coefficients and 1-a will be the variable. 将y [n]视为多项式,a * x [i]和y [0]将是系数,而1-a将是变量。 And there is no way to vectorize the evaluation of a polynomial.
而且没有办法向量化多项式的求值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.