-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathopHadamard.m
78 lines (68 loc) · 2.47 KB
/
opHadamard.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
classdef opHadamard < opSpot
%OPHADAMARD Hadamard matrix.
%
% opHadamard(N) creates a Hadamard operator for vectors of length
% N, where N is a power of two. Multiplication is done using a fast
% routine.
%
% opHadamard(N,NORMALIZED) is the same as above, except that the
% columns are scaled to unit two-norm. By default, the NORMALIZED
% flag is set to FALSE.
% Copyright 2008-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, GetAccess = public )
normalized = false
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods - Public
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
function op = opHadamard(n,normalized)
%opHadamard Constructor
if nargin < 1 || nargin > 2
error('Invalid number of arguments.');
end
if n ~= power(2,round(log2(n)))
error('Dimension must be a power of two.')
end
op = op@opSpot('Hadamard',n,n);
if nargin == 2 && normalized
op.normalized = true;
end
end % Constructor
end % Methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods - protected
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods( Access = protected )
% Multiplication
function y = multiply(op,x,mode)
y = x;
n = op.n;
k = round(log2(n));
b = 1; % Blocks on current level
s = n / 2; % Stride
for i=1:k % Level
for j=0:b-1 % Blocks
for k=1:s % Elements within block
i1 = j*n + k;
i2 = i1 + s;
t1 = y(i1);
t2 = y(i2);
y(i1) = t1 + t2;
y(i2) = t1 - t2;
end
end
b = b * 2; s = s / 2; n = n / 2;
end
if op.normalized
y = y / sqrt(op.n);
end
end % Multiply
end % Methods
end % Classdef