在没有包的情况下在python中实现Haar小波 [英] Implementing Haar wavelet in python without packages

查看:119
本文介绍了在没有包的情况下在python中实现Haar小波的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写代码以实现离散小波变换(haar wavelet dwt),而无需在python中使用包.

I am trying to write a code to implement discrete wavelet transform (haar wavelet dwt) without using packages in python.

到目前为止,我已经找到了一个链接,它们在其中实现了类似的功能,该链接是这个小波变换的实现对吗?.它在运行时不会给出任何错误,但最终结果是不正确的.我运行的代码是:

So far I've found a link where they implemented something similar, the link Is this wavelet transform implementation correct?. It doesn't give any errors while running, but the end result isn't correct. The code I ran is :

def discreteHaarWaveletTransform(x):
    N = len(x)
    output = [0.0]*N

    length = N >> 1
    while True:
        for i in range(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
        x = output[:length << 1]
        length >>= 1

输入:

list=[56, 40, 8, 24, 48, 48, 40, 16]

当前输出:

[280, -24, 64, 40, 16, -16, 0, 24]

预期输出:

[35, -3, 16, 10, 8, -8, 0, 12]

有什么明显的看不见的东西吗?

Is there something obvious I can't see?

推荐答案

类似的东西应该可以解决问题-这几乎是C 的答案.这可能意味着这不是非常Python-y的代码,但是如果您不使用 numpy 来实现,那还是不是非常Python-y.

Something like this should do the trick -- it's almost a literal translation of this answer for C. It probably means that this is not very Python-y code, but if you're not using numpy for this that's not very Python-y anyway.

未获得预期输出的主要原因是您忘记在过滤后缩放输出.这使得每个下一级的系数大约高两倍.

The main reason you did not get the output you expected is that you forgot to scale the output after filtering. This makes the coefficients at each next level approximately twice as high.

请注意,缩放比例为½可获得预期的输出,但更常用缩放比例为√√2,以保留小波变换下信号的L2-范数.

Mind that a scaling of ½ gives you the expected output, but a scaling of ½√2 is more commonly used, to preserve the L2-norm of signal under the wavelet transform.

def haarFWT ( signal, level ):

    s = .5;                  # scaling -- try 1 or ( .5 ** .5 )

    h = [ 1,  1 ];           # lowpass filter
    g = [ 1, -1 ];           # highpass filter        
    f = len ( h );           # length of the filter

    t = signal;              # 'workspace' array
    l = len ( t );           # length of the current signal
    y = [0] * l;             # initialise output

    t = t + [ 0, 0 ];        # padding for the workspace

    for i in range ( level ):

        y [ 0:l ] = [0] * l; # initialise the next level 
        l2 = l // 2;         # half approximation, half detail

        for j in range ( l2 ):            
            for k in range ( f ):                
                y [j]    += t [ 2*j + k ] * h [ k ] * s;
                y [j+l2] += t [ 2*j + k ] * g [ k ] * s;

        l = l2;              # continue with the approximation
        t [ 0:l ] = y [ 0:l ] ;

    return y

def main():
    s0 = [ 56, 40, 8, 24, 48, 48, 40, 16 ];

    print( "level 0" );
    print( s0 );

    print( "level 1" );
    print( haarFWT (s0, 1 ) );

    print( "level 2" );
    print( haarFWT (s0, 2 ) );

    print( "level 3" );
    print( haarFWT (s0, 3 ) );

if __name__ == "__main__":
    main()
# run with: >>> execfile ( "haarwavelet.py" )

这篇关于在没有包的情况下在python中实现Haar小波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