这个小波变换实现是否正确?

时间:2013-11-27 13:34:30

标签: python fft wavelet haar-wavelet wavelet-transform

我正在寻找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

1 个答案:

答案 0 :(得分:3)

我没有发现任何错误。您也可以通过将结果与Pywavelet package的结果进行比较来检查。关于使用Pywavelet实现haar小波还有一个example