# Why packet wavelet transform for frequency analysis gets non-informative results for some frequencies and phase?

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]
end

return ans_low, ans_high
end

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

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
end
return res
end

export haar, haar_packet, square_wave
end


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: But if a choose arbitrary period, like 200, I get the following (I can't guess frequency from this plot): 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?