📄 convolutionalcode_phase1.m
字号:
%%%%%%%%% ECE 8001 Fall Error Correction Coding Project (Phase 1) %%%%
%%%%%%%%% Author: Xin Li # 14005390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% xlm72@mizzou.edu %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% University of Missouri %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clear all;
clc;
sample_number = 25;
L = 512;
Smaple = randint(L,sample_number,[0,1]);
CW = 2*ones(L,2,sample_number);
EvaluateResult = inf*ones(10,10,25); % dim1 error ratio,
%dim2 error patterns, dim3 smaple,
% trellis = poly2trellis(5,[23,35]);
%%%%%%Step1:Encoder design
g1 = [1, 0, 0, 1, 1, 0];
g2 = [1, 1, 1, 0, 1, 0];
%%%%%%Step2: Building the trellis
for i = 0:31
Table.CurState(i+1,:) = bitget(i, 5:-1:1);
end
Table.CurState(33:64,:) = Table.CurState(1:32,:);
Table.Input(1:32,:) = 1;
Table.Input(33:64,:) = 0;
for i = 1:64
Tot_b(i,:) = [Table.Input(i,:) Table.CurState(i,:)];
Table.NextState(i,:) = [Table.Input(i,:) Table.CurState(i,1:4)];
end
for i = 1:64
c1_temp(i,:) = bitand(Tot_b(i,:),g1);
c2_temp(i,:) = bitand(Tot_b(i,:),g2);
end
c1 = mod(sum(c1_temp,2),2);
c2 = mod(sum(c2_temp,2),2);
Table.Output = [c1 c2];
Table.CurSNum = bin2dec(num2str(Table.CurState));
Table.NextSNum = bin2dec(num2str(Table.NextState));
for s_i = 1:sample_number
%%Encoding%%%
% a = 0;
% b = 1;
% m = floor((b-a+1)*rand(L,1)) +a;
m = Smaple(:,s_i);
Cur_State = 0;
for i = 1:L
index = find(Table.Input==m(i) & Table.CurSNum==Cur_State);
Cur_State = Table.NextSNum(index);
OutputCW(i,:) = Table.Output(index,:);
end
CW(:,:,s_i)= OutputCW;
%Step3: Impletmentthe Viterbi decoding algorithm to OutputCW
for p_error = 1:10
for error_pattern = 1:10
Receive = CW(:,:,s_i); %1.Input receiving sequence
% Receive = randint(L,2,[0 1]);
%%%%%%%%%%%%%%%%%Puncturing Part%%%%%%%%%%%%%%%
% % punc_position = [4:4:L]; %3/4
% punc_position = [5:5:L]; %4/5
% Receive(punc_position,:)=NaN;
% Receive_Temp = Receive;
% Receive_Temp(isnan(Receive_Temp)) = [];
% [R_s(1) R_s(2)] = size(Receive_Temp);
%
% error_n = round(R_s(1)*R_s(2)*0.01*p_error);
% error_position = randperm(R_s(1)*R_s(2));
% error_position = error_position(1:error_n);
% new_error_position = error_position+2*fix(error_position/9);
% E_pattern = zeros(2,L);
% E_pattern(new_error_position) = 1;
% Receive = abs(Receive-E_pattern');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%Unpuncturing Part%%%%%%%%%%%%%%%
error_n = round(L*2*0.01*p_error);
error_position = randperm(L*2);
error_position = error_position(1:error_n);
E_pattern = zeros(L,2);
E_pattern(error_position) = 1;
Receive = abs(CW(:,:,s_i)-E_pattern);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = -1*ones(L,1); %2.Output sequence
M = inf*ones(32,L);
M(1,1) =0; %3.initial path cost
P = -1*ones(32,L);
P(:,1)=[1:32]; % 4.initial paths
t = 1; % 5.set t = 0
while t<=L %6. Begin
for i = 1:32 %7.for each state q at time t+1
p_index = find(Table.NextSNum == i-1);
for j = 1:2
if isnan(Receive(t,1))||isnan(Receive(t,2))
Mut(j) = 0;
Mt(j) = M(Table.CurSNum(p_index(j))+1,t)+ Mut(j);
else
Mut(j) = sum(xor(Table.Output(p_index(j),:),...
Receive(t,:)),2);
%8.find the path metric for each path to state q
Mt(j) = M(Table.CurSNum(p_index(j))+1,t)+ Mut(j);
%9.for each pi connected to state q
%corresponding to input
%x(pi,q),cmput mt(q)=M(pi)+Mut(rt,x(pi,q)).
end
end
[C,I] = min(Mt);
M(i,t+1) = C;
%10.Select the smallest metrix M(q)=minimi and the
%corresponding predecessor state p.
P(i,t+1) = Table.CurSNum(p_index(I));
% X(i,t) = Table.Input(p_index(I));
%11.Extend the path to state q: Path(q)=[Path(p) p]
end %12.end for
t = t+1; %13.t= t+1
end %14.if t<L-1, goto 6
%15.Termination:
[C1 I1] = min(M(:,L+1));
for i = L+1:-1:2
X(i-1) = Table.Input(find(Table.NextSNum==(I1-1) &...
Table.CurSNum==P(I1,i)));
I1 = P(I1,i)+1;
end %16.Return the Sequence
ErrorLeft = sum(xor(m,X));
EvaluateResult(p_error,error_pattern,s_i) = ErrorLeft;
end
end
end
AvgDh = sum(sum(EvaluateResult,3),2)/250;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -