?? cyclic_redundancy_decode.m
字號:
<matlab> 實現循環冗余編碼<CRC>解碼檢錯
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% 該函數通過輸入信源編碼序列和相應選擇L階CRC生成多項式以及輸入 %
% 編碼的長度,驗證原始信號在傳輸過程中是否發生誤碼,如果信道 %
% 噪聲和干擾造成誤碼則輸出err錯誤標志,否則即解碼得到原始信號 %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [original_sequence,err] = cyclic_redundancy_decode(crc_coded_sequence,seq_length,crc_standard)
if nargin < 2 % 如果輸入參數不足,顯示出錯信息
error('參數不足! 必須輸入CRC編碼序列的長度 seq_length 和 CRC編碼的多項式標準(1 - CRC_CCITT,2 - CRC_CIT,3 - CRC_32)');
end
if crc_standard ~= 1 & crc_standard ~= 2 & crc_standard ~= 3 % 限制輸入的參數必須為1,2,3之間的一個
error('crc_standard 參數輸入錯誤!crc_standard = 1,2,3');
end
if seq_length ~= length(crc_coded_sequence)
error('輸入序列長度參數有誤!');
end
sequence_length = length(crc_coded_sequence); % 得到冗余編碼的長度
original_sequence = crc_coded_sequence; % 初始化輸出序列
switch crc_standard
case 1 %CRC_CCITT
crc_ccitt = [1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1]; % 常用的CRC生成多項式
remainder_bits = crc_coded_sequence; % 初始化余數數組
cycle_length = sequence_length-length(crc_ccitt)+1; % 計算長除法的循環周期
for k = 1:cycle_length % 開始循環計算長除得到最終余數
add_zeros = zeros(1,cycle_length-k); % 加入冗余位參與模2運算
register_bits = [crc_ccitt add_zeros]; % 構造除數數組
if remainder_bits(1) == 0 % 被除數第一位為0則將除數所有位置0
register_bits = zeros(1,length(register_bits));
end
remainder_bits = bitxor(register_bits,remainder_bits); % 將除數與被除數進行異或操作
register_bits = crc_ccitt; % 將寄存器恢復為除數數組
remainder_bits(1) = []; % 去除模2后得到的被除數的第1位
end
if sum(remainder_bits) == 0 % 傳輸碼元中沒有發生奇數個錯誤
original_sequence = crc_coded_sequence(1:cycle_length);
else
err = 1; % 碼元傳輸發生錯誤
end
case 2 %CRC_CIT
crc_cit = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1];
remainder_bits = crc_coded_sequence; % 初始化余數數組
cycle_length = sequence_length-length(crc_cit)+1; % 計算長除法的循環周期
for k = 1:cycle_length % 開始循環計算長除得到最終余數
add_zeros = zeros(1,cycle_length-k); % 加入冗余位參與模2運算
register_bits = [crc_cit add_zeros]; % 構造除數數組
if remainder_bits(1) == 0 % 被除數第一位為0則將除數所有位置0
register_bits = zeros(1,length(register_bits));
end
remainder_bits = bitxor(register_bits,remainder_bits); % 將除數與被除數進行異或操作
register_bits = crc_cit; % 將寄存器恢復為除數數組
remainder_bits(1) = []; % 去除模2后得到的被除數的第1位
end
if sum(remainder_bits) == 0 % 傳輸碼元中沒有發生奇數個錯誤
original_sequence = crc_coded_sequence(1:cycle_length);
else
err = 1; % 碼元傳輸發生錯誤
end
case 3 %CRC_32
crc_32 = [1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0 1 1 0 1 1 1];
remainder_bits = crc_coded_sequence; % 初始化余數數組
cycle_length = sequence_length-length(crc_32)+1; % 計算長除法的循環周期
for k = 1:cycle_length % 開始循環計算長除得到最終余數
add_zeros = zeros(1,cycle_length-k); % 加入冗余位參與模2運算
register_bits = [crc_32 add_zeros]; % 構造除數數組
if remainder_bits(1) == 0 % 被除數第一位為0則將除數所有位置0
register_bits = zeros(1,length(register_bits));
end
remainder_bits = bitxor(register_bits,remainder_bits); % 將除數與被除數進行異或操作
register_bits = crc_32; % 將寄存器恢復為除數數組
remainder_bits(1) = []; % 去除模2后得到的被除數的第1位
end
if sum(remainder_bits) == 0 % 傳輸碼元中沒有發生奇數個錯誤
original_sequence = crc_coded_sequence(1:cycle_length);
else
err = 1; % 碼元傳輸發生錯誤
end
otherwise
error('輸入參數錯誤!');
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -