# Home Networking Basis

### Walter Chen (view profile)

11 Aug 2003 (Updated )

M-files for chapters 2-10 and Simulink models for Ethernet and Wireless Ethernet.

rsdecoder.m
```%Received word
rc=[mx cw];
%Calculate Syndrom
synd=zeros(8,16);
for i=1:16
for j=1:255
if sum(rc(:,j))~=0
pp=b2pw8(rc(:,j),8,ppr)+i*(255-j);
bpp=p2bin8(pp,8,bpr);
synd(:,i)=xor(synd(:,i),bpp);
end
end
end
clear rts
clear txp
%Check if there is error to run the rest of the decoding algorithm
if sum(sum(synd))~=0
%Find the error locator polynomial
tx=zeros(8,2);
tx(8,2)=1;
L=0;
cnp=zeros(8,1);
cnp(8,1)=1;
delt=synd(:,1);
if sum(delt)~=0
cnp=[[0 0 0 0 0 0 0 1]' delt];
L=1;
tx=p2bin8(255-b2pw8(delt,8,ppr),8,bpr);
end
tx=[zeros(8,1) tx];
for i=2:m
delt=synd(:,i);
for j=1:L
cnpp=b2pw8(cnp(:,j+1),8,ppr);
syndp=b2pw8(synd(:,i-j),8,ppr);
cnpsynd=p2bin8(cnpp+syndp,8,bpr);
delt=xor(delt,cnpsynd);
end
if sum(delt)~=0
deltp=b2pw8(delt,8,ppr);
[txa,txb]=size(tx);
deltmtx=zeros(txa,txb);
for k=1:txb
txp(k)=b2pw8(tx(:,k),8,ppr);
if txp(k)~=-1
deltmtx(:,k)=p2bin8(deltp+txp(k),8,bpr);
end
end
cnppr=cnp;
[cnpa,cnpb]=size(cnp);
if cnpb<txb
cnpe=[cnp zeros(8,txb-cnpb)];
else
cnpe=cnp;
end
cnp=xor(cnpe,deltmtx);
if 2*L<i
L=i-L;
clear tx
for k=1:cnpb
cnpprp=b2pw8(cnppr(:,k),8,ppr);
cnpddelt=cnpprp-deltp;
if cnpddelt<0
cnpddelt=cnpddelt+255;
end
tx(:,k)=p2bin8(cnpddelt,8,bpr);
end
end
end
tx=[zeros(8,1) tx];
end
%Find roots and error locators
[cnpa,cnpb]=size(cnp);
for i=1:cnpb
cnpw(i)=b2pw8(cnp(:,i),8,ppr);
end
rtcnt=0;
for i=0:254
cns=[0 0 0 0 0 0 0 1]';
for j=2:cnpb
cns=xor(cns,p2bin8(cnpw(j)+i*(j-1),8,bpr));
end
if sum(cns)==0
rtcnt=rtcnt+1;
rts(rtcnt)=i;
end
end
%Form the z(x) polynomial
zx=[0 0 0 0 0 0 0 1]';
for i=1:length(rts)
zcoe=xor(synd(:,i),cnp(:,i+1));
for j=2:i
pcnsy=b2pw8(synd(:,j-1),8,ppr)+b2pw8(cnp(:,i-j+2),8,ppr);
zcoe=xor(zcoe,p2bin8(pcnsy,8,bpr));
end
zx=[zx zcoe];
end
clear zxp
[zxa,zxb]=size(zx);
for i=1:zxb
zxp(i)=b2pw8(zx(:,i),8,ppr);
end
%Find Error values
clear er
for i=1:length(rts)
zxb=[0 0 0 0 0 0 0 1]';
for j=1:length(zxp)-1
zxc=zxp(j+1)+rts(i)*j;
zxcb=p2bin8(zxc,8,bpr);
zxb=xor(zxb,zxcb);
end
zxbp=b2pw8(zxb,8,ppr);
pbb=0;
for j=1:length(rts)
if j~=i
rtsrtsm=rts(i)-rts(j);
if rtsrtsm<0
rtsrtsm=rtsrtsm+255;
end
blr=xor([0 0 0 0 0 0 0 1]',p2bin8(rtsrtsm,8,bpr));
pbb=pbb+b2pw8(blr,8,ppr);
end
end
er(i)=zxbp-pbb;
for j=1:3
if er(i)<0
er(i)=er(i)+255;
end
end
end
rts
er
end```