关于RM码的详细说明-> 德莉莎世界第一可爱
RM码生成矩阵构造函数RMG代码见文末
close all
clear all
clc
r=1;
m=5;
k=0;
for i=0:r
k=k+nchoosek(m,i);
end
d=pow2(m-r);
t=fix((d-1)/2);
len=pow2(m);
%encode
G=RMG(r,m);
u=randBinary(k);
s='u:';
s=strcat(s,int2str(u));
disp(s)
c=mod(u*G,2);
s='code:';
s=strcat(s,int2str(c));
disp(s)
%error
err_num=randi(t);
s='error num:';
s=strcat(s,num2str(err_num));
disp(s)
E=zeros(1,len);
while err_num
pos=randi(len);
E(pos)=E(pos)+1;
err_num=err_num-1;
end
E=mod(E,2);
s='error sample:';
s=strcat(s,int2str(E));
disp(s)
%decode
r=mod(c+E,2);
s='receive code:';
s=strcat(s,int2str(r));
disp(s)
H=[1 1;1 -1];
w=zeros(1,len);
for i=1:len
w(i)=r(i)*2-1;
end
for i=1:m
Him=kron(kron(eye(pow2(m-i)),H),eye(pow2(i-1)));
w=w*Him;
end
max_pos=1;
for i=2:len
if abs(w(max_pos))<abs(w(i))
max_pos=i;
end
end
message=zeros(1,k);
if w(max_pos)>0
message(1)=1;
else
message(1)=0;
end
max_pos=max_pos-1;
for i=2:k
message(i)=mod(max_pos,2);
max_pos=fix(max_pos/2);
end
s='message:';
s=strcat(s,int2str(message));
disp(s)
sc=message*G;
sr=mod(sc+r,2);
s='distance:';
s=strcat(s,num2str(sum(sr)));
disp(s)
RMG函数代码
function [ G ] = RMG( r,m )
%
% RM码生成矩阵
% 迭代构造
if r>m
disp('check r and m');
return;
end
len1=pow2(m);
if r==0
G=zeros(1,len1);
for i=1:len1
G(i)=1;
end
return ;
end
if r==m
z=zeros(1,len1);
z(len1)=1;
G=[RMG(r-1,m);z];
return ;
end
len2=pow2(m-1);
tem=RMG(r,m-1);
sum=0;
for i=0:r-1
sum=sum+nchoosek(m-1,i);
end
z=zeros(sum,len2);
G=[tem tem;z RMG(r-1,m-1)];
end
white_156
发布了161 篇原创文章 · 获赞 170 · 访问量 1万+
私信
关注