简体   繁体   English

Python NumPy 矢量化以减少处理时间

[英]Python NumPy Vectorization to decrease processing time

I am currently trying to improve the performance of my python code.我目前正在尝试提高我的 python 代码的性能。 To do so I would like to vectorize the following code:为此,我想对以下代码进行矢量化:

def phiGCC(self, signal: np.array, TDOA_values: np.array, __frequency_bins__, alpha: float, e_old) -> float:

        alle_taus = []

        temp_gcc_results = np.zeros((len(TDOA_values), len(self.__frequency_bins__)))

        for counter, frequency in enumerate(__frequency_bins__):

            x = np.array([signal[0][counter], signal[1][counter]])

            y = np.conj(x)
            y = y.T

            E = np.zeros((2,2), dtype = complex)
            E[0,0] = x[0] * y[0]
            E[0,1] = x[0] * y[1]
            E[1,0] = x[1] * y[0]
            E[1,1] = x[1] * y[1]


            if(self.interation_count == 1):
                corr_matr = E
            else:

                corr_matr = alpha * self.altes_E[counter] + (1 - alpha) * E

            self.altes_E[counter] = corr_matr


            max_val = 0
            max_tau = 0
            Rxx12 = corr_matr[0][1]

            for tau_index, tau in enumerate(TDOA_values):

                temp = (Rxx12/abs(Rxx12) * np.exp(-2j * np.pi * tau * frequency)).real  #plus frequenz
                temp_gcc_results[tau_index, counter] = temp

                if(temp > max_val):
                    max_val = temp
                    max_tau = tau


            alle_taus.append(max_tau)



        tau_func = np.mean(temp_gcc_results, axis=1)
        maximaler_wert = np.argmax(tau_func)
        return alle_taus[maximaler_wert]

I have read about vectorization and was able to vectorize another function which was very easy and not as complex as the code I have quoted, because there was no function call within the for-loop and I am not sure how I could vectorize the quoted function.我已经阅读了有关矢量化的内容,并且能够矢量化另一个函数,该函数非常简单,而且不像我引用的代码那么复杂,因为 for 循环中没有函数调用,我不确定如何矢量化引用的函数.

My code works but really slow... and I hope that I can get close to real-time performance by vectorizing it.我的代码可以工作,但真的很慢……我希望通过矢量化我可以接近实时性能。 Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

I haven't tried to understand your full code, so can only suggests some initial steps.我没有试图理解你的完整代码,所以只能建议一些初始步骤。

For start I try to eliminate inner loops.首先,我尝试消除内部循环。 Only then is there a chance of tackling the outer one.只有这样,才有机会解决外部问题。

    for tau_index, tau in enumerate(TDOA_values):
        temp = (Rxx12/abs(Rxx12) * np.exp(-2j * np.pi * tau * frequency)).real  #plus frequenz
        temp_gcc_results[tau_index, counter] = temp

        if(temp > max_val):
            max_val = temp
            max_tau = tau

At a glance I think this can be replaced with:乍一看,我认为这可以替换为:

 temp_gcc_results[:,counter] = (Rxx12/abs(Rxx12) * np.exp(-2j * np.pi * TDOA_values * frequency)).real

 i = np.argmax(temp_gcc_results[:,counter])
 max_val = temp_gcc_results[i]
 max_tau = TDOA_values[i]

Since I have run your code, or these changes, there might be errors.由于我已经运行了您的代码或这些更改,因此可能会出现错误。 But as best I can see there's no need to evaluate temp in the (python) loop, since the value doesn't depend on the previous.但尽我所能看到没有必要在 (python) 循环中评估temp ,因为该值不依赖于前一个。 The if clause can be handled with argmax . if子句可以用argmax处理。

        E = np.zeros((2,2), dtype = complex)
        E[0,0] = x[0] * y[0]
        E[0,1] = x[0] * y[1]
        E[1,0] = x[1] * y[0]
        E[1,1] = x[1] * y[1]

This can probably written as a outer product of x and y .这可能可以写成xy的外积。 Though it's small enough that there won't be much of a time savings.虽然它足够小,不会节省太多时间。

        E = np.outer(x,y)
        E = x[:,None]*y

Not a speedup, but if corr_matr is an array:不是加速,但如果corr_matr是一个数组:

Rxx12 = corr_matr[0,1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM