-
Notifications
You must be signed in to change notification settings - Fork 0
/
graycode.m
41 lines (37 loc) · 1003 Bytes
/
graycode.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
function [P,offset] = graycode(width,height)
% Generate Gray codes for vertical and horizontal stripe patterns.
% See: http://en.wikipedia.org/wiki/Gray_code
P = cell(2,1);
offset = zeros(2,1);
for j = 1:2
% Allocate storage for Gray code stripe pattern.
if j == 1
N = ceil(log2(width));
offset(j) = floor((2^N-width)/2);
else
N = ceil(log2(height));
offset(j) = floor((2^N-height)/2);
end
P{j} = zeros(height,width,N,'uint8');
% Generate N-bit Gray code sequence.
B = zeros(2^N,N,'uint8');
B_char = dec2bin(0:2^N-1);
for i = 1:N
B(:,i) = str2num(B_char(:,i));
end
G = zeros(2^N,N,'uint8');
G(:,1) = B(:,1);
for i = 2:N
G(:,i) = xor(B(:,i-1),B(:,i));
end
% Store Gray code stripe pattern.
if j ==1
for i = 1:N
P{j}(:,:,i) = repmat(G((1:width)+offset(j),i)',height,1);
end
else
for i = 1:N
P{j}(:,:,i) = repmat(G((1:height)+offset(j),i),1,width);
end
end
end