Skip to content

Commit

Permalink
add get functions for wavelet types
Browse files Browse the repository at this point in the history
  • Loading branch information
gummif committed Mar 12, 2015
1 parent 42b255e commit f68dc9c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ API
wavelet(w::WT.WaveletClass, boundary::WaveletBoundary=Periodic) # defaults to filter
waveletfilter(w::WT.WaveletClass, boundary::WaveletBoundary=Periodic)
waveletls(w::WT.WaveletClass, boundary::WaveletBoundary=Periodic)

# DWT (discrete wavelet transform)
dwt(x::AbstractArray, wt::DiscreteWavelet, L::Integer=maxtransformlevels(x))
idwt(x::AbstractArray, wt::DiscreteWavelet, L::Integer=maxtransformlevels(x))
Expand Down Expand Up @@ -74,12 +75,21 @@ The numbers for orthogonal wavelets indicate the number vanishing moments of the
| `Vaidyanathan` | vaid | `OrthoWaveletClass` | |
| `CDF` | cdf | `BiOrthoWaveletClass` | (9,7) |

Class information functions
Class information
```julia
class(::WaveletClass) ::ASCIIString # class full name
name(::WaveletClass) ::ASCIIString # type short name
vanishingmoments(::WaveletClass) # vanishing moments of wavelet function
```
Transform type information
```julia
length(f::OrthoFilter) # length of filter
qmf(f::OrthoFilter) # quadrature mirror filter
name(f::OrthoFilter)
makeqmfpair(f::OrthoFilter, fw::Bool=true, T::Type=eltype(qmf(f)))
makereverseqmfpair(f::OrthoFilter, fw::Bool=true, T::Type=eltype(qmf(f)))
name(s::GLS)
```

Examples
---------
Expand Down
26 changes: 5 additions & 21 deletions src/transforms_filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@
#
##################################################################################

function makeqmf(h::AbstractVector, fw::Bool, T::Type=eltype(h))
scfilter, dcfilter = makereverseqmf(h, fw, T)
return reverse(scfilter), reverse(dcfilter)
end
function makereverseqmf(h::AbstractVector, fw::Bool, T::Type=eltype(h))
h = convert(Vector{T}, h)
if fw
scfilter = reverse(h)
dcfilter = mirror(h)
else
scfilter = h
dcfilter = reverse(mirror(h))
end
return scfilter, dcfilter
end

for (Xwt) in (:dwt!, :wpt!)
@eval begin
# pseudo "inplace" by copying
Expand All @@ -39,7 +23,7 @@ end
# writes to y
function dwt!{T<:FloatingPoint}(y::AbstractVector{T}, x::AbstractVector{T}, filter::OrthoFilter, L::Integer, fw::Bool)
si = Array(T, length(filter)-1) # tmp filter vector
scfilter, dcfilter = makereverseqmf(filter.qmf, fw, T)
scfilter, dcfilter = makereverseqmfpair(filter, fw, T)

