📄 cm_sm32.m
字号:
% 本程序在给定信噪比的前提下,求出比特误码率和符号误码率;
% 本程序的基本过程如下:(1)发送前的信号映射;(2)序列产生;
% (3)在信道中加入衰落和噪声;(4)在接受端做相关检测;
% (5)择大判决;(6)统计并计算误码率;
function [pb,ps]=cm_sm32(snr_in_dB)
% [pb,ps]=cm_sm32(snr_in_dB)
% CM_SM3 finds the probability of bit error and
% symbol error for
% the given value of snr_in_dB, signal to noise ratio in dB.
N=100;
% 产生瑞利分布的因子;
n_factor=0.8;
% energy per symbol
% 符号能量;
E=1;
numofsymbolerror=0;
numofbiterror=0;
counter=0;
% signal to noise ratio
% 信噪比;
snr=10^(snr_in_dB/10);
% 噪声方差,由调用函数时输入的信噪比决定;
sgma=sqrt(E/snr)/2; % noise variance
% signal mapping
% 信号映射,实际比特流是如下所示的0101的代码,但再发送的时候
% 要做映射,使他们的平均值为零;
s00=[1 0]; s01=[0 1]; s11=[-1 0]; s10=[0 -1];
% generation of the data source
while(numofbiterror<100)
% 以下相当于产生100个4进制信号,两列100个2进制信号;
for i=1:N
% a uniform random variable between 0 and 1
temp=rand;
% with probability 1/4, source output is "00"
% 由于是从小到大顺序判断,不会有误判的可能;
if (temp<0.25)
dsource1(i)=0; dsource2(i)=0;
% with probability 1/4, source output is "01"
elseif (temp<0.5)
dsource1(i)=0; dsource2(i)=1;
% with probability 1/4, source output is "10"
elseif (temp<0.75)
dsource1(i)=1; dsource2(i)=0;
% with probability 1/4, source output is "11"
else
dsource1(i)=1; dsource2(i)=1;
end;
end;
% 下面是检测部分;
% detection and the probability of error calculation
% r表示接受信号,信号在传输的过程中受到信道衰,减加入噪声;
% 上面的命令表示产生100个发送端符号,下面的命令是逐个发送,相关
% 判决;
for i=1:N
% 这里信道的衰减因子是一定的,
% 这里把衰落因子拿到程序前面去设定;
ray=raylrnd(n_factor);
% 2 normal distributed r.v with 0, variance sgma
n=sgma*randn(1,2);
if ((dsource1(i)==0) & (dsource2(i)==0))
r=ray*s00+n;
elseif ((dsource1(i)==0) & (dsource2(i)==1))
r=ray*s01+n;
elseif ((dsource1(i)==1) & (dsource2(i)==0))
r=s10*ray+n;
else
r=s11*ray+n;
end;
% The correlation metrics are computed below
% 接受端作相关检测;
c00=dot(r,s00); c01=dot(r,s01);
c10=dot(r,s10); c11=dot(r,s11);
% The decision on the ith symbol is made next
% 判决,选择相关值最大的;
c_max=max([c00,c01,c10,c11]);
if (c00==c_max)
decis1=0; decis2=0;
elseif (c01==c_max)
decis1=0; decis2=1;
elseif (c10==c_max)
decis1=1; decis2=0;
else
decis1=1; decis2=1;
end;
% Increment the error counter,
% if the decision is not correct
symbolerror=0;
if (decis1~=dsource1(i))
numofbiterror=numofbiterror+1; symbolerror=1;
end;
if (decis2~=dsource2(i))
numofbiterror=numofbiterror+1; symbolerror=1;
end;
if (symbolerror==1)
numofsymbolerror=numofsymbolerror+1;
end;
end;
counter=counter+1;
end;
% 基本过程是这样的: 产生多次循环,每次循环产生100个符号,对这100个
% 符号统计比特误码个数,如果误比特个数小于100,再进行下一次循环;
% since there are totally N symbols
ps=numofsymbolerror/(N*counter);
% since 2N bits are transmitted
pb=numofbiterror/(2*N*counter);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -