-
Notifications
You must be signed in to change notification settings - Fork 29
/
opConvolve.m
339 lines (281 loc) · 12 KB
/
opConvolve.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
classdef opConvolve < opSpot
%OPCONVOLVE One and two dimensional convolution operator.
%
% opConvolve(M,N,KERNEL,OFFSET,MODE) creates an operator for one or
% two-dimensional convolution, depending on the size of the KERNEL,
% and the matrix or vector (MxN) the convolution is applied to. The
% convolution is one dimensional only if KERNEL is a column vector
% and N=1, or KERNEL is a row vector and M=1. The OFFSET parameter
% determines the center of the KERNEL and has a default value of
% [1,1]. When the OFFSET lies outside the size of the KERNEL, the
% KERNEL is embedded in a zero matrix/vector with appropriate
% center. For one-dimensional convolution, KERNEL may be a
% scalar. Specifying an offset that is not equal to one where the
% corresponding size of the kernel does equal one leads to the
% construction of a two-dimensional convolution operator. There are
% three types of MODE:
%
% MODE = 'regular' - convolve input with kernel;
% 'truncated' - convolve input with kernel, but keep only
% those MxN entries in the result that
% overlap with the input;
% 'cyclic' - do cyclic convolution of the input with a
% kernel that is wrapped around as many
% times as needed.
%
% The output of the convolution operator, like all other
% operators, is in vector form.
% Copyright 2009, Ewout van den Berg and Michael P. Friedlander
% 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)
funHandle % Multiplication function
end % Properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function op = opConvolve(m,n,kernel,offset,mode)
if nargin < 3
error('opConvolve requires at least three parameters.');
end
if nargin < 4, offset = []; end;
if nargin < 5, mode = 'regular'; end;
switch lower(mode)
case {'cyclic'}
cyclic = true; truncated = false;
case {'truncated'}
cyclic = false; truncated = true;
case {'default','regular',''}
cyclic = false; truncated = false;
otherwise
error('Mode parameter must be one of ''regular'', ''cyclic'', or ''truncated''.');
end
% Check if one-dimensional convolution applies
if (n == 1 && size(kernel,2) == 1) || (m == 1 && size(kernel,1) == 1)
convolveOneDim = true;
if length(offset) >= 2
if ((n == 1 && size(kernel,2) == 1 && offset(2) ~= 1) || ...
(m == 1 && size(kernel,1) == 1 && offset(1) ~= 1))
convolveOneDim = false;
else
offset = offset(1) * offset(2);
end
end
else
convolveOneDim = false;
if length(offset) < 2
error('The offset parameter needs to contain at least two entries.');
end
end
if convolveOneDim
% ========= One-dimensional case =========
% Get basic information
if isempty(offset), offset = 1; end;
offset = offset(1);
kernel = kernel(:);
k = length(kernel);
cflag = ~isreal(kernel);
if cyclic
% ========= Cyclic =========
% Ensure offset lies between 1 and m
offset = rem(offset-1,m)+1;
if offset <= 0, offset = offset + m; end;
% Wrap around or zero-pad kernel as needed
if k > m
% Wrap around however many times needed
kernel = [kernel; zeros(rem(m-rem(k,m),m),1)];
kernel = sum(reshape(kernel,m,length(kernel)/m),2);
k = m;
else
% Pad with zeros
kernel = [kernel; zeros(m-k,1)];
k = m;
end
% Apply cyclic shift to correct for offset
kernel = [kernel(offset:end); kernel(1:offset-1)];
% Precompute kernel in frequency domain
fKernel = fft(full(kernel));
% Create function handle and determine operator size
fun = @(x,mode) opConvolveCircular1D_intrnl(fKernel,cflag,x,mode);
nRows = m;
nCols = m;
else
% ========= Regular or truncated convolution =========
% Zero pad kernel if offset lies outside range
if offset < 1
kernel = [zeros(1-offset,1); kernel];
offset = 1;
k = length(kernel);
end
if offset > k
kernel = [kernel; zeros(offset-k,1)];
k = length(kernel);
end
% Shift kernel and add internal padding
kernel = [kernel(offset:end);zeros(m-1,1);kernel(1:offset-1)];
if truncated
idx = 1:m;
else
idx = [length(kernel)-(offset-2):length(kernel), 1:(m+k-offset)];
end
% Precompute kernel in frequency domain
fKernel = fft(full(kernel));
% Create function handle and determine operator size
fun = @(x,mode) opConvolve1D_intrnl(fKernel,k,m,idx,cflag,x,mode);
nRows = length(idx);
nCols = m;
end
else
% ========= Two-dimensional case =========
% Get basic information
if isempty(offset), offset = [1,1]; end;
if length(offset) < 2
error('Offset parameter for 2D convolution needs to contain two entries.');
end
offset = offset(1:2);
k = [size(kernel,1),size(kernel,2)];
cflag = ~isreal(kernel);
if cyclic
% ========= Cyclic =========
% Ensure offset(1) lies between 1 and m
offset(1) = rem(offset(1)-1,m)+1;
if offset(1) <= 0, offset(1) = offset(1) + m; end;
% Ensure offset(2) lies between 1 and n
offset(2) = rem(offset(2)-1,n)+1;
if offset(2) <= 0, offset(2) = offset(2) + n; end;
% Wrap around kernel and zero pad if needed
newKernel = zeros(m,n);
for i=0:ceil(k(1)/m)-1
idx1 = 1:min(m,k(1)-i*m);
for j=0:ceil(k(2)/n)-1
idx2 = 1:min(n,k(2)-j*n);
newKernel(idx1,idx2) = newKernel(idx1,idx2) + kernel(i*m+idx1,j*n+idx2);
end
end
kernel = newKernel;
% Apply cyclic shifts to correct for offset
kernel = [kernel(offset(1):end,offset(2):end), kernel(offset(1):end,1:offset(2)-1); ...
kernel(1:offset(1)-1,offset(2):end), kernel(1:offset(1)-1,1:offset(2)-1)];
% Precompute kernel in frequency domain
fKernel = fft2(full(kernel));
% Create function handle and determine operator size
fun = @(x,mode) opConvolveCircular2D_intrnl(fKernel,m,n,cflag,x,mode);
nRows = m*n;
nCols = m*n;
else
% ========= Regular or truncated convolution =========
% Zero pad kernel if offset lies outside range
if offset(1) < 1
kernel = [zeros(1-offset(1),k(2)); kernel];
offset(1) = 1;
k(1) = size(kernel,1);
end
if offset(1) > k(1)
kernel = [kernel; zeros(offset(1)-k(1),k(2))];
k(1) = size(kernel,1);
end
if offset(2) < 1
kernel = [zeros(k(1),1-offset(2)), kernel];
offset(2) = 1;
k(2) = size(kernel,2);
end
if offset(2) > k(2)
kernel = [kernel, zeros(k(1),offset(2)-k(2))];
k(2) = size(kernel,2);
end
% Shift kernel and add internal padding
kernel = [kernel(offset(1):end,:);zeros(m-1,k(2));kernel(1:offset(1)-1,:)];
kernel = [kernel(:,offset(2):end),zeros(size(kernel,1),n-1),kernel(:,1:offset(2)-1)];
if truncated
idx1 = 1:m;
idx2 = 1:n;
else
idx1 = [size(kernel,1)-(offset(1)-2):size(kernel,1), 1:(m+k(1)-offset(1))];
idx2 = [size(kernel,2)-(offset(2)-2):size(kernel,2), 1:(n+k(2)-offset(2))];
end
% Precompute kernel in frequency domain
fKernel = fft2(full(kernel));
% Create function handle and determine operator size
fun = @(x,mode) opConvolve2D_intrnl(fKernel,k,m,n,idx1,idx2,cflag,x,mode);
nRows = length(idx1) * length(idx2);
nCols = m*n;
end
end
% Construct operator
op = op@opSpot('Convolve', nRows, nCols);
op.cflag = cflag;
op.funHandle = fun;
end % Constructor
end % Methods
methods ( Access = protected )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiply
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = multiply(op,x,mode)
y = op.funHandle(x,mode);
end % Multiply
end % Methods
end % Classdef
%=======================================================================
function y = opConvolve1D_intrnl(fKernel,k,m,idx,cflag,x,mode)
if mode == 1
fx = fft([full(x);zeros(k-1,1)]);
y = ifft(fKernel.*fx);
y = y(idx);
if (~cflag && isreal(x)), y = real(y); end;
else
z = zeros(m+k-1,1);
z(idx) = full(x);
y = ifft(conj(fKernel).*fft(z));
y = y(1:m);
if (~cflag && isreal(x)), y = real(y); end;
end
end
%======================================================================
function y = opConvolveCircular1D_intrnl(fKernel,cflag,x,mode)
if mode == 1
y = ifft(fKernel.*fft(full(x)));
if (~cflag && isreal(x)), y = real(y); end;
else
y = ifft(conj(fKernel).*fft(full(x)));
if (~cflag && isreal(x)), y = real(y); end;
end
end
%======================================================================
function y = opConvolve2D_intrnl(fKernel,k,m,n,idx1,idx2,cflag,x,mode)
if mode == 1
fx = fft2([full(reshape(x,m,n)), zeros(m,k(2)-1); ...
zeros(k(1)-1,n), zeros(k(1)-1,k(2)-1)]);
y = ifft2(fKernel.*fx);
y = y(idx1,idx2);
y = y(:);
if (~cflag && isreal(x)), y = real(y); end;
else
z = zeros(m+k(1)-1,n+k(2)-1);
z(idx1,idx2) = full(reshape(x,length(idx1),length(idx2)));
y = ifft2(conj(fKernel).*fft2(z));
y = y(1:m,1:n);
y = y(:);
if (~cflag && isreal(x)), y = real(y); end;
end
end
%======================================================================
function y = opConvolveCircular2D_intrnl(fKernel,m,n,cflag,x,mode)
if mode == 1
y = ifft2(fKernel.*fft2(full(reshape(x,m,n))));
y = y(:);
if (~cflag && isreal(x)), y = real(y); end;
else
y = ifft2(conj(fKernel).*fft2(full(reshape(x,m,n))));
y = y(:);
if (~cflag && isreal(x)), y = real(y); end;
end
end