LTE协议中的算法设计——查表法求取CRC

LTE协议中的算法设计——查表法求取CRCLTE协议中的算法设计——查表法求取CRCLTE协议中的算法设计——查表法求取CRCLTE协议中的算法设计——查表法求取CRCLTE协议中的算法设计——查表法求取CRCLTE协议中的算法设计——查表法求取CRCLTE协议中的算法设计——查表法求取CRCLTE协议中的算法设计——查表法求取CRC

附:博客中的仿真代码(我用matlab做了个代码模板的生成器,但还不太成熟,所以可能这代码看起来格式有点诡异,就凑合看吧,但功能是没有问题的,我用仿真验证过,可以支持LTE协议中定义的CRC_24A/CRC_24B/CRC_16/CRC_8,生成结果与matlab提供的库函数给出的结果完全一致)

%% description

%{

Name: Lte_CrcAdd

function: CRC attachmentinput parameter:

  -> CrcPara_Stru,the parameter related with CRC attachement

  -> InBitStream_Vec,the orignal bit stream

output parameter:

  -> OutBitStream_Vec,the output bit stream with CRC

INFO: generated by code auto-generator in 2018-42-11 10:42:45 +0900

%}

function OutBitStream_Vec = Lte_CrcAdd(CrcPara_Stru, InBitStream_Vec)

%% NOTE

% TO BE ADDED

 

%% Check Input Parameter

% TO BE ADDED

 

%% set parameter

% TO BE ADDED

CrcType_Enum = CrcPara_Stru.CrcType_Enum;

InBitSByteNum = CrcPara_Stru.TBS/8;

 

% get the sequence length

if CrcType_Enum == 2%crctype_CRC_16_ENUM

    CrcSeqLen = 16;

else

    if CrcType_Enum == 3%crctype_CRC_8_ENUM

        CrcSeqLen = 8;

    else

        CrcSeqLen = 24;

    end

end

 

%% initialize output

% TO BE ADDED

OutBitStream_Vec = zeros(length(InBitStream_Vec) + CrcSeqLen,1);

OutBitStream_Vec(1:length(InBitStream_Vec)) = InBitStream_Vec;

%% main process

% THE MAIN PROCESS OF THIS FUNCTION CAN BE ADDED HERE!

 

% step 1: generate the table

% TO BE OPT, the table would be saved as constant table

CrcTab_Arr = Lte_CrcTabGen(CrcType_Enum + 1);

 

% step 2: generate the CRC based on the table

%calcuation

CrcSeq = 0;

for LoopByteCnt = 1:InBitSByteNum

    

    

    % NOTE:

    % for the CRC table,

    % the index 'ByteValue' = [bit0,bit1,...,bitK].*(2.^[0:1:K]

    % the content 'CrcValue' = [crc0,...,crcN].*(2.^[0:1:N]

    % however for the inpu source stream defined in LTE, (TS36.212,5.1.1)

    % the order of bit in one byte is, [bit7,bit6,...,bit0]

    % and the required order for CRC is , [crcK,...crc0]

    

    % get BYTE[N-k]

    % Note: the order of byte in InBitStream_Vec is [bit7, bit6, ... , bit0]

    InBitsByte = sum(InBitStream_Vec(LoopByteCnt*8:-1:LoopByteCnt*8-7).*(2.^([0:1:7].')));

    

    % calculation of ByteSeq

    ByteSeq = bitxor(bitshift(CrcSeq,-(CrcSeqLen-8)),InBitsByte);

    

    % get CRC of ByteSeq based on the table

    CrcSeq1 = CrcTab_Arr(ByteSeq+1);

    

    % update CrcSeq

    if CrcSeqLen == 8

        tmpCrcSeq = 0;

    else

        tmpCrcSeq = bitand(CrcSeq,2^(CrcSeqLen-8)-1)*2^8;

    end

    CrcSeq = bitxor(tmpCrcSeq,CrcSeq1);

end

 

% get the CRC sequence

CrcSeq_Vec = zeros(CrcSeqLen,1);

for BitCnt = 1:CrcSeqLen

    if bitand(CrcSeq,2^(BitCnt-1))~=0

        CrcSeq_Vec(BitCnt) = 1;

    end

end

 

% step 3: output the sequence with CRC

OutBitStream_Vec(end:-1:length(InBitStream_Vec)+1) = CrcSeq_Vec;

end

 

%% sub-function for this function

% TO BE ADDED

 

% generate the table for CRC adding/checking

function CrcTab = Lte_CrcTabGen(crc_type)

 

% % initialize output

% CrcTab = zeros(256,1);

%

% % get the generation polynomial

% switch crc_type

%     case 1 % CRC_24A

%         g = 'z^24 + z^23 + z^18 + z^17 + z^14 + z^11 + z^10 + z^7 + z^6 + z^5 + z^4 + z^3 + z + 1';

%         CrcLen = 24;

%     case 2 % CRC_24B

%         g = 'z^24 + z^23 + z^6 + z^5 + z + 1';

%         CrcLen = 24;

%     case 3 % CRC_16

%         g = 'z^16 + z^12 + z^5 + 1';

%         CrcLen = 16;

%     case 4 % CRC8

%         g = 'z^8 + z^7 + z^4 + z^3 + z + 1';

%         CrcLen = 8;

%     otherwise

%         error('error crc_type: %d\n',crc_type);

% end

% hTBCRCGen = comm.CRCGenerator('Polynomial',g);

%

% % generate the table

% for ByteValueIdx = 1:256

%     

%     % NOTE: for matlab, the order of output sequence is shown as below,

%     % [bit0,bit1,...,bitK] -> [bit0,bit1,...,bitK,crc0,crc1,...,crcN]

%     

%     InputByte_Vec = zeros(1,8);

%     for BitIdx = 1:8

%         if bitand((ByteValueIdx-1),2^(BitIdx-1)) ~= 0

%             InputByte_Vec(9-BitIdx) = 1;

%         end

%     end

%     

%     % NOTE: the inpu sequence should be column vector

%     tmpCrc_Vec = step(hTBCRCGen, InputByte_Vec.');

%     tmpCrc = 0;

%     for BitCnt = 1:CrcLen

%         tmpCrc =  tmpCrc_Vec(BitCnt+8) * (2^ (CrcLen-BitCnt)) + tmpCrc;

%     end

%     CrcTab(ByteValueIdx) = tmpCrc;

% end

%

% release(hTBCRCGen);

 

switch crc_type

    case 1  %CRC24A

        CrcTab = [0;8801531;9098509;825846;9692897;1419802;1651692;10452759;10584377;2608578;2839604;11344079;3303384;11807523;12104405;4128302;12930697;4391538;5217156;13227903;5679208;13690003;14450021;5910942;6606768;14844747;15604413;6837830;16197969;7431594;8256604;16494759;840169;9084178;8783076;18463;10434312;1670131;1434117;9678590;11358416;2825259;2590173;10602790;4109873;12122826;11821884;3289031;13213536;5231515;4409965;12912278;5929345;14431610;13675660;5693559;6823513;15618722;14863188;6588335;16513208;8238147;7417269;16212302;1680338;10481449;9664223;1391140;9061683;788936;36926;8838341;12067563;4091408;3340262;11844381;2868234;11372785;10555655;2579964;14478683;5939616;5650518;13661357;5180346;13190977;12967607;4428364;8219746;16457881;16234863;7468436;15633027;6866552;6578062;14816117;1405499;9649856;10463030;1698765;8819930;55329;803287;9047340;11858690;3325945;4072975;12086004;2561507;10574104;11387118;2853909;13647026;5664841;5958079;14460228;4446803;12949160;13176670;5194661;7454091;16249200;16476294;8201341;14834538;6559633;6852199;15647388;3360676;11864927;12161705;4185682;10527045;2551230;2782280;11286707;9619101;1346150;1577872;10379115;73852;8875143;9172337;899466;16124205;7357910;8182816;16421083;6680524;14918455;15678145;6911546;5736468;13747439;14507289;5968354;12873461;4334094;5159928;13170435;4167245;12180150;11879232;3346363;11301036;2767959;2532769;10545498;10360692;1596303;1360505;9604738;913813;9157998;8856728;92259;16439492;8164415;7343561;16138546;6897189;15692510;14936872;6662099;5986813;14488838;13733104;5750795;13156124;5174247;4352529;12855018;2810998;11315341;10498427;2522496;12124823;4148844;3397530;11901793;9135439;862644;110658;8912057;1606574;10407765;9590435;1317464;15706879;6940164;6651890;14889737;8145950;16384229;16161043;7394792;5123014;13133629;12910283;4370992;14535975;5997020;5707818;13718737;2504095;10516836;11329682;2796649;11916158;3383173;4130419;12143240;8893606;129117;876971;9121104;1331783;9576124;10389322;1625009;14908182;6633453;6925851;15721184;7380471;16175372;16402682;8127489;4389423;12891860;13119266;5137369;13704398;5722165;6015427;14517560];

    case 2  %CRC24B

        CrcTab = [0;8388707;8388773;198;8388905;330;396;8389103;8389169;594;660;8389367;792;8389499;8389565;990;8389633;1122;1188;8389831;1320;8389963;8390029;1518;1584;8390227;8390293;1782;8390425;1914;1980;8390623;8390753;2050;2244;8390823;2376;8390955;8391149;2446;2640;8391219;8391413;2710;8391545;2842;3036;8391615;3168;8391683;8391877;3238;8392009;3370;3564;8392079;8392273;3634;3828;8392343;3960;8392475;8392669;4030;8392865;4290;4100;8392807;4488;8393195;8393005;4430;4752;8393459;8393269;4694;8393657;5082;4892;8393599;5280;8393923;8393733;5222;8394121;5610;5420;8394063;8394385;5874;5684;8394327;6072;8394715;8394525;6014;6336;8394915;8394853;6150;8395241;6538;6476;8395055;8395505;6802;6740;8395319;7128;8395707;8395645;6942;8395969;7330;7268;8395783;7656;8396171;8396109;7470;7920;8396435;8396373;7734;8396761;8122;8060;8396575;8397089;8514;8580;8397287;8200;8396907;8396973;8398;8976;8397683;8397749;9174;8397369;8794;8860;8397567;9504;8398147;8398213;9702;8397833;9322;9388;8398031;8398609;10098;10164;8398807;9784;8398427;8398493;9982;10560;8399139;8399333;10630;8398953;10250;10444;8399023;8399729;11026;11220;8399799;10840;8399419;8399613;10910;8400193;11554;11748;8400263;11368;8399883;8400077;11438;12144;8400659;8400853;12214;8400473;11834;12028;8400543;12672;8401379;8401189;12614;8401065;12490;12300;8401007;8401841;13266;13076;8401783;12952;8401659;8401469;12894;8402305;13794;13604;8402247;13480;8402123;8401933;13422;14256;8402899;8402709;14198;8402585;14074;13884;8402527;8403425;14722;14660;8403239;14536;8403115;8403053;14350;15312;8403891;8403829;15126;8403705;15002;14940;8403519;15840;8404355;8404293;15654;8404169;15530;15468;8403983;8404945;16306;16244;8404759;16120;8404635;8404573;15934];

    case 3  %CRC16

        CrcTab = [0;4129;8258;12387;16516;20645;24774;28903;33032;37161;41290;45419;49548;53677;57806;61935;4657;528;12915;8786;21173;17044;29431;25302;37689;33560;45947;41818;54205;50076;62463;58334;9314;13379;1056;5121;25830;29895;17572;21637;42346;46411;34088;38153;58862;62927;50604;54669;13907;9842;5649;1584;30423;26358;22165;18100;46939;42874;38681;34616;63455;59390;55197;51132;18628;22757;26758;30887;2112;6241;10242;14371;51660;55789;59790;63919;35144;39273;43274;47403;23285;19156;31415;27286;6769;2640;14899;10770;56317;52188;64447;60318;39801;35672;47931;43802;27814;31879;19684;23749;11298;15363;3168;7233;60846;64911;52716;56781;44330;48395;36200;40265;32407;28342;24277;20212;15891;11826;7761;3696;65439;61374;57309;53244;48923;44858;40793;36728;37256;33193;45514;41451;53516;49453;61774;57711;4224;161;12482;8419;20484;16421;28742;24679;33721;37784;41979;46042;49981;54044;58239;62302;689;4752;8947;13010;16949;21012;25207;29270;46570;42443;38312;34185;62830;58703;54572;50445;13538;9411;5280;1153;29798;25671;21540;17413;42971;47098;34713;38840;59231;63358;50973;55100;9939;14066;1681;5808;26199;30326;17941;22068;55628;51565;63758;59695;39368;35305;47498;43435;22596;18533;30726;26663;6336;2273;14466;10403;52093;56156;60223;64286;35833;39896;43963;48026;19061;23124;27191;31254;2801;6864;10931;14994;64814;60687;56684;52557;48554;44427;40424;36297;31782;27655;23652;19525;15522;11395;7392;3265;61215;65342;53085;57212;44955;49082;36825;40952;28183;32310;20053;24180;11923;16050;3793;7920];

    case 4  %CRC8

        CrcTab = [[0;155;173;54;193;90;108;247;25;130;180;47;216;67;117;238;50;169;159;4;243;104;94;197;43;176;134;29;234;113;71;220;100;255;201;82;165;62;8;147;125;230;208;75;188;39;17;138;86;205;251;96;151;12;58;161;79;212;226;121;142;21;35;184;200;83;101;254;9;146;164;63;209;74;124;231;16;139;189;38;250;97;87;204;59;160;150;13;227;120;78;213;34;185;143;20;172;55;1;154;109;246;192;91;181;46;24;131;116;239;217;66;158;5;51;168;95;196;242;105;135;28;42;177;70;221;235;112;11;144;166;61;202;81;103;252;18;137;191;36;211;72;126;229;57;162;148;15;248;99;85;206;32;187;141;22;225;122;76;215;111;244;194;89;174;53;3;152;118;237;219;64;183;44;26;129;93;198;240;107;156;7;49;170;68;223;233;114;133;30;40;179;195;88;110;245;2;153;175;52;218;65;119;236;27;128;182;45;241;106;92;199;48;171;157;6;232;115;69;222;41;178;132;31;167;60;10;145;102;253;203;80;190;37;19;136;127;228;210;73;149;14;56;163;84;207;249;98;140;23;33;186;77;214;224;123]];

    otherwise

        error('error crc_type %d\n',crc_type);

end

 

 

end

 

注:上述代码中使用的全局变量如下,

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% ENUM->CrcType_Enum

% this enum is used to record the CRC type

% CRC 24A, used for DLSCH/ULSCH

global crctype_CRC_24A_ENUM;

crctype_CRC_24A_ENUM = 0;

% CRC 24B, used for DLSCH/ULSCH (segment)

global crctype_CRC_24B_ENUM;

crctype_CRC_24B_ENUM = 1;

% CRC 16, used for DCI

global crctype_CRC_16_ENUM;

crctype_CRC_16_ENUM = 2;

% CRC 8

global crctype_CRC_8_ENUM;

crctype_CRC_8_ENUM = 3;

% the invalid value for this enum

global crctype_BUFF_ENUM;

crctype_BUFF_ENUM = 4;

 

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% STRUCT->CrcPara_Stru

%{ the description of usage for this struct

%}

global CrcPara_Stru;

%{

 Defined in TS36.213, it means the length of source bit stream

%}

% valid scope [16,391656]

CrcPara_Stru.TBS = 0;

%{

 the element with struct type

%}

CrcPara_Stru.CrcType_Enum = 0;