dwt!(y, x, filter, L, fw, dcfilter, scfilter, si)
return y
Expand Down Expand Up @@ -68,7 +52,7 @@ function dwt!{T<:FloatingPoint}(y::AbstractVector{T}, x::AbstractVector{T}, filt
# detail coefficients
filtdown!(dcfilter, si, y, detailindex(n,l,1), detailn(n,l), s, 1,-filtlen+1, true)
# scaling coefficients
filtdown!(scfilter, si, y, 1, detailn(n,l), s, 1, 0, false)
filtdown!(scfilter, si, y, 1, detailn(n,l), s, 1, 0, false)
else
# scaling coefficients
filtup!(false, scfilter, si, y, 1, detailn(n,l-1), s, 1, -filtlen+1, false)
Expand All @@ -91,7 +75,7 @@ function unsafe_dwt1level!{T<:FloatingPoint}(y::AbstractVector{T}, x::AbstractVe
# detail coefficients
filtdown!(dcfilter, si, y, detailindex(n,l,1), detailn(n,l), x, 1,-filtlen+1, true)
# scaling coefficients
filtdown!(scfilter, si, y, 1, detailn(n,l), x, 1, 0, false)
filtdown!(scfilter, si, y, 1, detailn(n,l), x, 1, 0, false)
else
# scaling coefficients
filtup!(false, scfilter, si, y, 1, detailn(n,l-1), x, 1, -filtlen+1, false)
Expand All @@ -107,7 +91,7 @@ function dwt!{T<:FloatingPoint}(y::Matrix{T}, x::AbstractMatrix{T}, filter::Orth
n = size(x,1)
si = Array(T, length(filter)-1) # tmp filter vector
tmpvec = Array(T,n<<1) # tmp storage vector
scfilter, dcfilter = makereverseqmf(filter.qmf, fw, T)
scfilter, dcfilter = makereverseqmfpair(filter, fw, T)

dwt!(y, x, filter, L, fw, dcfilter, scfilter, si, tmpvec)
return y
Expand Down Expand Up @@ -203,7 +187,7 @@ function wpt!{T<:FloatingPoint}(y::AbstractVector{T}, x::AbstractVector{T}, filt
si = Array(T, length(filter)-1)
ns = ifelse(fw, length(x)>>1, length(x))
snew = Array(T, ns)
scfilter, dcfilter = makereverseqmf(filter.qmf, fw, T)
scfilter, dcfilter = makereverseqmfpair(filter, fw, T)

wpt!(y, x, filter, tree, fw, dcfilter, scfilter, si, snew)
return y
Expand Down
56 changes: 42 additions & 14 deletions src/wavelettypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export DiscreteWavelet,
#CPBoundary,
#
OrthoFilter,
qmf,
name,
makereverseqmfpair,
makeqmfpair,
#BiOrthoFilter,
LSstep,
GLS,
Expand All @@ -22,6 +26,7 @@ export DiscreteWavelet,
waveletfilter,
waveletls

using ..Util
using Compat

# TYPE HIERARCHY
Expand Down Expand Up @@ -228,8 +233,8 @@ end
# IMPLEMENTATIONS OF FilterWavelet

immutable OrthoFilter{T<:WaveletBoundary} <: FilterWavelet{T}
qmf::Vector{Float64} # quadrature mirror filter
name::ASCIIString # filter short name
qmf ::Vector{Float64} # quadrature mirror filter
name ::ASCIIString # filter short name
OrthoFilter(qmf, name) = new(qmf, name)
end

Expand All @@ -246,6 +251,32 @@ function OrthoFilter{WC<:WT.OrthoWaveletClass, T<:WaveletBoundary}(w::WC, ::T=DE
end

Base.length(f::OrthoFilter) = length(f.qmf)
qmf(f::OrthoFilter) = f.qmf
name(f::OrthoFilter) = f.name

# scale filter by scalar
function scale{T<:WaveletBoundary}(f::OrthoFilter{T}, a::Number)
return OrthoFilter{T}(f.qmf.*a, f.name)
end

# quadrature mirror filter pair
function makeqmfpair(f::OrthoFilter, fw::Bool=true, T::Type=eltype(qmf(f)))
scfilter, dcfilter = makereverseqmfpair(f, fw, T)
return reverse(scfilter), reverse(dcfilter)
end

# reversed quadrature mirror filter pair
function makereverseqmfpair(f::OrthoFilter, fw::Bool=true, T::Type=eltype(qmf(f)))
h = convert(Vector{T}, qmf(f))
if fw
scfilter = reverse(h)
dcfilter = mirror(h)
else
scfilter = h
dcfilter = reverse(mirror(h))
end
return scfilter, dcfilter
end

#immutable BiOrthoFilter{T<:WaveletBoundary} <: FilterWavelet{T}
# qmf1::Vector{Float64} # quadrature mirror filter 1
Expand All @@ -254,17 +285,12 @@ Base.length(f::OrthoFilter) = length(f.qmf)
# BiOrthoFilter(qmf1, qmf2, name) = new(qmf1, qmf2, name)
#end

# scale filter by scalar
function scale{T<:WaveletBoundary}(f::OrthoFilter{T}, a::Number)
return OrthoFilter{T}(f.qmf.*a, f.name)
end

# IMPLEMENTATIONS OF LSWavelet

immutable LSstep
stept::Char # step type: 'p' for predict, 'u' for update
coef::Vector{Float64} # lifting coefficients
shift::Int # + left shift, - right shift
stept ::Char # step type: 'p' for predict, 'u' for update
coef ::Vector{Float64} # lifting coefficients
shift ::Int # + left shift, - right shift
function LSstep(stept,coef,shift)
@assert stept=='p' || stept=='u'
return new(stept,coef,shift)
Expand All @@ -273,10 +299,10 @@ end

# general lifting scheme
immutable GLS{T<:WaveletBoundary} <: LSWavelet{T}
step::Vector{LSstep} # steps to be taken
norm1::Float64 # normalization of scaling coefs.
norm2::Float64 # normalization of detail coefs.
name::ASCIIString # name of scheme
step ::Vector{LSstep} # steps to be taken
norm1 ::Float64 # normalization of scaling coefs.
norm2 ::Float64 # normalization of detail coefs.
name ::ASCIIString # name of scheme
GLS(step, norm1, norm2, name) = new(step, norm1, norm2, name)
end

Expand All @@ -287,6 +313,8 @@ function GLS{WC<:WT.WaveletClass, T<:WaveletBoundary}(w::WC, ::T=DEFAULT_BOUNDAR
return GLS{T}(schemedef..., name)
end

name(s::GLS) = s.name


# IMPLEMENTATIONS OF ContinuousWavelet

Expand Down
9 changes: 9 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,13 @@ end
#...


# ============= wavelet types ================
print("transforms: wavelet types ...\n")

wt = waveletfilter(WT.db2)
@test length(wt) == 4
same = Wavelets.name(wt) == "db2"
@test same

wt = waveletls(WT.db2)
@test Wavelets.name(wt) == "db2"

0 comments on commit f68dc9c

Please sign in to comment.