2025-03-30 10:31:17 -07:00

223 lines
13 KiB
Matlab

function [DecMatrixZcLatch, CheckMatrix0, CheckMatrix1,CheckDisLatch] = GenBG1Para(Zc,CodeRate,BaseGraph,FileTitle)
% 根据Zc值、校验矩阵的前26列,生成微码所需的参数
% Zc: LDPC编码提升值
% CodeRate: 码率0-3,将BG1拆分成4中不同码率,分别对应
% 4层(26*Zc),17层(39*Zc)、30层(52*Zc)、
% 46层(68*Zc),前两列为zores(1,2*Zc)
% BaseGraph: BG1基本图参数之一(BG1共1张基本图,分为8种参数)
% FileTitle: 文件命名信息标识
% DecMatrixZcLatch: 译码迭代时所需的latch配置矩阵
% CheckMatrix0: A.MREG[128-191]对应的校验矩阵,A指M[0-255]
% CheckMatrix1: B.MREG[128-191]对应的校验矩阵,B指M[256-512]
% CheckDisLatch: 判决结果存储在A&B.M[96-108]中
%% debug start
% clc;clear;
% Zc = 384;
% CodeRate = 3;
% FileTitle = 'Zc384';
% BaseGraph = [... %%zc=384
% 307 19 50 369 -1 181 216 -1 -1 317 288 109 17 357 -1 215 106 -1 242 180 330 346 1 0 -1 -1 -1
% 76 -1 76 73 288 144 -1 331 331 178 -1 295 342 -1 217 99 354 114 -1 331 -1 112 0 0 0 -1 -1
% 205 250 328 -1 332 256 161 267 160 63 129 -1 -1 200 88 53 -1 131 240 205 13 -1 -1 -1 0 0 -1
% 276 87 -1 0 275 -1 199 153 56 -1 132 305 231 341 212 -1 304 300 271 -1 39 357 1 -1 -1 0 -1
% 332 181 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0
% 195 14 -1 115 -1 -1 -1 -1 -1 -1 -1 -1 166 -1 -1 -1 241 -1 -1 -1 -1 51 157 -1 -1 -1 -1
% 278 -1 -1 -1 -1 -1 257 -1 -1 -1 1 351 -1 92 -1 -1 -1 253 18 -1 225 -1 -1 -1 -1 -1 -1
% 9 62 -1 -1 316 -1 -1 333 290 -1 -1 -1 -1 -1 114 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
% 307 179 -1 165 -1 -1 -1 -1 -1 -1 -1 -1 18 -1 -1 -1 39 -1 -1 224 -1 368 67 -1 170 -1 -1
% 366 232 -1 -1 -1 -1 -1 -1 -1 -1 321 133 -1 57 -1 -1 -1 303 63 -1 82 -1 -1 -1 -1 -1 -1
% -1 101 339 -1 274 -1 -1 111 383 -1 -1 -1 -1 -1 354 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
% 48 102 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 8 -1 -1 -1 47 -1 -1 -1 -1 188 334 115 -1 -1 -1
% 77 186 -1 -1 -1 -1 -1 -1 -1 -1 174 232 -1 50 -1 -1 -1 -1 74 -1 -1 -1 -1 -1 -1 -1 -1
% 313 -1 -1 177 -1 -1 -1 266 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 115 -1 -1 370 -1 -1 -1
% 142 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 248 -1 -1 137 89 347 -1 -1 -1 12 -1 -1 -1 -1 -1
% 241 2 -1 -1 -1 -1 -1 -1 -1 -1 210 -1 -1 318 -1 -1 -1 -1 55 -1 -1 -1 -1 -1 -1 269 -1
% -1 13 -1 338 -1 -1 -1 -1 -1 -1 -1 57 -1 -1 -1 -1 -1 -1 -1 -1 289 -1 57 -1 -1 -1 -1
% 260 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 303 -1 81 358 -1 -1 -1 375 -1 -1 -1 -1 -1
% -1 130 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 163 280 -1 -1 -1 -1 132 4 -1 -1 -1 -1 -1 -1 -1
% 145 213 -1 -1 -1 -1 -1 344 242 -1 197 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
% 187 -1 -1 206 -1 -1 -1 -1 -1 264 -1 341 -1 -1 -1 -1 -1 -1 -1 -1 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-1 -1 -1 -1 -1
% 230 -1 -1 -1 -1 -1 -1 148 -1 -1 -1 -1 -1 -1 -1 202 -1 312 -1 -1 -1 -1 -1 -1 -1 -1 -1
% -1 320 -1 -1 -1 -1 335 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 266 -1 -1 -1 -1
% 210 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 313 297 -1 -1 21 -1 -1 -1 -1 -1 -1 -1 -1
% -1 269 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 82 -1 -1 -1 -1 -1 -1 -1 -1 -1 115 -1 -1 -1
% 185 -1 -1 -1 -1 -1 -1 -1 -1 177 289 -1 214 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
% -1 258 -1 93 -1 -1 -1 346 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 297 -1 -1 -1 -1 -1 -1 -1
% 175 -1 -1 -1 -1 -1 -1 -1 37 -1 -1 -1 -1 -1 -1 -1 -1 312 -1 -1 -1 -1 -1 -1 -1 -1 -1
% -1 52 -1 314 -1 -1 -1 -1 -1 139 -1 -1 -1 -1 -1 -1 -1 -1 288 -1 -1 -1 -1 -1 -1 -1 -1
% 113 -1 -1 -1 14 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 218 -1 -1
% -1 113 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 132 -1 114 -1 -1 -1 -1 -1 -1 168 -1
% 80 -1 -1 -1 -1 -1 -1 78 -1 163 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 274 -1 -1 -1 -1
% -1 135 -1 -1 -1 -1 149 -1 -1 -1 15 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1];
%% debug end
%% 函数体
ACodeBlockIndex = [1 2 3 5 6 7 12 13 15 18 20 24 26];
BCodeBlockIndex = [4 8 9 10 11 14 16 17 19 21 22 23 25];
MergedLayerStart = [1 2 3 4 5 6 8 10 13 18 23 26 31 35 40 45]; %%多层参数合并
MergedLayerEnd = [MergedLayerStart(2:end) 46];
ZnBufferMRegStart = double(0x80); %%预计将Zn存储的mreg的128-223中
BitBuffMregStart = double(0x60); %%预计将抽取后的bit存储至96-127中
BaseGraph(BaseGraph~=-1) = mod(BaseGraph(BaseGraph~=-1),Zc);
EleIndex = cell(46,[]);
AMatrixIndexInMReg = cell(46,[]);
BMatrixIndexInMReg = cell(46,[]);
AEleLen = zeros(46,1);
BEleLen = zeros(46,1);
AMatrixValue = cell(46,[]);
BMatrixValue = cell(46,[]);
for i = 1:46
EleIndex{i} = find(BaseGraph(i,1:26)~=-1);
AMatrixIndexInMReg{i} = find(ismember(ACodeBlockIndex,EleIndex{i})~=0); %前26列中13列非0值对应的索引
BMatrixIndexInMReg{i} = find(ismember(BCodeBlockIndex,EleIndex{i})~=0); %前26列中13列非0值对应的索引
AEleLen(i) = length(AMatrixIndexInMReg{i});
BEleLen(i) = length(BMatrixIndexInMReg{i});
AMatrixValue{i} = BaseGraph(i,ACodeBlockIndex(AMatrixIndexInMReg{i}));
BMatrixValue{i} = BaseGraph(i,BCodeBlockIndex(BMatrixIndexInMReg{i}));
end
%% 以校验矩阵的前四层中的有效循环移位值生成校验参数
ACheckMatrix = [AMatrixValue{1} AMatrixValue{2} AMatrixValue{3} AMatrixValue{4}];
BCheckMatrix = [BMatrixValue{1} BMatrixValue{2} BMatrixValue{3} BMatrixValue{4}];
if 64 < Zc
CheckMatrix0 = [bitshift(ACheckMatrix,-3) mod(ACheckMatrix,8)]; %校验矩阵0;校验矩阵中每个有效循环移位值n都拆分成两部分:x=n>>3,y=n&7;
CheckMatrix1 = [bitshift(BCheckMatrix,-3) mod(BCheckMatrix,8)]; %校验矩阵1;便于微码进行循环移位操作
else
CheckMatrix0 = ACheckMatrix; % Zc<=64,判决得到字节级的译码结果,每Zc字节可利用shu进行交织实现循环移位操作
CheckMatrix1 = BCheckMatrix; % 不需要将循环移位值拆解为x,y两部分
end
CheckMatrix0 = Int16ToInt32(CheckMatrix0);
CheckMatrix1 = Int16ToInt32(CheckMatrix1);
%% 以校验矩阵的前四层的有效循环移位值的索引,配合bit抽取后存储的起始地址,生成dis(latch)
ACheckDisLatchL= zeros(1,32);
ACheckDisLatchH= zeros(1,32);
BCheckDisLatchL= zeros(1,32);
BCheckDisLatchH= zeros(1,32);
tmp = [AMatrixIndexInMReg{1} AMatrixIndexInMReg{2}];
ACheckDisLatchL(1:length(tmp)) = tmp+BitBuffMregStart-1;
ACheckDisLatchL(end) = length(tmp);
tmp = [AMatrixIndexInMReg{3} AMatrixIndexInMReg{4}];
ACheckDisLatchH(1:length(tmp)) = tmp+BitBuffMregStart-1;
ACheckDisLatchH(end) = length(tmp);
tmp = [BMatrixIndexInMReg{1} BMatrixIndexInMReg{2}];
BCheckDisLatchL(1:length(tmp)) = tmp+BitBuffMregStart-1;
BCheckDisLatchL(end) = length(tmp);
tmp = [BMatrixIndexInMReg{3} BMatrixIndexInMReg{4}];
BCheckDisLatchH(1:length(tmp)) = tmp+BitBuffMregStart-1;
BCheckDisLatchH(end) = length(tmp);
ACheckDisLatch = [ACheckDisLatchL ACheckDisLatchH];
BCheckDisLatch = [BCheckDisLatchL BCheckDisLatchH];
CheckDisLatch = [ACheckDisLatch BCheckDisLatch];
CheckDisLatch = Int8ToInt32(CheckDisLatch);
%% 生成68Zc的zc(latch)表
MergedLayerEndtmp = MergedLayerEnd;
MergedLayerEndtmp(end) = 47;
ZcLatchLen = length(MergedLayerStart);
ADecMatrixZcLatch = zeros(ZcLatchLen,16);
BDecMatrixZcLatch = zeros(ZcLatchLen,16);
for i = 1:ZcLatchLen
Atmp = [];
Btmp = [];
for j = MergedLayerStart(i):MergedLayerEndtmp(i)-1
Atmp = [Atmp AMatrixValue{j}+bitshift(AMatrixIndexInMReg{j}-1+bitshift(ZnBufferMRegStart,-1),9)];
Btmp = [Btmp BMatrixValue{j}+bitshift(BMatrixIndexInMReg{j}-1+bitshift(ZnBufferMRegStart,-1),9)];
end
AtmpLen = length(Atmp);
BtmpLen = length(Btmp);
ADecMatrixZcLatch(i,1:AtmpLen) = Atmp;
BDecMatrixZcLatch(i,1:BtmpLen) = Btmp;
ADecMatrixZcLatch(i,end) = bitshift(AtmpLen,8); %16*16bit,最高8bit表示
BDecMatrixZcLatch(i,end) = bitshift(BtmpLen,8);
end
ADecMatrixZcLatch = reshape(ADecMatrixZcLatch',32,[]);
BDecMatrixZcLatch = reshape(BDecMatrixZcLatch',32,[]);
DecMatrixZcLatch(:,:,1) = ADecMatrixZcLatch;
DecMatrixZcLatch(:,:,2) = BDecMatrixZcLatch;
DecMatrixZcLatch = permute(DecMatrixZcLatch,[1 3 2]);
DecMatrixZcLatch = Int16ToInt32(DecMatrixZcLatch);
%% 文件生成
if ~strcmp(FileTitle,'null')
if ~exist(FileTitle,'dir')
mkdir(FileTitle);
end
% filename = sprintf('./%s/ABEleLen.dat',FileTitle); %打印出左右元素的个数,打印一次即可
% fid = fopen(filename,'w');
% for i=1:46
% fprintf(fid,'%d/%d:',AEleLen(i),BEleLen(i));
% fprintf(fid,'%4d',ACodeBlockIndex(AMatrixIndexInMReg{i}));
% fprintf(fid,';');
% fprintf(fid,'%4d',BCodeBlockIndex(BMatrixIndexInMReg{i}));
% fprintf(fid,'\n');
% end
% fclose(fid);
filename = sprintf('./%s/%s_CR%d_DecMatrixZcLatch.dat',FileTitle,FileTitle,CodeRate); %存储zcLatch配置,不同Zc,码率,zclatch均不同
fid = fopen(filename,'w');
% fprintf(fid,'0x%08x,0x%08x,0x%08x,0x%08x,\n',DecMatrixZcLatch);
fprintf(fid,'0x%08x,\n',DecMatrixZcLatch);
fclose(fid);
% filename = sprintf('./%s/%s_CR%d_CheckDisLatch.dat',FileTitle,FileTitle,CodeRate); %存储dislatch配置,判决后的bit存在96起始的mreg中,DisLatch只存一次即可
% fid = fopen(filename,'w');
% fprintf(fid,'0x%08x,0x%08x,0x%08x,0x%08x,\n',CheckDisLatch);
% fclose(fid);
AtmpL = length(CheckMatrix0);
filename = sprintf('./%s/%s_CR%d_CheckMatrix0.dat',FileTitle,FileTitle,CodeRate); %存储校验矩阵0,不同zc对应不同的校验矩阵
fid = fopen(filename,'w');
% fprintf(fid,'0x%08x,0x%08x,0x%08x,0x%08x,\n',[CheckMatrix0 zeros(1,ceil(AtmpL/16)*16-AtmpL)]);
fprintf(fid,'0x%08x,\n',[CheckMatrix0 zeros(1,ceil(AtmpL/16)*16-AtmpL)]);
fclose(fid);
BtmpL = length(CheckMatrix1);
filename = sprintf('./%s/%s_CR%d_CheckMatrix1.dat',FileTitle,FileTitle,CodeRate); %存储校验矩阵1,不同zc对应不同的校验矩阵
fid = fopen(filename,'w');
% fprintf(fid,'0x%08x,0x%08x,0x%08x,0x%08x,\n',[CheckMatrix1 zeros(1,ceil(BtmpL/16)*16-BtmpL)]);
fprintf(fid,'0x%08x,\n',[CheckMatrix1 zeros(1,ceil(BtmpL/16)*16-BtmpL)]);
fclose(fid);
end
end
%% 8bit转32bit
% function Int32Out = Int8ToInt32(Int8In)
% tmp = reshape(Int8In,4,[]);
% Int32Out = bitshift(tmp(1,:),0)+bitshift(tmp(2,:),8)+...
% bitshift(tmp(3,:),16)+bitshift(tmp(4,:),24);
% end
%% 16bit转32bit
% function Int32Out = Int16ToInt32(Int16In)
% tmp = reshape(Int16In,2,[]);
% Int32Out = bitshift(tmp(1,:),0)+bitshift(tmp(2,:),16);
% end
%% 提升值和基本图,ZC>64
% mpd(ceil(Zc/64),2)==0 | mpd(ceil(Zc/64),2)==1
% 0 128, 256; |
% 1 96, 384; | 192;
% 2; | 160, 320
% 3 112, 224; |
% 4 72; | 144, 288
% 5 88, 352, | 176
% 6 104, 208; |
% 7 120, 240 |
%%