I wrote the following code in Julia for packet wavelet transform. It uses Haar transform (I've chosen low-pass filter to keep average value of the signal). As usual, it works recursively filtering each subband of the signal with low-pass and high-pass Haar filters, downsampling by 2 and arranging results in the correct order.

There is a testing function square_wave which generates a piece-wise constant periodic wave.

module HaarTransform

function haar_step(x::Array{Float64, 1})
    local half = div(length(x), 2)
    local ans_low = Array{Float64,1}(undef, half)
    local ans_high = Array{Float64,1}(undef, half)

    for i = 1:half
        ans_low[i] = (x[2i] + x[2i-1]) / 2
        ans_high[i] = x[2i] - x[2i-1]

    return ans_low, ans_high

function haar_packet(x::Array{Float64, 1}, forward = true)
    local res::Array{Float64, 1}
    if length(x) == 1
        res = x
        local flt = haar_step(x)
        flt = forward ? flt : reverse(flt)
        local flt2 = map(haar_packet, flt, (true, false))
        res = vcat(flt2...)
    return res

function square_wave(run, n)
    local res = Array{Float64, 1}(undef, n)
    local period = 2run
    for i = 1:n
        local x = rem(i-1,period)
        res[i] = x<run ? 1.0 : 0.0
    return res

export haar, haar_packet, square_wave

If I use wave with period $2^n$, $n \in \mathbb{N}$, I get very nice frequency detection as you can see here (these plots are for periods 4 and 32: Frequency analysis for square_wave(2,8192) and square_wave(16,8192)

But if a choose arbitrary period, like 200, I get the following (I can't guess frequency from this plot): Frequency analysis for square_wave(100,8192

It's clear what this flaw is based on the fact that the algorithm divides data by 2 in each pass and if an oscillation crosses power-of-two boundary, strange results may occur. The same strange results occur if the input is shifted in time by some constant phase.

My question is this: can wavelet packet transform algorithm be modified, so it behaves well with all frequencies, not only with power-of-two?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.