Utils

Useful wavelet/signal utilities

Public API

Wavelets.Util.maxtransformlevelsFunction
maxtransformlevels(n)

maxtransformlevels(x[, dims])

Extension function from Wavelets.jl. Finds the max number of transform levels for an array x. If dims is provided for a multidimensional array, then it finds the max transform level for that corresponding dimension.

Arguments

  • x::AbstractArray: Input array.
  • dims::Integer: Dimension used for wavelet transform.
  • n::Integer: Length of input vector.

Returns

::Integer: Max number of transform levels.

Examples

using Wavelets, WaveletsExt

# Define random signal
x = randn(64, 128)

# Max transform levels and their corresponding return values
maxtransformlevels(128)     # 7
maxtransformlevels(x)       # 6
maxtransformlevels(x, 2)    # 7
source
WaveletsExt.Utils.getbasiscoefFunction
getbasiscoef(Xw, tree)

Get the basis coefficients for the decomposed signal Xw with respect to the tree tree.

Arguments

  • Xw::AbstractArray{T,2} where T<:Number: Decomposed 1D-signal.
  • tree::BitVector: The corresponding basis tree.

Returns

  • ::Array{T,1}: Basis coefficients of signal.

Examples

using Wavelets, WaveletsExt

# Generate signal and wavelet
x = generatesignals(:heavysine)
wt = wavelet(WT.db4)

# Decompose signal
Xw = iwpd(x, wt)
tree = maketree(128, 6, :dwt)

# Get basis coefficients
xw = getbasiscoef(Xw, tree)
source
WaveletsExt.Utils.getbasiscoefallFunction
getbasiscoefall(Xw, tree)

Get the basis coefficients for all decomposed signals in Xw with respect to the tree(s) tree.

Arguments

  • Xw::AbstractArray{T,3} where T<:Number: A set of decomposed 1D-signals.
  • tree::BitVector or tree::BitArray{2}: The corresponding basis tree(s). If input is a BitMatrix, each column corresponds to a signal in Xw, and therefore the number of columns must be equal to the number of signals.

Returns

  • ::Array{T,2}: Basis coefficients of signals.

Examples

using Wavelets, WaveletsExt

# Generate signals and wavelet
c = ClassData(:cbf, 10, 10, 10)
X = generateclassdata(c)
wt = wavelet(WT.db4)

# Decompose signals
Xw = iwpdall(X, wt)
tree = maketree(128, 6, :dwt)

# Get basis coefficients
getbasiscoefall(Xw, tree)
source
WaveletsExt.Utils.getrowrangeFunction
getrowrange(n, idx)

Get the row range from a matrix with n rows that corresponds to idx from a quadtree.

Arguments

  • n::Integer: Number of rows in matrix.
  • idx::T where T<:Integer: Index from a quadtree corresponding to the matrix.

Returns

::UnitRange{Int64}: Row range in matrix that corresponds to idx.

Examples

using WaveletsExt

x = randn(8,8)
tree = maketree(x, 3, :full)
getrowrange(8,3)            # 1:4

See also: Wavelets.Util.maketree, getcolrange

source
WaveletsExt.Utils.getcolrangeFunction
getcolrange(n, idx)

Get the column range from a matrix with n columns that corresponds to idx from a quadtree.

Arguments

  • n::Integer: Number of columns in matrix.
  • idx::T where T<:Integer: Index from a quadtree corresponding to the matrix.

Returns

::UnitRange{Int64}: Column range in matrix that corresponds to idx.

Examples

using WaveletsExt

x = randn(8,8)
tree = maketree(x, 3, :full)
getcolrange(8,3)            # 5:8

See also: Wavelets.Util.maketree, getrowrange

source
WaveletsExt.Utils.nodelengthFunction
nodelength(N, L)

Returns the node length at level L of a signal of length N when performaing wavelet packet decomposition. Level L == 0 corresponds to the original input signal.

Arguments

  • N::Integer: Length of signal.
  • L::Integer: Level of signal.

Returns

::Integer: Length of nodes at level L.

source
WaveletsExt.Utils.coarsestscalingrangeFunction
coarsestscalingrange(x, tree[, redundant])
coarsestscalingrange(n, tree[, redundant])

Given a binary tree, returns the index range of the coarsest scaling coefficients.

