使用这里https://github.com/dingluo/fwht/blob/master/FWHT.py给定的代码,我编写了一个变体,该变体适用于矩阵的每一列,并以Hadamard顺序返回转换。问题甚至是原始的,与简单的Fortran实现相比,我的变体至少慢了两个数量级。在改善性能(速度)方面的任何帮助将不胜感激。这是我的变体。
def fwhtvec(x, s=0):
""" Fast Walsh-Hadamard Transform
Based on mex function written by Chengbo Li@Rice Uni
for his TVAL3 algorithm. His code is according to the
K.G. Beauchamp's book -- Applications of Walsh and
Related Functions. Modiefied by me to get it in hadamard order and
now works for all the vectors in a matrix. For s = 0 it is
usual Hadamard with matrix [[1, 1],[1, -1]]/sqrt(2), for s = 1
it is [[1, -i],[i, 1]]/sqrt(2)
"""
a = (-1j) ** s
N, nc = x.shape
x = x.T.reshape(nc, N, 1)
G = N / 2 # Number of Groups
M = 2 # Number of Members in Each Group
# First stage
y = np.zeros((nc, N / 2, 2), dtype=np.complex128)
y[0:nc, 0:G, 0] = x[0:nc, 0:G, 0] + a * x[0:nc, G:N, 0]
y[0:nc, 0:G, M - 1] = np.conj(a) * x[0:nc, 0:G, 0] - x[0:nc, G:N, 0]
x = y.copy()
# Second and further stage
for nStage in xrange(2, int(log(N, 2)) + 1):
y = np.zeros((nc, G / 2, M * 2), dtype=np.complex128)
y[0:nc, 0:G / 2, 0:M * 2:4] = x[0:nc, 0:G // 2, 0:M:2] + a * x[0:nc, G // 2:G, 0:M:2]
y[0:nc, 0:G / 2, 1:M * 2:4] = np.conj(a) * x[0:nc, 0:G // 2, 0:M:2] - x[0:nc, G // 2:G, 0:M:2]
y[0:nc, 0:G / 2, 2:M * 2:4] = x[0:nc, 0:G // 2, 1:M:2] + a * x[0:nc, G // 2:G, 1:M:2]
y[0:nc, 0:G / 2, 3:M * 2:4] = np.conj(a) * x[0:nc, 0:G // 2, 1:M:2] - x[0:nc, G // 2:G, 1:M:2]
x = y.copy()
G = G / 2
M = M * 2
x = y[:, 0, :].T / (np.sqrt(2) ** int(log(N, 2)))
return x