-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdec2gc.m
62 lines (48 loc) · 1.48 KB
/
dec2gc.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
function gcode = dec2gc(dec,N)
% Description: This function converts a decimal number to its equivalent
% gray code representation.
%
% Call Syntax: [output_variables] = function_name(input_variables)
%
% Input Arguments:
% Name: dec
% Type: vector
% Description: real positive numeric column vector
% Name: N
% Type: scalar
% Description: precision for the gray code
% Output Arguments:
% Name: gcode
% Type: matrix
% Description: gray code of size length(dec) x N as an integer matrix
%
% Creation Date: 9/17/2005
% Author : Arjun Srinivasan Rangamani
%*************************************************************************
%------------------
% Check valid input
%------------------
if (nargin ~= 2)
error('Error (dec2gc): must have 2 input arguments.');
end
if (isnumeric(dec) ~= 1 | isreal(dec) ~= 1 | min(dec) < 0)
error('Error (dec2gc): input parameter must be a real positive numeric column vector ');
end
bin = dec2bin(round(dec),N); %change the real value to binary representation
len = size(bin,2);
gcode = zeros(size(bin));
for row = 1:size(bin,1)
temp = bin(row,:);
if len > 1
for col = len:-1:2
if str2num(temp(col-1)) == 1
gcode(row,col) = 1 - str2num(temp(col));
else
gcode(row,col) = str2num(temp(col));
end
gcode(row,1) = str2num(temp(1));
end
else
gcode(row,len) = str2num(temp);
end
end