This repository was archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.m
56 lines (44 loc) · 1.65 KB
/
decode.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
function [ u ] = decode( v_rec, g )
% Decodes v_rec according to the generation function g
%
% v_rec : Matrix with each row being a received code word to decode
% g : Generator Polynomial of the cyclic code
%
% u : Matrix with each row being a decoded information word
%% Setup
%
n = size(v_rec, 2);
k = n - length(g) + 1;
no_err = zeros(1,n); % No error = vector of zeros
err = no_err;
err(1) = 1; % One unique error at first position
synd_err = gen_synd(err, g); % Syndrome of our reference error for the given g (cyclic code)
synd = gen_synd(v_rec, g); % Syndromes of all code words
n_rotations = 0; % Number of rotations made on decoding a given word
%% Error identification and correction
%
for i = 1:size(synd,1)
n_rotations = 0; % Reset number of rotations
if synd(i,:) ~= no_err % Check if there is an error on received word
while synd(i,:) ~= synd_err % While the error is not on the first position
synd = rotate_relative(synd(i,:), g); % Rotate syndrome
n_rotations = mod(n_rotations + 1, n);
if n_rotations == 0 % n rotations leads the word back to initial settings
break
end
end
if synd(i,:) == synd_err
v_rec(n_rotations+1) = mod(v_rec(n_rotations+1)+1,2); % Correct error of word
end
end
end
%% Decoding of the code word
% Polynomial form of codes
polyv = fliplr(v_rec);
polyg = fliplr(g);
u = zeros(size(v_rec));
for i = 1:size(v_rec,1)
[q,u(i,:)] = deconv(polyv(i,:), polyg); % Calculate each information word
end
u = mod(fliplr(u), 2);
end