主程序
% implement RS code of UWB
clc;clear;close all;
%% Any bit
I=67; % real data for translate,there is 67 bits
s=round(rand(1,I));
N=330;
%% encoded
%% step a) add dummy bits
sa=[s zeros(1,N-I)];
%% step b) Bit to symbol conversion
%% need cal?
% a=[0 0 0 0 1 0];
% a0=[0 0 0 0 0 1];
% a1=a;
% a2=mul(a1,a1);
% a3=mul(a2,a1);
% a4=mul(a3,a1);
% a5=mul(a4,a1);
% dk=zeros(1,6);
% sb=[];
% for k=0:54
% dk=mod(sa(k*6+1)*ones(1,6)+sa(k*6+2)*a1+sa(k*6+3)*a2+sa(k*6+4)*a3+...
% sa(k*6+5)*a4+sa(k*6+6)*a5,2);
% sb=[sb dk];
% end
%% dont cal
sb=sa;
%% step c) encoding
gx=[1 55 61 37 48 47 20 6 22];
gxb=[];% change to bits
for i=1:9
gxb=[gxb dec_to_6_bits(gx(i))];
end
sc=[sb zeros(1,48)];
for i=1:55 % do 55 times module g(x)
temp_gxb=zeros(1,54); % 6*9=54
for j=1:9
temp_gxb(j*6-5:j*6)=mul(gxb(j*6-5:j*6),sb(i*6-5:i*6));
end
sc(i*6-5:i*6+48)=mod(sc(i*6-5:i*6+48)+temp_gxb,2);
end
px=sc(331:end); % 330-48+1=283
sc=[sb px];
%% step d) Symbol to bit conversion
sd=sc;
%% step e) removal of dummy bits
se=sd(331-I:end);
有限域乘法函数 mul.m
function s= mul(a,b)
%MUL Summary of this function goes here
% Detailed explanation goes here
% 6 bit
% cal a*b mod poly
% poly 1+x+x^6
% 1/a=a^62
% [1 0 0 0 0 1 1]
% a=[1 0 1 0 0 0]
% b=[1 0 1 1 1 1]
ts=zeros(1,11);
py=[1 0 0 0 0 1 1];
for i=1:6
if(b(i)==1)
% ts=mod(ts+[zeros(1,6-i) a zeros(1,i-1)],2);
ts=mod(ts+[zeros(1,i-1) a zeros(1,6-i)],2);
% ^
end
end
% ts
for i=1:5
if(ts(i)==1)
ts=mod(ts+[zeros(1,i-1) py zeros(1,5-i)],2);
end
end
s=ts(6:end);
end
十进制转换为6 bits 函数 dec_to_6_bits.m
function s=dec_to_6_bits(n)
z=[];
while(n>=2)
if(mod(n,2)==0)
z=[0 z];
n=n/2;
else
z=[1 z];
n=(n-1)/2;
end
end
if(n==1)
z=[1 z];
else
z=[0 z];
end
L=length(z);
if(L<6)
z=[zeros(1,6-L) z];
end
s=z;
end
十进制转换为4比特函数 dec_to_4_bits.m
function s=dec_to_4_bits(n)
z=[];
while(n>=2)
if(mod(n,2)==0)
z=[0 z];
n=n/2;
else
z=[1 z];
n=(n-1)/2;
end
end
if(n==1)
z=[1 z];
else
z=[0 z];
end
L=length(z);
if(L<4)
z=[zeros(1,4-L) z];
end
s=z;
end
二进制转换为十进制函数 bit_to_dec.m
function s=bits_to_dec(b)
n=length(b);
s=0;
for i=1:n
s=2*s+b(i);
end
end