我正在寻找FFT的替代方法,以便在python中创建频谱图分析仪.我听说小波变换比短时FFT更快,并且提供了更好的时间精度.我读了这篇*文章,其中介绍了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
解决方法:
我没有发现任何问题.您也可以通过将结果与Pywavelet package的结果进行比较来进行检查.在Pywavelet中实现haar wavelet的过程也有example.