我正在寻找 FFT 的替代品以在 python 中创建频谱图分析器。我听说小波变换比短时 FFT 更快,并且提供更好的时间精度。我阅读了这篇 wikipedia article,其中介绍了 Java 中的 Haar 小波变换实现:
https://en.wikipedia.org/wiki/Discrete_wavelet_transform#Code_example
我粗暴地将它转换为 python,但我不知道我得到的值是否正确。有人可以确认吗?
from math import *
N = 8
res = [sin(k) for k in xrange(N)]
for k in xrange(N):
print res[k]
print
def discreteHaarWaveletTransform(x):
N = len(x)
output = [0.0]*N
length = N >> 1
while True:
for i in xrange(0,length):
summ = x[i * 2] + x[i * 2 + 1]
difference = x[i * 2] - x[i * 2 + 1]
output[i] = summ
output[length + i] = difference
if length == 1:
return output
#Swap arrays to do next iteration
#System.arraycopy(output, 0, x, 0, length << 1)
x = output[:length << 1]
length >>= 1
res = discreteHaarWaveletTransform(res)
for k in xrange(N):
print res[k]
结果:
0.0
0.841470984808
0.909297426826
0.14112000806
-0.756802495308
-0.958924274663
-0.279415498199
0.656986598719
0.553732750242
3.23004408914
-0.208946450078
-2.09329787049
-0.841470984808
0.768177418766
0.202121779355
-0.936402096918