
m
=
3
n
=
2^m-1
%定义码长
k
=
n-m
%信息位长
msg
=
randint(k*4,1,2)
%随机提取信号,引起一致地分布的任意整数矩阵
subplot(2,2,1)
stem(msg)
title('编码器输入信号')
p=cyclpoly(n,k)
%循环码生成多项式,n=7,k=4
code
=
encode(msg,n,k,'cyclic',p)
%编码函数,对信号进行差错编码
subplot(2,2,2)
stem(code)
title('编码器输出信号')
recode=decode(code,n,k,'cyclic',p)
%对信号进行译码,对接收到的码字进行译码,恢复
出原始的信息,译码参数和方式必须和编码时采用的严格相同
subplot(2,2,3)
stem(recode)
title('译码器输出信号')
t=-1:0.01:1
x=recode
%将recode赋值给x,并进行长度与fft设定
N=length(x)
fx=fft(x)
df=100/N
n=0:N/2
f=n*df
subplot(2,2,4)
plot(f,abs(fx(n+1))*2/念胡N)
grid
title('频谱图')
2、误码率与信噪比之间的关系程序(以(首高斗3,2)循环码进行测试)
m
=
2
n
=
2^m-1
%定义码长
k
=
n-m
%信息位长
Fs=40
%系统采样频率
Fd=1
%码速率
N=Fs/Fd
M=2
%进制数
for
SNRpBit=1:100%信噪比
SNR=SNRpBit/log2(M)
%制造100个信息组,每组k位
msg
=
randint(100,k,[0,1])
code
=
encode(msg,n,k,'cyclic/binary')
%加入噪声
%在已调信号中加入高斯白噪声
noisycode=awgn(code,SNR-10*log10(0.5)-10*log10(N),'measured',[],'dB')
%将浮点数转化为二进制,波形整形过程
for
i=1:100
for
a=1:k+1
if
noisycode(i,a)<0.5
noisycode(i,a)
=
0
else
noisycode(i,a)
=
1
end
end
end
%译码
newmsg
=
decode(noisycode,n,k,'cyclic')
%计算误码率
[number,ratio]=biterr(newmsg,msg)
result(SNRpBit)=ratio
disp(['The
bit
error
rate
is',num2str(ratio)])
end
%不同信噪比下循环码经过加性高斯白噪声信道的误码率
figure(1)
stem(result)
title('循环码在不同信噪比下的误码率')
legend('误码率','*')
xlabel('信噪比')
ylabel('在加性高斯白噪声下的误码率')
function [Three]=Eight2Three(Eight)%从左到右依次为从高位低到位
%输入为二进制的数组
%比方说:Eight=[1 0 1 0 0 0 0 0]
%那么唯帆根据优先编码盯山伏的原则,这个编码为最高位,应该输出7的二进制:1 1 1
%编码原则为7 6 5 4 3 2 1 0
if nargin==0
Eight=rand(1,8)>0.5%没有输入则随机生成一个输入
Eight
end
if length(Eight)>8
Eight=Eight(1:8)
end
Eight=(Eight==1)%处理不合理的输入
IndexNum = 8-find(Eight, 1, 'first')%找到最高位的二进制编码
Str=dec2bin(IndexNum,3)
Three(1)= str2num(Str(1))
Three(2)=str2num(Str(2))
Three(3)=str2num(Str(3))
运行结果
>>[Three]=Eight2Three()
Eight =
0 0 1 1 0 0 0 1
Three =
1 0 1
%随机生成了高位凯携在第5位的,那么输出 1 0 1
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)