-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathencode.m
More file actions
33 lines (24 loc) · 689 Bytes
/
Copy pathencode.m
File metadata and controls
33 lines (24 loc) · 689 Bytes
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
function code_word = encode(word, N, k_indices)
% Encodes the binary word into N sized polard code block by using the
% indices provided in k_inidices.
% Check if N is valid
[f, e] = log2(N);
if f ~= 0.5
error('N was not a power of 2!');
end
% Check if word and indices are valid
if length(word) ~= length(k_indices)
error('Word was not the same length as indices!');
end
n = e - 1;
code_word = zeros(1, N);
code_word(k_indices) = word;
base_matrix = [1 1; 0 1];
encoding_matrix = base_matrix;
for i = 1:n-1
encoding_matrix = kron(encoding_matrix, base_matrix);
end
code_word = mod(encoding_matrix * code_word.', 2).';
disp('The code word is:');
disp(code_word);
end