forked from talenik/YALDPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQCLDPCEncode.m
151 lines (131 loc) · 3.94 KB
/
QCLDPCEncode.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function CW = QCLDPCEncode( p1, p2, p3 )
%QCLDPCEncode - encode data using one of the QC LDPC codes defined in WiMAX or WIFI6
% internally calls the QCLDPCEncodeMEX MEX file
%
% enc = QCLDPCEncode()
% return default encoder options structure
% default encoder method is 'array'
% method 'bitmap' is valid only for select parameters
%
% enc = QCLDPCEncode( enc )
% recompute dependent parameters
%
% QCLDPCEncode( code, enc )
% test code and encoder parameters compatibility
% throws error if parameters configuration is unsupported/invalid
%
% CW = QCLDPCEncode( data, code, enc )
% encode Data using given parameters
%
% data - column vector or matrix of column vectors
% for 'array' type encoder:
% default data type is 'uint8', storing only values 0 or 1
% other types are auto-converted
% nr. of rows must == code.K
%
% for 'bitmap' type encoder:
% only uint8, uint16, uint32 data types supported
% assumed to be bitmaps
% nr. of rows must == code.K / enc.wb
%
% code - code options structure, see loadQCLDPC()
% enc - encoder options structure
%
% compatible MEX file must first be built using saveLDPCheader() and buildMEXfile()
% see testEnc for examples
%default options:
dopts.dbglev = 0 ; % 0 > silent, only works with debug build
dopts.build = 'release' ;
%build options
dopts.method = 'array' ; % 'array' or 'bitmap'
dopts.mexfun = 'QCLDPCEncodeMEX' ;
dopts.sources = [ "encoder.c" "debug.c" "ldpc.c" ] ;
%default options for the bitmap method:
dopts.type = 'uint8' ;
dopts = bitmapParams( dopts.type, dopts ) ;
%dopts = mergeStructs( dopts, bopts ) ;
if nargin == 0
%just return default options
CW = dopts ;
elseif nargin == 1
if ~isstruct( p1 )
error('Usage: enc = QCLDPCEncode( enc )') ;
end
CW = bitmapParams( p1.type, p1 ) ;
elseif nargin == 2
%just test code and encoder parameters are set correctly
CW = paramsOK( p1, p2 ) ;
elseif nargin == 3
%actual encoding
if paramsOK( p2, p3 )
CW = encode( p1, p3 ) ;
end
else
error('Unsupported parameters combination. See help for usage.')
end
end
function OK = paramsOK( code, enc )
if strcmp( code.std, 'wifi' )
if strcmp( enc.method, 'bitmap' )
error( "Bitmap encoding unsupported for WIFI parameters.") ;
end
elseif strcmp( code.std, 'wimax' )
if strcmp( enc.method, 'bitmap' )
%only select subset of code parameters supported
if mod( code.N, enc.wb ) ~= 0 || mod( code.K, enc.wb ) ~= 0 || mod( code.M, enc.wb ) ~= 0
error( "Bitmap encoding unsupported parameters: N,K,M must be disible by WB.") ;
end
end
else
error( "Unsupported standard, set: 'wimax' of 'wifi'.") ;
end
OK = true ;
end
function CW = encode( Data, enc )
Options = [ enc.dbglev, 0 ] ; %method option is unused now
if strcmp( enc.method, 'array')
%for 'array' encoder type auto-convert the Data to enc.type
assert( isBinary( Data ) ) ;
if ~isa( Data, enc.type )
t = class( Data ) ;
UData = cast( Data, enc.type ) ;
UPar = QCLDPCEncodeMEX( UData, Options ) ;
Par = cast( UPar, t ) ;
CW = [ Data ; Par ] ;
else
Par = QCLDPCEncodeMEX( Data, Options ) ;
CW = [ Data ; Par ] ;
end
else
if ~isa( Data, enc.type )
error( "For bitmap encoding you must explicitly use the Data of encoder type.") ;
end
%no auto-datatype conversion performed for 'bitmap' encoder
Par = QCLDPCEncodeMEX( Data, Options ) ;
CW = [ Data ; Par ] ;
end
end
function encoder = bitmapParams( t , encoder )
encoder.type = t ;
switch t
case 'uint64'
w = 64 ;
encoder.ctype = 'uint64_t' ;
case 'uint32'
w = 32 ;
encoder.ctype = 'uint32_t' ;
case 'uint16'
w = 16 ;
encoder.ctype = 'uint16_t' ;
case 'uint8'
w = 8 ;
encoder.ctype = 'uint8_t' ;
otherwise
error('Unsupported type.') ;
end
ws = num2str( w ) ;
encoder.wb = w ;
encoder.mextype = [ 'mxUint' ws ] ;
encoder.mexclass = [ 'mxUINT' ws '_CLASS' ] ;
encoder.mexget = [ 'mxGetUint' ws 's' ] ;
end