📄 code_uncode.m
字号:
clear
close all
clc
pe_1(1,41)=0;
pe_2(1,41)=0;
eb=1;%one bit energy
a=[1 1 0 0 1 0 0 1 0 1];%编码前的数据
%----------------
c=2*a-1; %未编码的bpsk映射
%----------------
%NRZI编码
re(1)=a(1);
for i=2:10
re(i)=xor(re(i-1),a(i));
end
%--------------------
b=2*re-1;%bpsk映射,将0,1转换成1和-1
%--------------------
for s_n=-20:1:20
c_n=awgn(c,s_n)
re_n=awgn(b,s_n);%加噪
d0=zeros(1,10);%for state0's route
d1=zeros(1,10);%for state1's route
rece=zeros(1,10);%decoded serial
d_0(1)=(re_n(1)+sqrt(eb))^2;%d_0:sum of accumulation for state 0
d_1(1)=(re_n(1)-sqrt(eb))^2;%d_1:sum of accumulation for state 0
d0(1)=-1;
d1(1)=1;
for i=2:10
d00=(re_n(i)+sqrt(eb))^2;%每个时刻到0 的度量值
d_0(i)=min(d_0(i-1)+d00,d_1(i-1)+d00);
if d_0(i)==d_0(i-1)+d00
d0(1:i)=[d0(1:i-1),0];
else
d0(1:i)=[d1(1:i-1),1];
end
d11=(re_n(i)-sqrt(eb))^2;%每个时刻到1 的度量值
d_1(i)=min(d_0(i-1)+d11,d_1(i-1)+d11);
if d_1(i)==d_0(i-1)+d11
d1(1:i)=[d0(1:i-1),1];
else
d1(1:i)=[d1(1:i-1),0];
end
%---
if i>=6
if d_0(i)<=d_1(i)
rece(i-5)=d0(i-5);
else
rece(i-5)=d1(i-5);
end
end
end
%-----------------------------
%decoding of the last five bits
%==========
if d_0(10)<=d_1(10)
rece(6:10)=d0(6:10);
else
rece(6:10)=d1(6:10);
end
%-------------
flag=0;
for i=1:10
if rece(i)~=a(i)
flag=flag+1;
end
end
pe1(s_n+21)=flag/10;
%--------------------
for i=1:10 %judge the output:1 or -1
if c_n(i)>0;
c_esti(i)=1;
else
c_esti(i)=0;
end
end
%----------------------
flag=0; %the number of error bit
for i=1:10
if c_esti(i)~=c(i) %not equal to the source signal,it's an error bit
flag=flag+1;
end
end
pe2(s_n+21)=flag/10; %aquire the error rate
flag=0;
end
%-----------------------
%=======================
s_n=-20:1:20;
%subplot(2,1,1);
%plot(s_n,pe1(s_n+21));
%subplot(2,1,2);
%plot(s_n,pe2(s_n+21));
plot(s_n,pe1(s_n+21),'r-',s_n,pe2(s_n+21),'b--'),grid on %r-为红色实线,为编码后的,b--为蓝色虚线,为未编码的
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -