/
transforms.jl
268 lines (223 loc) · 8.1 KB
/
transforms.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
module Transforms
using ..Util, ..WT
using Compat
export dwt, idwt, dwt!, idwt!,
wpt, iwpt, wpt!, iwpt!,
dwtc, idwtc
# TODO Use StridedArray instead of AbstractArray where writing to array.
typealias DWTArray AbstractArray
typealias WPTArray AbstractVector
# DWT (discrete wavelet transform)
for (Xwt, Xwt!, _Xwt!, fw) in ((:dwt, :dwt!, :_dwt!, true),
(:idwt, :idwt!, :_dwt!, false))
@eval begin
# filter
function ($Xwt){T<:AbstractFloat}(x::DWTArray{T},
filter::OrthoFilter,
L::Integer=maxtransformlevels(x))
y = Array(T, size(x))
return ($_Xwt!)(y, x, filter, L, $fw)
end
function ($Xwt!){T<:AbstractFloat}(y::DWTArray{T}, x::DWTArray{T},
filter::OrthoFilter,
L::Integer=maxtransformlevels(x))
return ($_Xwt!)(y, x, filter, L, $fw)
end
# lifting
function ($Xwt){T<:AbstractFloat}(x::DWTArray{T},
scheme::GLS,
L::Integer=maxtransformlevels(x))
y = Array(T, size(x))
copy!(y, x)
return ($_Xwt!)(y, scheme, L, $fw)
end
function ($Xwt!){T<:AbstractFloat}(y::DWTArray{T},
scheme::GLS,
L::Integer=maxtransformlevels(x))
return ($_Xwt!)(y, scheme, L, $fw)
end
end # begin
end # for
@doc """
`dwt(x, wt[, L=maxtransformlevels(x)])`
Perform a discrete wavelet transform of the array `x`.
The wavelet type `wt` determines the transform type
(filter or lifting) and the wavelet class, see `wavelet`.
The number of transformation levels `L` can be any non-negative
integer such that the size of `x` is divisible by `L`.
Performs the identity transformation if `L==0`.
**Example:**
```julia
dwt(x, wavelet(WT.coif6))
```
**See also:** `idwt`, `dwt!`, `wavelet`
""" -> dwt
@doc """
`idwt(x, wt[, L=maxtransformlevels(x)])`
Perform an inverse discrete wavelet transform of the array `x`,
the inverse of `dwt(x, wt, L)`.
**See also:** `dwt`, `idwt!`
""" -> idwt
@doc """
`dwt!(y, x, wt::OrthoFilter[, L=maxtransformlevels(x)])`
`dwt!(y, wt::GLS[, L=maxtransformlevels(x)])`
Same as `dwt` but without array allocation.
Perform "out of place" transform with a filter, or
a inplace transform with a lifting scheme. The difference
between the filter and lifting methods is due to the
structure of the transform algorithms.
**See also:** `idwt!`
""" -> dwt!
@doc """
`idwt!(y, x, wt::OrthoFilter[, L=maxtransformlevels(x)])`
`idwt!(y, wt::GLS[, L=maxtransformlevels(x)])`
The inverse of `dwt!`.
**See also:** `dwt!`
""" -> idwt!
# WPT (wavelet packet transform)
for (Xwt, Xwt!, _Xwt!, fw) in ((:wpt, :wpt!, :_wpt!, true),
(:iwpt, :iwpt!, :_wpt!, false))
@eval begin
function ($Xwt){T<:AbstractFloat}(x::WPTArray{T},
wt::DiscreteWavelet,
L::Integer=maxtransformlevels(x))
return ($Xwt)(x, wt, maketree(length(x), L, :full))
end
# filter
function ($Xwt){T<:AbstractFloat}(x::WPTArray{T},
filter::OrthoFilter,
tree::BitVector=maketree(x, :full))
y = Array(T, size(x))
return ($_Xwt!)(y, x, filter, tree, $fw)
end
function ($Xwt!){T<:AbstractFloat}(y::WPTArray{T}, x::WPTArray{T},
filter::OrthoFilter,
tree::BitVector=maketree(x, :full))
return ($_Xwt!)(y, x, filter, tree, $fw)
end
function ($Xwt!){T<:AbstractFloat}(y::WPTArray{T}, x::WPTArray{T},
filter::OrthoFilter,
L::Integer=maxtransformlevels(x))
return ($Xwt!)(y, x, filter, maketree(length(x), L, :full))
end
# lifting
function ($Xwt){T<:AbstractFloat}(x::WPTArray{T},
scheme::GLS,
tree::BitVector=maketree(x, :full))
y = Array(T, size(x))
copy!(y, x)
return ($_Xwt!)(y, scheme, tree, $fw)
end
function ($Xwt!){T<:AbstractFloat}(y::WPTArray{T},
scheme::GLS,
tree::BitVector=maketree(y, :full))
return ($_Xwt!)(y, scheme, tree, $fw)
end
function ($Xwt!){T<:AbstractFloat}(y::WPTArray{T},
scheme::GLS,
L::Integer=maxtransformlevels(x))
return ($Xwt!)(y, scheme, maketree(length(x), L, :full))
end
end # begin
end # for
# DWTC (column-wise discrete wavelet transform)
#dwtc(::AbstractArray, ::DiscreteWavelet)
#idwtc(::AbstractArray, ::DiscreteWavelet)
# SWT (stationary wavelet transform)
#swt(::AbstractVector, ::DiscreteWavelet)
#iswt(::AbstractVector, ::DiscreteWavelet)
# CWT (continuous wavelet transform)
#cwt(::AbstractVector, ::ContinuousWavelet)
#icwt(::AbstractVector, ::ContinuousWavelet)
# CWTFT (continuous wavelet transform via FFT)
#cwtft(::AbstractVector, ::ContinuousWavelet)
#icwtft(::AbstractVector, ::ContinuousWavelet)
@deprecate dwt!(y, x, filter::OrthoFilter, L, fw) (fw ? dwt!(y,x,filter,L) : idwt!(y,x,filter,L))
@deprecate dwt!(y, scheme::GLS, L, fw) (fw ? dwt!(y,scheme,L) : idwt!(y,scheme,L))
@deprecate wpt!(y, x, filter::OrthoFilter, L, fw) (fw ? wpt!(y,x,filter,L) : iwpt!(y,x,filter,L))
@deprecate wpt!(y, scheme::GLS, L, fw) (fw ? wpt!(y,scheme,L) : wpt!(y,scheme,L))
# Int -> Float
for Xwt in (:dwt, :idwt, :dwtc, :idwtc, :wpt, :iwpt)
@eval begin
($Xwt){T<:Integer}(x::AbstractArray{T}, args...) = ($Xwt)(float(x), args...)
end
end
# non-exported "out of place" functions
for (Xwt_oop!, Xwt!) in ((:dwt_oop!, :dwt!), (:idwt_oop!, :idwt!))
@eval begin
# filter
function ($Xwt_oop!){T<:AbstractFloat}(y::DWTArray{T}, x::DWTArray{T},
filter::OrthoFilter,
L::Integer=maxtransformlevels(x))
return ($Xwt!)(y, x, filter, L)
end
# lifting
function ($Xwt_oop!){T<:AbstractFloat}(y::DWTArray{T}, x::DWTArray{T},
scheme::GLS,
L::Integer=maxtransformlevels(x))
copy!(y, x)
return ($Xwt!)(y, scheme, L)
end
end # begin
end # for
# column-wise transforms or color images, transform each x[:,...,:,i] separately (default)
# or transform each x[:,...,i,:,...] separately at dim td
for (Xwtc, Xwt) in ((:dwtc, :dwt!), (:idwtc, :idwt!))
@eval begin
function $Xwtc{T<:AbstractFloat}(x::AbstractArray{T}, wt::DiscreteWavelet, L::Integer, td::Integer=ndims(x))
dim = ndims(x)
(1 <= td <= dim) || throw(BoundsError())
sizex = size(x)
sizexc = maketfsize(sizex, td)
y = Array(T, sizex)
xc = Array(T, sizexc)
yc = Array(T, sizexc)
ind = Array(Any, dim)
for i = 1:dim
ind[i] = 1:sizex[i]
end
for d = 1:sizex[td]
ind[td] = d
if dim > 2
xc[:] = x[ind...]
($Xwt)(yc, xc, wt, L)
y[ind...] = yc
else # fast copy methods in 2-D
Util.copygeneral2!(xc, x, ind...)
($Xwt)(yc, xc, wt, L)
Util.copygeneral1!(y, ind..., yc)
end
end
return y
end
end
end
# utils
# for dwtc
function maketfsize(t::NTuple, td::Integer)
s = Array(eltype(t[1]), length(t)-1)
k = 1
for i=1:length(t)
if i != td
s[k] = t[i]
k += 1
end
end
return tuple(s...)
end
# Array with shared memory
function unsafe_vectorslice(A::Array, i::Int, n::Int)
return pointer_to_array(pointer(A, i), n, false)::Vector
end
# 2-D
row_idx(i, n) = i
col_idx(i, n) = 1 + (i-1)*n
# 3-D
row_idx(i, j, n) = row_idx(i, n) + (j-1)*n*n
col_idx(i, j, n) = col_idx(i, n) + (j-1)*n*n
hei_idx(i, j, n) = i + (j-1)*n
# filter transforms
include("transforms_filter.jl")
# lifting transforms
include("transforms_lifting.jl")
end # module