?? viterbi_decoder.m
字號:
function sysout=Viterbi_Decoder(deint_matrix)
% This is the Viterbi Decoder
% The decoder will decode 21 times top roduce a total of 378 bits and the 6 remaining bits are padded with 0s
sysout1=zeros([1,192]);
% we create the node structure
node=struct('parent',{[]},'input',{[]},'code',{[]},'tout',{[]},'child1no',{[]},'child2no',{[]},'shift0',{[]},'shiftstate',{[]},'shift1',{[]},'o1',{[]},'o2',{[]},'stage',{[]});
metric=struct('code',{[]},'input',{[]},'tout',{[]},'parent',{[]},'error',{0});
% initial shift register state to start the Viterbi Alogrithm
sshift=[0 0 0 0 0 0 0 0 0];
% we perform complete decoding for 21 times
for i=1:18:378
% run the viterbi alogrithm
% we make up the 9 shift register states
shifts=zeros([1,9]);
% we create the code tree with 1023 nodes
tree=[];
for j=1:1023
tree=[tree,node];
end
% total number of nodes in the code tree
total=0;
% 'k'reflects the current stage number
for k=0:9
% number of nodesthat we have create data specific stage
completed=0;
% number of nodes that we have to create at a specific stage
numnodes=2^k;
% each time it computes one node
while completed~= numnodes
%we define the start node of the tree if index i=0
if total==0
%we define it sparent and input,input means a bit input which causes
%the parent node to come into this node
tree(total+1).parent=[];
tree(total+1).input=[];
tree(total+1).code=[];
tree(total+1).tout=[];
tree(total+1).shiftstate=sshift;
end
% for all other nodes,we define the irparent by calculating the parent's index
% =floor(total/2)
if total>0
%total+1 means the index of the current node we a revisiting
pindex=floor((total+1)/2);
tree(total+1).parent=tree(pindex).code;
if mod((total+1),2)==0
tree(total+1).tout=[tree(pindex).tout,tree(pindex).o1];
tree(total+1).shiftstate=tree(pindex).shift0;
end
if mod((total+1),2)==1
tree(total+1).tout=[tree(pindex).tout,tree(pindex).o2];
tree(total+1).shiftstate=tree(pindex).shift1;
end
%we want to calculate the code of the current node visiting
tree(pindex*2).code=[tree(total+1).parent,0];
tree((pindex*2)+1).code=[tree(total+1).parent,1];
tree(pindex*2).input=0;
tree((pindex*2)+1).input=1;
end
% there will be two different inputs,0 and 1 and therefore, we can calculate
% a parent node'schilds(2)
for l=0:1
% we get original shift state from parent
shifts=tree(total+1).shiftstate;
% shift the input into the shift register
shifts(2:9)=shifts(1:8);
shifts(1)=l;
out1=mod((shifts(1)+shifts(2)+shifts(3)+shifts(4)+shifts(6)+shifts(8)+shifts(9)),2);
out2=mod((shifts(1)+shifts(3)+shifts(4)+shifts(5)+shifts(9)),2);
if l==0
tree(total+1).shift0=shifts;
% we calculate the output due to a '0' input
tree(total+1).o1=[out1, out2];
% we calculate the child(index)due to a '0' input
tree(total+1).child1no=(total+1)*2;
end
if l==1
tree(total+1).shift1=shifts;
% we calculate the output due to a '1' input
tree(total+1).o2=[out1, out2];
% we calculate the child due to a '1'input
tree(total+1).child2no=((total+1)*2)+1;
end
end
% we the stage of the current node
tree(total+1).stage=k+1;
% we only increase the number of computed nodes 1,not 2,after all the
% childs of aparent are computed
total=total+1;
completed=completed+1;
%end of while
end
% end of for
end
% compute the metrics with512 elements
metrics=[];
for m =1:512
metrics=[metrics,metric];
end
% we get all the possible codes off the tree
for n=512:1023
metrics(n-511).code=tree(n).code;
metrics(n-511).tout=tree(n).tout;
end
buf=deint_matrix(i:i+17);
% we let the first to have the lowest error
metrics(1).input=buf;
bbb=find(metrics(1).tout- metrics(1).input);
metrics(1).error=length(bbb);
Lindex=1;
Lerror=metrics(1).error;
% check the others to see if there are any with lower error
for p=2:512
metrics(p).input=buf;
bb=find(metrics(p).tout- metrics(p).input);
metrics(p).error=length(bb);
if metrics(p).error<Lerror
Lindex=p;
Lerror=metrics(p).error;
end
end
% join the decoded code to the output bit stream
sysout1(round(i/2):round((i/2)+8))=metrics(Lindex).code;
% update the initial shift register states to start the next virterbi alogrithm
sshift=tree(Lindex+511).shiftstate;
end
sysout=sysout1;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -