-
Notifications
You must be signed in to change notification settings - Fork 29
/
opWavelet2.m
390 lines (326 loc) · 13.7 KB
/
opWavelet2.m
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
classdef opWavelet2 < opSpot
%OPWAVELET Wavelet operator.
%
% opWavelet(P,Q,FAMILY) creates a Wavelet operator of given FAMILY for
% signals of size P-by-Q. The wavelet transformation is computed using
% the Rice Wavelet Toolbox. The values supported for FAMILY are
% 'Daubechies' and 'Haar'.
%
% opWavelet(P,Q,FAMILY,FILTER,LEVELS,REDUNDANT,TYPE) allows for four
% additional parameters: FILTER (default 8) specifies the filter length,
% which must be even. LEVELS (default 5) gives the number of levels in
% the transformation. P and Q do not need to be divisible by 2^LEVELS.
% However, if LEVELS is bigger than LOG2(MIN(P,Q)), then LEVELS is
% adjusted to be equal to FLOOR(LOG2(MIN(P,Q))).
% The Boolean field REDUNDANT (default false) indicates whether the wavelet
% is redundant. TYPE (default 'min') indictates what type of solution is
% desired; 'min' for minimum phase, 'max' for maximum phase, and 'mid'
% for mid-phase solutions.
%
% The opWavelet operator is linear but not orthogonal. Therefore, the
% transpose of the operator is not the inverse operator. However, the
% inverse of the operator can be obtained through a left-inverse
% operation. For example:
% W = opWavelet(...)
% y = W*x
% if z = W'*y, then z ~= x
% but, u = W\y, then u = x
% Copyright 2007-2009, Rayan Saab, Ewout van den Berg and Michael P. Friedlander
%
% June 6, 2012: Added mirror symmetric extension of signals that are not
% integer multiples of 2^levels.
% Hassan Mansour (hassanm@cs.ubc.ca)
% June 25, 2012: Overloaded mldivide function to compute the inverse of
% the operator.
% Hassan Mansour (hassanm@cs.ubc.ca)
% Feb 28, 2014: Incorporate fix by Prasad Sudhakar regarding a bug
% in the code for redundant wavelet transforms. This
% issue was independently reported by Stephen Becker.
%
% See the file COPYING.txt for full copyright information.
% Use the command 'spot.gpl' to locate this file.
% http://www.cs.ubc.ca/labs/scl/spot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
properties( SetAccess = private, GetAccess = public )
family = 'Daubechies'; % Wavelet family
lenFilter = 8; % Filter length
filter % Filter computed by daubcqf
levels = 5; % Number of levels
typeFilter = 'min'
redundant = false; % Redundant flag
nseg
signal_dims % Dimensions of the signal domain
coeff_dims % Dimensions of extended coefficients
funHandle % Multiplication function
funHandle2 % Divide function
end % Properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods - public
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% opWavelet. Constructor.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function op = opWavelet2(p,q,family,lenFilter,levels,redundant,typeFilter)
if nargin <= 2 || isempty(family)
family = 'Daubechies';
end
if nargin < 5 || isempty(levels)
levels = 5;
end
if nargin >= 6 && redundant
if p == 1 || q == 1
nseg = levels + 1;
else
nseg = 3*levels + 1;
end
n = p*q;
% find coefficient dimensions
[pext, qext, levels] = CoeffDims(p, q, levels);
m = pext*qext*nseg;
redundant = true;
else
nseg = [];
n = p*q;
% find coefficient dimensions
[pext, qext, levels] = CoeffDims(p, q, levels);
m = pext*qext;
redundant = false;
end
op = op@opSpot('Wavelet2', m, n);
op.signal_dims = [p, q];
op.coeff_dims = [pext, qext];
op.levels = levels;
op.redundant = redundant;
op.nseg = nseg;
if nargin >= 4 && ~isempty(lenFilter)
op.lenFilter = lenFilter;
end
if nargin >= 7 && ischar(typeFilter)
op.typeFilter = typeFilter;
end
switch lower(family)
case {'daubechies'}
op.family = 'Daubechies';
op.filter = spot.rwt.daubcqf(op.lenFilter,op.typeFilter);
case {'haar'}
op.family = 'Haar';
op.filter = spot.rwt.daubcqf(0);
otherwise
error('Wavelet family %s is unknown.', family);
end
% Initialize function handle
if redundant
op.funHandle = @multiply_redundant_intrnl;
op.funHandle2 = @divide_redundant_intrnl;
else
op.funHandle = @multiply_intrnl;
op.funHandle2 = @divide_intrnl;
end
end % function opWavelet
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Divide
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = mldivide(op,x)
y = op.funHandle2(op,x);
end % function multiply
end % methods - public
methods( Access = private )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% matvec. Application of Wavelet operator.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = multiply_intrnl(op,x,mode)
if issparse(x), x = full(x); end
x = double(x);
p = op.signal_dims(1);
q = op.signal_dims(2);
pext = op.coeff_dims(1);
qext = op.coeff_dims(2);
levels = op.levels; filter = op.filter;
% apply matvec operation
R = opExtend(p,q,pext,qext);
if mode == 1
% extend the signal
xext = R*x;
% reshape the extended signal
Xmat = reshape(xext,pext,qext);
if isreal(x)
y = spot.rwt.mdwt(Xmat, filter, levels);
else
y1 = spot.rwt.mdwt(real(Xmat), filter, levels);
y2 = spot.rwt.mdwt(imag(Xmat), filter, levels);
y = y1 + sqrt(-1) * y2;
end
y = y(:);
else % mode == 2
Xmat = reshape(x,pext,qext);
if isreal(x)
y = spot.rwt.midwt(Xmat, filter, levels);
else
y1 = spot.rwt.midwt(real(Xmat), filter, levels);
y2 = spot.rwt.midwt(imag(Xmat), filter, levels);
y = y1 + sqrt(-1) * y2;
end
% apply adjoint of extension operator
y = R'*y(:);
end
end % function matvec
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% matvec_redundant. Application of redundant Wavelet operator.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = multiply_redundant_intrnl(op,x,mode)
if issparse(x), x = full(x); end
x = double(x);
p = op.signal_dims(1);
q = op.signal_dims(2);
pext = op.coeff_dims(1);
qext = op.coeff_dims(2);
nseg = op.nseg;
levels = op.levels; filter = op.filter;
R = opExtend(p,q,pext,qext);
if mode == 1
% extend the signal
xext = R*x;
% reshape the extended signal
Xmat = reshape(xext,pext,qext);
if isreal(x)
[yl,yh] = spot.rwt.mrdwt(Xmat, filter, levels);
y = [yl,yh];
else
[yl1,yh1] = spot.rwt.mrdwt(real(Xmat), filter, levels);
[yl2,yh2] = spot.rwt.mrdwt(imag(Xmat), filter, levels);
y = [yl1,yh1] + sqrt(-1) * [yl2,yh2];
end
y = y(:);
else % mode == 2
xl = reshape(x(1:pext*qext),pext,qext);
xh = reshape(x(pext*qext+1:end),pext,(nseg-1)*qext);
% scaling for transpose instead of inverse
if((p==1) || (q==1))
xl = xl * 2^levels;
for seg = 0:levels-1
idx = seg*qext+1:(seg+1)*qext;
xh(:,idx) = xh(:,idx)*(2^(seg+1));
end
else
xl = xl * (2^levels * 2^levels);
for seg = 0:levels-1
idx = 3*seg*qext+1:3*(seg+1)*qext;
xh(:, idx) = xh(:, idx) * (2^(seg+1) * 2^(seg+1));
end
end
if isreal(x)
y = spot.rwt.mirdwt(xl, xh, filter, levels);
else
y1 = spot.rwt.mirdwt(real(xl), real(xh), filter, levels);
y2 = spot.rwt.mirdwt(imag(xl), imag(xh), filter, levels);
y = y1 + sqrt(-1) * y2;
end
% apply adjoint of extension operator
y = R'*y(:);
end
end % function matvec_redundant
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% divide_intrnl. Application of redundant Wavelet operator.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = divide_intrnl(op,x)
if issparse(x), x = full(x); end
x = double(x);
p = op.signal_dims(1);
q = op.signal_dims(2);
pext = op.coeff_dims(1);
qext = op.coeff_dims(2);
levels = op.levels; filter = op.filter;
Xmat = reshape(x,pext,qext);
if isreal(x)
y = spot.rwt.midwt(Xmat, filter, levels);
else
y1 = spot.rwt.midwt(real(Xmat), filter, levels);
y2 = spot.rwt.midwt(imag(Xmat), filter, levels);
y = y1 + sqrt(-1) * y2;
end
% clip signal back to original dimensions
y = y(1:p, 1:q);
y = y(:);
end % function divide
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% divide_intrnl. Application of redundant Wavelet operator.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = divide_redundant_intrnl(op,x)
if issparse(x), x = full(x); end
x = double(x);
p = op.signal_dims(1);
q = op.signal_dims(2);
pext = op.coeff_dims(1);
qext = op.coeff_dims(2);
nseg = op.nseg;
levels = op.levels; filter = op.filter;
% ii = 1:length(filter);
% filter = (-1).^ii.*(filter);
xl = reshape(x(1:pext*qext),pext,qext);
xh = reshape(x(pext*qext+1:end),pext,(nseg-1)*qext);
if isreal(x)
y = spot.rwt.mirdwt(xl, xh, filter, levels);
else
y1 = spot.rwt.mirdwt(real(xl), real(xh), filter, levels);
y2 = spot.rwt.mirdwt(imag(xl), imag(xh), filter, levels);
y = y1 + sqrt(-1) * y2;
end
% clip signal back to original dimensions
y = y(1:p, 1:q);
y = y(:);
end % function divide
end % methods - private
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods - protected
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods( Access = protected )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiply
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = multiply(op,x,mode)
y = op.funHandle(op,x,mode);
end % function multiply
end % methods - protected
end % classdef
function [pext, qext, levels] = CoeffDims(p, q, levels)
if p >= 2^levels
plevels = levels;
if q >= 2^levels
qext = ceil(q/(2^levels))*2^levels;
elseif q > 1
qlevels = floor(log2(q));
levels = min(plevels,qlevels);
qext = ceil(q/(2^levels))*2^levels;
else
qext = q;
end
pext = ceil(p/(2^levels))*2^levels;
elseif p > 1
plevels = floor(log2(p));
if q >= 2^levels
levels = min(levels,plevels);
qext = ceil(q/(2^levels))*2^levels;
elseif q > 1
qlevels = floor(log2(q));
levels = min(plevels,qlevels);
qext = ceil(q/(2^levels))*2^levels;
else
levels = min(levels,plevels);
qext = q;
end
pext = ceil(p/(2^levels))*2^levels;
else
pext = p;
if q >= 2^levels
qext = ceil(q/(2^levels))*2^levels;
elseif q > 1
levels = floor(log2(q));
qext = ceil(q/(2^levels))*2^levels;
else
qext = q;
end
end
end