-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCode_block_desegmentation_Z_2048.m
102 lines (79 loc) · 2.16 KB
/
Code_block_desegmentation_Z_2048.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function [b, segErr] = Code_block_desegmentation_Z_2048(c_r, B)
% source: https://github.com/robmaunder/turbo-3gpp-matlab
% according to 5.3 filler bits are unncessary
if B <= 0
error('Unsupported block length');
end
supported_values_of_K = [40:8:511,512:16:1023,1024:32:2047,2048:64:6144];
Z = 2048;
if B <= Z
L = 0;
C = 1;
B_prime = B;
else
L = 24;
C = ceil(B/(Z-L));
B_prime = B+C*L;
end
K_plus = min(supported_values_of_K(C*supported_values_of_K>=B_prime));
if C == 1
C_plus = 1;
K_minus = 0;
C_minus = 0;
elseif C>1
K_minus = max(supported_values_of_K(supported_values_of_K<K_plus));
delta_K = K_plus - K_minus;
C_minus = floor((C*K_plus-B_prime)/delta_K);
C_plus = C-C_minus;
end
K_r = zeros(1,C);
for r = 0:C-1
if r < C_minus
K_r(r+1) = K_minus;
else
K_r(r+1) = K_plus;
end
end
F = C_plus*K_plus + C_minus*K_minus - B_prime;
% sanity check
if F ~= 0
error('We should need no Filler bits, but we need %d.', F);
end
b = zeros(1,B);
segErr = [];
k = F;
s = 0;
for r = 0:C-1
while k < K_r(r+1)-L
b(s+1) = c_r{r+1}(k+1);
k = k+1;
s = s+1;
end
if C>1
a_r = c_r{r+1}(1:K_r(r+1)-L);
% sanity check
if sum(isnan(a_r)) > 0
error('There are NANs in a_r.');
end
a_r(isnan(a_r)) = 0;
%p_r2 = calculate_crc_bits(a_r,G_max);
p_r2 = lteCRCEncode(a_r,'24B');
p_r2 = p_r2(end-23:end);
p_r2 = double(p_r2');
p_r = zeros(1,L);
while k < K_r(r+1)
p_r(k+L-K_r(r+1)+1) = c_r{r+1}(k+1);
k = k+1;
end
if ~isequal(p_r,p_r2)
%b = [];
segErr = [segErr int8(1)];
%return;
end
end
k=0;
end
% convert b to correct format
b = b';
b = int8(b);
end