-
Notifications
You must be signed in to change notification settings - Fork 2
/
berlekamp_decode.m
61 lines (53 loc) · 1.56 KB
/
berlekamp_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
57
58
59
60
61
function [data, decoded_data, error_locations] = ...
berlekamp_decode(data_to_decode)
% Parameters of n = 31, k = 16, t = 3 BCH
n = 31;
t = 3;
k = 16;
m = 5; % 2^5 = 32
% GF field vars
zero = gf(0, m);
one = gf(1, m);
alpha = gf(2, m);
codeword = gf(data_to_decode, m);
% compute the syndromes
S = codeword*(alpha.^([1:2*t]'*fliplr(0:30)))';
L = 0;
k = -1;
sigma = [one gf(zeros(1,(2*t - 1)), m)];
D = [zero one gf(zeros(1,(2*t - 2)), m)];
for n = 0:(2*t - 1)
discrepency = fliplr(S((n-L + 1):(n + 1)))*sigma(1:(L + 1))';
if discrepency ~= 0
sigma_star = sigma - discrepency*D;
if L < n - k
L_star = n - k;
k = n - L;
D = sigma/discrepency;
L = L_star;
end
sigma = sigma_star;
end
D = [zero D(1:(end-1))];
end
% Find roots of sigma
rootsOfSigma = [];
rootToTry = alpha.^(1:31);
for index = 1:length(rootToTry)
% if sum is zero then is root
if sigma*(rootToTry(index).^(0:(length(sigma) - 1)))' == zero
rootsOfSigma = [rootsOfSigma index];
end
end
degree = find(sigma ~= zero);
degree = degree(end) - 1;
error_locations = zeros(1,length(data_to_decode));
if length(rootsOfSigma) < degree
% disp('WARNING: Cannot correct more than 3 errors')
decoded_data = data_to_decode;
data = decoded_data(16:end); % assuming a systematic code
return
end
error_locations(rootsOfSigma) = ones(1,length(rootsOfSigma));
decoded_data = mod(data_to_decode + error_locations,2);
data = decoded_data(16:end); % assuming a systematic code