Coding Theory Programs Built in Octave. Part III.

#
1;

#================================
function Badd = add(x1,x2)
# description: this function takes two binary vectors x1,x2, and returns x1+x2
# mode 2.
# usgae: Badd = add(x1,x2)   
      n=rows(x1);
      x3=zeros(n,1);
      for i=1:n
            m=x1(i)+x2(i);
            if (m==1)  (x3(i)=1); endif;
            if (m==2)  (x3(i)=0); endif;
      endfor;
      Badd=x3;
endfunction
#=====================================
function TwoFact=TF(n)
Prod=1;
for i=1:n
      Prod=Prod*2;
endfor;
TF=Prod;
endfunction
#=====================================

function BtoD=BtD(x)
[n,m]=size(x);
N=max(n,m);
s=x(1);
for i=2:N
      s=s+x(i)*TF(i-1);
endfor;
BtD=s;
endfunction

#=====================================

function Code=CodeGen(M)
# description: This function takes a generator of a certain code and returns
# the code itself. Each row of the matrix M is one generator.
# usage: Code=CodeGen(M)
[m,n]=size(M);
Prod=1;
for k=1:m
      Prod=Prod*2;
endfor;
C=zeros(Prod,n);
for i=1:m
      C(i,:)=M(i,:);
endfor;
posit=m;
for i=1:m
      for j=i+1:m
            #s=zeros(n,1)
            x=C(i,:)';
            s=x;
            for k=j:m
                  posit=posit+1;         
                  y=C(k,:)';
                  #qq=add(x,y)
                  s=add(s,y);
                  C(posit,:)=s';
            endfor;
      endfor;
endfor;
Code=C;
endfunction
#=====================================
function weight=wt(v)

# Description: This function takes a certain vector v and returns
# its weight; i.e. the number of ones.
# usage: weight=wt(v)

[n,m]=size(v);
if (m>n) (v=v');  endif;
n=rows(v);
w=0;
for i=1:n
      if (v(i)==1) (w=w+1); endif;
endfor;
weight=w;
endfunction

#=====================================
function MatVecMul=Bmult(M,v)

# Description: This function takes a certain binary matrix M and a binary
# vector v, and returns M*v mod 2.
# Usage: MatVecMul=Bmult(M,v)

#[n,m]=size(v);
#if (m>n) (v=v')  endif;
[n,m]=size(M);
w=zeros(n,1);
for i=1:n
      qq=zeros(m,1);
      for j=1:m
            qq(j)=M(i,j)*v(j);
      endfor;
      #qq
      count=0;
      for k=1:m
            #sum=sum+qq(k)
            #if (sum==2) (sum==0);  endif;
            if (qq(k)==1) (count=count+1); endif;
            #count
      endfor;
      zz=count/2;
      yy=round(zz);
      if (yy==zz) (w(i)=0); endif;
      if (yy<>zz) (w(i)=1); endif;
      #w(i)=sum
endfor;
w=w; #new
MatVecMul=w;
           
endfunction


#=====================================

function [MVadd,flag1] = ad(M,v,check)
# description: This function takes a binary vectors v,a binary matrix M, and
# returns M+v mod 2.
# usgae: [MVadd,flag1] = ad(M,v,check) 
      [nn,n]=size(M);flag1=0;M1=M;
      x=zeros(n,1);y=zeros(n,1);
      for i=1:nn
            for j=1:n
                  x(j)=M1(i,j);
            endfor;
            #x=M1(i,:)';
            y=add(x,v);
            for k=1:8
                  if (check(k,:)==y')
                        (flag1=1);
                  endif;
            endfor;
            for j=1:n
                  M1(i,j)=y(j);
            endfor;

      endfor;
      MVadd=M1;
endfunction
#================
function [B,G]=Golay()

# Description: This function returns the generator matrix of the
# Extended Golay Code.

B=zeros(12,12);
for i=1:11
      B(12,i)=1;
endfor;
for i=1:11
      B(i,12)=1;
endfor;

B(1,1)=1;B(1,2)=1;B(1,4)=1;B(1,5)=1;B(1,6)=1;B(1,10)=1;
for i=2:11
      temp=B(i-1,1);
      for j=1:10
            B(i,j)=B(i-1,j+1);
      endfor;
      B(i,11)=temp;
endfor;
G=[eye(12),B];
endfunction
#==================================
function [decAlg,flag]=decode(r)

# Description: This function decodes a certain received vector by using
# the Golay Extended Code algoritm.
# Usage: [decAlg,flag]=decode(r)
# NOTE: flag=1 means that decoding is performed, while flag=0 means not.
flag=0;
[B,G]=Golay();
H=[B,eye(12)];
s=Bmult(H,r');
FirstSyndrome=s
ss=Bmult(G,r');
SecondSyndrome=ss
if (wt(s)<=3)
       e=[zeros(12,1)',s']
       decAlg=(add(r',e'))';
       printf("First syndrome has weight less than four\n");
       #decAlg=add(r,e);
       flag=1;
       return;
endif;
           
if (wt(ss)<=3)
      e=[ss',zeros(12,1)']
      decAlg=(add(r',e'))';
      printf("Second syndrome has weight less than four\n");
      #decAlg=add(r,e)
      flag=1;
      return;
endif;
#BB=B';
I=eye(12);
for i=1:12
      if (wt(add(s,B(:,i)))<=2)
            e=[I(i,:),(add(s,B(:,i)))']
            decAlg=(add(r',e'))';
            printf("The following column of B was used with the first syndrome\n ");           
            column=i
            printf("\n");
            #decAlg=add(r,e)
            flag=1;
            return;
      endif;
endfor;
for i=1:12
      if (wt(add(ss,B(:,i)))<=2)
            e=[(add(ss,B(:,i)))',I(i,:)]
            decAlg=(add(r',e'))';
            printf("The following column of B was used with the second syndrome\n");
           
            column=i
            printf("\n");
            #decAlg=add(r,e)
            flag=1;
            return;
      endif;
endfor;
printf("Decoding fails");

endfunction
 

Back to Iyad Abu-Jeib's Homepage