Arguments

  • x::AbstractArray{T} where T<:Number: Decomposed 1D-signal.
  • n::Integer: Length of signal of interest.
  • tree::BitVector: Binary tree.
  • redundant::Bool: (Default: false) Whether the wavelet decomposition is redundant. Examples of redundant wavelet transforms are the Autocorrelation wavelet transform (ACWT), Stationary wavelet transform (SWT), and the Maximal Overlap wavelet transform (MOWT).

Returns

UnitRange{Integer} or ::Tuple{UnitRange{Integer}, Integer}: The index range of the coarsest scaling subspace based on the input binary tree.

Examples

using Wavelets, WaveletsExt

x = randn(8)
wt = wavelet(WT.haar)
tree = maketree(x)

# Non-redundant wavelet transform
xw = wpd(x, wt)
coarsestscalingrange(xw, tree)          # 1:1

# Redundant wavelet transform
xw = swpd(x, wt)
coarsestscalingrange(xw, tree, true)    # (1:8, 8)

See also: finestdetailrange

source
WaveletsExt.Utils.finestdetailrangeFunction
finestdetailrange(x, tree[, redundant])
finestdetailrange(n, tree[, redundant])

Given a binary tree, returns the index range of the finest detail coefficients.

Arguments

  • x::AbstractArray{T} where T<:Number: Decomposed 1D-signal.
  • n::Integer: Length of signal of interest.
  • tree::BitVector: Binary tree.
  • redundant::Bool: (Default: false) Whether the wavelet decomposition is redundant. Examples of redundant wavelet transforms are the Autocorrelation wavelet transform (ACWT), Stationary wavelet transform (SWT), and the Maximal Overlap wavelet transform (MOWT).

Returns

UnitRange{Integer} or ::Tuple{UnitRange{Integer}, Integer}: The index range of the finest detail subspace based on the input binary tree.

Examples

using Wavelets, WaveletsExt

x = randn(8)
wt = wavelet(WT.haar)
tree = maketree(x)

# Non-redundant wavelet transform
xw = wpd(x, wt)
finestdetailrange(xw, tree)          # 8:8

# Redundant wavelet transform
xw = swpd(x, wt)
finestdetailrange(xw, tree, true)    # (1:8, 15)

See also: coarsestscalingrange

source

Tree traversing functions

Public API

Wavelets.Util.isvalidtreeMethod
isvalidtree(x, b)

Check if tree is a valid tree for the signal x.

Arguments

  • x::AbstractVector or x::AbstractMatrix: Signal (1D or 2D).
  • b::BitVector: Tree to represent x.

Returns

  • ::Bool: Whether tree is valid.
source
Wavelets.Util.maketreeFunction
maketree(x::Vector, s::Symbol=:full)
maketree(n::Int, L::Int, s::Symbol=:full)

return a tree (BitVector) s=:full, all nodes for first L levels equal 1, others 0 s=:dwt, nodes corresponding to a dwt for first L levels equal 1, others 0

maketree(x[, s])
maketree(n, m, L[, s])

Build quadtree for 2D wavelet transform. Indexing of the tree are as follows:

Level 0                 Level 1                 Level 2             ...
_________________       _________________       _________________
|               |       |   2   |   3   |       |_6_|_7_|10_|11_|
|       1       |       |_______|_______|       |_8_|_9_|12_|13_|   ...
|               |       |   4   |   5   |       |14_|15_|18_|19_|
|_______________|       |_______|_______|       |16_|17_|20_|21_|

Arguments

  • x::AbstractMatrix{T} where T<:Number: Input array.
  • n::Integer: Number of rows in x.
  • m::Integer: Number of columns in x.
  • L::Integer: Number of decomposition levels.
  • s::Symbol: (Default: :full) Type of quadtree. Available types are :full and :dwt.

Returns

::BitVector: Quadtree representation.

Examples

using Wavelets, WaveletsExt

x = randn(16,16)
maketree(x)
source
WaveletsExt.Utils.getchildindexFunction
getchildindex(idx, child)

Get the child index of a parent index idx.

Arguments

  • idx::T where T<:Integer: Index of parent node.
  • child::Symbol: Type of child. For binary trees, available children are :left and :right. For quadtrees, available children are :topleft, :topright, :bottomleft, :bottomright.

Returns

  • ::T: Index of child node.

Examples

using WaveletsExt

getchildindex(3,:left) == 6
getchildindex(3,:right) == 7
getchildindex(3,:topleft) == 22
getchildindex(3,:topright) == 23
getchildindex(3,:bottomleft) == 24
getchildindex(3,:bottomright) == 25
source
WaveletsExt.Utils.getparentindexFunction
getparentindex(idx, tree_type)

Get the parent index of the child node idx.

Arguments

  • idx::T where T<:Integer: Index of child node.
  • tree_type::Symbol: Tree type, ie. :binary tree or :quad tree.

Returns

  • ::T: Index of parent node.
source
WaveletsExt.Utils.getleafFunction
getleaf(tree, tree_type)

Returns the leaf nodes of a tree.

Arguments

  • tree::BitVector: BitVector to represent binary tree.

Returns

::BitVector: BitVector that can represent a binary tree, but only the leaves are labeled 1, the rest of the nodes are labeled 0.

Examples

using Wavelets, WaveletsExt

tree = maketree(4, 2, :dwt)     # [1,1,0]
getleaf(tree)                   # [0,0,1,1,1,0,0]
source
WaveletsExt.Utils.getdepthFunction
getdepth(idx, tree_type)

Get depth of idx in a binary tree or quadtree.

Arguments

  • idx::T where T<:Integer: Index of a node.
  • tree_type::Symbol: Tree type. Supported types are :binary and :quad trees.

Returns

::T: Depth of node idx.

Examples

using WaveletsExt

getdepth(1,:binary)     # 0
getdepth(3,:binary)     # 1
getdepth(8,:binary)     # 3

getdepth(1,:quad)       # 0
getdepth(3,:quad)       # 1
getdepth(8,:quad)       # 2

See also: Wavelets.Util.maketree

source
WaveletsExt.Utils.gettreelengthFunction
gettreelength(n[, m])

Compute the length of the binary/quad tree BitVector.

Arguments

  • n::T where T<:Integer: Array size at 1st dimension/Signal length for 1D-signals.
  • m::T where T<:Integer: Array size at 2nd dimension.

Returns

  • ::T: Length of binary/quad tree.

Examples

using WaveletsExt

Utils.gettreelength(8)          # 7
Utils.gettreelength(8,8)        # 21
source

Metrics

Public API

WaveletsExt.Utils.relativenormFunction
relativenorm(x, x₀[, p]) where T<:Number

Returns the relative norm of base p between original signal x₀ and noisy signal x.

Arguments

  • x::AbstractArray{T} where T<:Number: Signal with noise.
  • x₀::AbstractArray{T} where T<:Number: Reference signal.
  • p::Real: (Default: 2) $p$-norm to be computed.

Returns

::AbstractFloat: The relative norm between x and x₀.

Examples

using WaveletsExt

x = randn(8)
y = randn(8)

relativenorm(x, y)

See also: psnr, snr, ssim

source
WaveletsExt.Utils.psnrFunction
psnr(x, x₀)

Returns the peak signal to noise ratio (PSNR) between original signal x₀ and noisy signal x.

PSNR definition: $10 \log_{10} \frac{\max{(x_0)}^2}{MSE(x, x_0)}$

Arguments

  • x::AbstractVector{T} where T<:Number: Signal with noise.
  • x₀::AbstractVector{T} where T<:Number: Reference signal.

Returns

::AbstractFloat: The PSNR between x and x₀.

Examples

using WaveletsExt

x = randn(8)
y = randn(8)

psnr(x, y)

See also: relativenorm, snr, ssim

source
WaveletsExt.Utils.snrFunction
snr(x, x₀)

Returns the signal to noise ratio (SNR) between original signal x₀ and noisy signal x.

SNR definition: $20 \log_{10} \frac{||x_0||_2}{||x-x_0||_2}$

Arguments

  • x::AbstractVector{T} where T<:Number: Signal with noise.
  • x₀::AbstractVector{T} where T<:Number: Reference signal.

Returns

::AbstractFloat: The SNR between x and x₀.

Examples

using WaveletsExt

x = randn(8)
y = randn(8)

snr(x, y)

See also: relativenorm, psnr, ssim

source
WaveletsExt.Utils.ssimFunction
ssim(x, x₀)

Wrapper for assess_ssim function from ImageQualityIndex.jl.

Returns the Structural Similarity Index Measure (SSIM) between the original signal/image x₀ and noisy signal/image x.

Arguments

  • x::AbstractArray{T} where T<:Number: Signal with noise.
  • x₀::AbstractArray{T} where T<:Number: Reference signal.

Returns

::AbstractFloat: The SNR between x and x₀.

Examples

using WaveletsExt

x = randn(8)
y = randn(8)

ssim(x, y)

See also: relativenorm, psnr, snr

source

Dataset generation

Public API

WaveletsExt.Utils.ClassDataType
ClassData(type, s₁, s₂, s₃)

Based on the input type, generates 3 classes of signals with sample sizes s₁, s₂, and s₃ respectively. Accepted input types are:

  • :tri: Triangular signals of length 32
  • :cbf: Cylinder-Bell-Funnel signals of length 128

Based on N. Saito and R. Coifman in "Local Discriminant Basis and their Applications" in the Journal of Mathematical Imaging and Vision, Vol. 5, 337-358 (1995).

See also: generateclassdata

source
WaveletsExt.Utils.duplicatesignalsFunction
duplicatesignals(x, n, k[, noise=false, t=1])

Given a signal x, returns n shifted versions of the signal, each with shifts of multiples of k.

Setting noise = true allows randomly generated Gaussian noises of μ = 0, σ² = t to be added to the circularly shifted signals.

Arguments

  • x::AbstractVector{T} where T<:Number: 1D-signal to be duplicated.
  • n::Integer:: Number of duplicated signals.
  • k::Integer:: Circular shift size for each duplicated signal.
  • noise::Bool: (Default: false) Whether or not to add Gaussian noise.
  • t::Real: (Default: 1) Relative size of noise.

Returns

::Array{T}: Duplicated signals.

Examples

using WaveletsExt

x = generatesignals(:blocks)
duplicatesignals(x, 5, 0)      # [x x x x x]

See also: generatesignals

source
WaveletsExt.Utils.generatesignalsFunction
generatesignals(fn, L)

Generates a signal of length 2ᴸ given the function symbol fn. Current accepted inputs below are based on D. Donoho and I. Johnstone in "Adapting to Unknown Smoothness via Wavelet Shrinkage" Preprint Stanford, January 93, p 27-28.

  • :blocks
  • :bumps
  • :heavisine
  • :doppler
  • :quadchirp
  • :mishmash

The code for this function is adapted and translated based on MATLAB's Wavelet Toolbox's wnoise function.

Arguments

  • fn::Symbol: Type of function/signal to generate.
  • L::Integer: (Default = 7) Size of the signal to generate. Will return a signal of size 2ᴸ.

Returns

::Vector{Float64}: Signal of length 2ᴸ.

Examples

using WaveletsExt

generatesignals(:bumps, 8)
source
WaveletsExt.Utils.generateclassdataFunction
generateclassdata(c[, shuffle])

Generates 3 classes of data given a ClassData struct as an input. Returns a matrix containing the 3 classes of signals and a vector containing their corresponding labels.

Based on N. Saito and R. Coifman in "Local Discriminant Basis and their Applications" in the Journal of Mathematical Imaging and Vision, Vol. 5, 337-358 (1995).

Arguments

  • c::ClassData: Type of signal classes to generate.
  • shuffle::Bool: (Default: true). Whether or not to shuffle the signals.

Returns

  • ::Matrix{Float64}: Generated signals.
  • ::Vector{Int64}: Class corresponding to each column of generated signals.

Examples

using WaveletsExt

c = ClassData(:tri, 100, 100, 100)
generateclassdata(c)

See also: ClassData

source

Miscellaneous

Private API

WaveletsExt.Utils.main2depthshiftFunction
main2depthshift(sm, L)

Given the overall shift sm, compute the cumulative shift at each depth. Useful for computing the shift based inverse redundant wavelet transforms.

Arguments

  • sm::Integer: Overall shift.
  • L::Integer: The total number of depth.

Returns

::Vector{Int}: Vector where each entry i describes the shift at depth i-1.

Examples

using WaveletsExt

Utils.main2depthshift(10, 4)      # [0, 0, 2, 2, 10]
Utils.main2depthshift(5, 5)       # [0, 1, 1, 5, 5, 5]
source