一个更复杂的forloop的Python矢量化

2024-05-03 13:02:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想矢量化这个函数的for循环,但是由于广播错误,我的所有尝试都失败了。。我是新的使用矢量化,所以我想你们中的一些人可能有更多的经验,可以帮助我

当我试图将corr_matr附加到全局字段self.altes_E时,出现了强制转换错误。我认为这是因为我想为每个frequenz保存corr_matr,但我不知道如何求解

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

    alle_taus = []

    temp_gcc_ergebnisse = np.zeros((len(TDOA_werte), len(__frequency_bins__)))

    for zaehler, frequenz in enumerate(__frequency_bins__):

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

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

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

        if(self.iterator == 1):
            corr_matr = E
        else:
            corr_matr = alpha * self.altes_E[zaehler] + (1 - alpha) * E
        self.altes_E[zaehler] = corr_matr

        max_tau = 0
        Rxx12 = corr_matr[0,1]

        temp_gcc_ergebnisse[:,zaehler] = (Rxx12/abs(Rxx12) * np.exp(-2j * np.pi * TDOA_werte * frequenz)).real

        i = np.argmax(temp_gcc_ergebnisse[:,zaehler])
        max_tau = TDOA_werte[i]

        alle_taus.append(max_tau)


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

感谢您的帮助,谢谢


Tags: selfsignalnparraytempgcctaucorr