## 二、原理概述

### 2.3 解调方式

（1）最小欧式距离准则判决

$\ln f(\vec{r} \mid \vec{s} {m})=-\frac{N}{2} \ln (\pi N{0})-\frac{1}{N_{0}} \sum_{k=1}^{N}(r_{k}-s_{m k})^{2}$

(2) 最佳检测器

8PSK: ;

QPSK:

8PSK:

## 四、主函数设计

### 4.1 星座图绘制主函数

1.流程图

2.代码实现

clc,clear,close;
% Symbol sequence length
L=100000;
% Generate the original bit sequence
sourceSeq=randnum(L);
[pI,pQ,sourceSeqCode]=Map(sourceSeq,L);
Eb = 1/3;

errbit = zeros(1,26);
errnum = zeros(1,26);
SNR=-5:20;
LS = length(SNR);
for i=1:LS
% Find the one-sided power spectral density of noise for a given signal-to-noise ratio
N0=Eb/(10^(SNR(i)/10));
% variance
var(i)=N0/2  ;
[rI,rQ]=noise(var(i),pI,pQ);
[result,I]=judgment(rI,rQ);
[errbit(i),errnum(i)]=Count(result,I,sourceSeq,sourceSeqCode);
end
draw(sourceSeqCode,rI,rQ);

### 4.2 QPSK与8PSK误码率对比主函数

1.流程图

2.代码实现

clc,clear,close;
% Symbol sequence length
L=100000;
% Generate the original bit sequence
sourceSeq=randnum(L);
[pI,pQ,sourceSeqCode]=Map(sourceSeq,L);
Eb = 1/3;

errbit = zeros(1,26);
errnum = zeros(1,26);
SNR=-5:20;
LS = length(SNR);
for i=1:LS
% Find the one-sided power spectral density of noise for a given signal-to-noise ratio
N0=Eb/(10^(SNR(i)/10));
% variance
var(i)=N0/2  ;
[rI,rQ]=noise(var(i),pI,pQ);
[result,I]=judgment(rI,rQ);
[errbit(i),errnum(i)]=Count(result,I,sourceSeq,sourceSeqCode);
end
% draw(sourceSeqCode,rI,rQ);

% Calculate the theoretical bit error rate using the erfc function
Theory8PSKSER = erfc(sqrt(3*10.^(SNR/10)) * sin(pi/8));
% QPSK
TheoryQPSKSER = erfc(sqrt(10.^(SNR/10))).*(1-0.25*erfc(sqrt(10.^(SNR/10))));;
% Load the SER-SNR curve of QPSK
figure(2);
epskerr = errnum/L;
semilogy(SNR,epskerr,'r-o');hold on;
semilogy(SNR,qpskerr,'b-o');hold on;
semilogy(SNR,Theory8PSKSER,'k-');hold on;
semilogy(SNR,TheoryQPSKSER,'k-');hold on;
ylabel('SER');
xlabel('SNR/dB')
legend('8PSK仿真曲线','QPSK仿真曲线','理论曲线','Location', 'northeast' );
grid on;
axis([-5,20,10e-7,1]);

## 五、子函数设计

### 5.1 随机比特序列的产生

function [SourceSeq]=randnum(L)
% L is the length of the generated sequence code
% Since a code is composed of 3 bits, it is generated here with 3*L.
randnum=rand(3*L,1);
% Initialize the original sequence
SourceSeq=zeros(3*L,1);
% The randomly generated sequence is judged
% if the random number is greater than 0.5, it is judged as 1, otherwise it is judged as 0.
for i=1:3*L
if(randnum(i)>=0.5)
SourceSeq(i)=1;
else
SourceSeq(i)=0;
end
end

### 5.2格雷编码序列

% Define 8psk mapping function
function [pI,pQ,SourceCode] = Map(SourceSeq,L)
% pI - in-phase component
% SourceCode - The size of the binary number of each digit of the sequence
% initialization
pI = zeros(L,1);
pQ = zeros(L,1);
% In order to facilitate subsequent expressions, sqrt(2)/2 is represented here
root =sqrt(2)/2;
% Constructing the mapping matrix according to the Gray code of 8PSK
MappingMat = [[1,0];[root,root];[-root,root];[0,1];[root,-root];[0,-1];[-1,0];[-root,-root]];

SourceCode =zeros(L,1);
% mapping process
for i=1:L
% Since a source symbol is composed of three bits
% the low and high bits are read in reverse order here, and expressed in decimal
SourceCode(i)=SourceSeq(3*i-2)*4+SourceSeq(3*i-1)*2+SourceSeq(3*i)+1;
% Find the corresponding code through the decimal representation and map it
pI(i) = MappingMat(SourceCode(i),1);
pQ(i) = MappingMat(SourceCode(i),2);
end
end

% Octal Gray Code Conversion
function[a1,a2]=Map_other(N)
% a1 is a sequence of random bits in binary
% a2 is a sequence of octal symbols
% Random bit sequence of length 3L
a1=bit(3*N);
% a2 is used to store the code element sequence of length L
a2=zeros(1,N);
for i=1:3:3*N-2
a2((i+2)/3)=abs(a1(i)*7-abs(a1(i+1)*3-a1(i+2)));
% Converts binary bit sequence Gray-encoded to octal sequence
end
end

### 5.3 映射函数

% 8PSK coordinate mapping
function [y3]=coordinate(x1,bit)
% x1 is the encoded octal sequence, bit is the originally generated binary random sequence
N=length(x1);
Es=bit*bit'/N;
% The first line of y3 is used to store the abscissa
% the second line is used to store the ordinate
y3=zeros(2,N);
% Coordinate mapping
for i=1:N
y3(1,i)=sqrt(Es)*cos(pi/4*x1(i)+pi/8);
y3(2,i)=sqrt(Es)*sin(pi/4*x1(i)+pi/8);
end
end

### 5.4 噪声生成与叠加输出

% Generate Gaussian random noise sub-function , var is the variance
function [rI,rQ] = noise(var,pI,pQ)
L = length(pI);
nc=zeros(L,1);
ns=zeros(L,1);
for k=1:L
u=rand;
z=sqrt(var*2*log(1/(1-u)));
nc(k)=z*cos(2*pi*u);
ns(k)=z*sin(2*pi*u);
end
% Output two mutually orthogonal Gaussian signals
rI = pI+nc;
rQ = pQ+ns;
end

### 5.5 判决函数

% Judgment Criterion: Minimum Euclidean Distance Criterion
function [result,I]=judgment(rI,rQ)
root=sqrt(2)/2;
L = length(rI);
I=zeros(L,1);
mapl = [[1,0];[root,root];[-root,root];[0,1];[root,-root];[0,-1];[-1,0];[-root,-root]];
for i=1:L
index=0;
minp=100;
for j=1:8
% Traverse each coordinate
% find the point with the smallest Euclidean distance from the point, and record it.
if((mapl(j,1)-rI(i))^2+(mapl(j,2)-rQ(i))^2<minp)
minp=(mapl(j,1)-rI(i))^2+(mapl(j,2)-rQ(i))^2;
index=j;
end
end
I(i)=index;
end

% According to the judgment point, make the corresponding assignment
result = zeros(3*L,1);
for i=1:L
switch(I(i))
case 1
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(0,0,0);
case 2
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(0,0,1);
case 3
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(0,1,0);
case 4
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(0,1,1);
case 5
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(1,0,0);
case 6
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(1,0,1);
case 7
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(1,1,0);
case 8
[result(3*i-2),result(3*i-1),result(3*i)]=SetValue(1,1,1);
end
end

end

SetValue将对应的值进行赋值即可，由于判决函数中调用次数过多，因此抽象封装成一个函数，便于使用。

% SetValue function
function [a,b,c]=SetValue(d,e,f)
a=d;
b=e;
c=f;
end

### 5.6星座图绘制函数

function draw(I,rI,rQ)
figure;
root = sqrt(2)/2;
%映射矩阵
MappingMat = [[1,0];[root,root];[-root,root];[0,1];[root,-root];[0,-1];[-1,0];[-root,-root]];
L=length(I)
for i=1:L
% Draw points with different colors according to different values
switch(I(i))
case 1
plot(rI(i),rQ(i),'*','color','r');hold on;

case 2
plot(rI(i),rQ(i),'*','color','g');hold on;

case 3
plot(rI(i),rQ(i),'*','color','b');hold on;

case 4
plot(rI(i),rQ(i),'*','color','c');hold on;

case 5
plot(rI(i),rQ(i),'*','color','m');hold on;

case 6
plot(rI(i),rQ(i),'*','color','y');hold on;

case 7
plot(rI(i),rQ(i),'*','color','k');hold on;

case 8
plot(rI(i),rQ(i),'*','color','[0.5,0.5,0.5]');hold on;

end
end
x = -4:0.1:4;
y = -2:0.1:2;
% plot(x,zeros(length(x)),'k');hold on;
% plot(zeros(length(y)),y,'k');hold on;
axis equal
axis([-2,2,-2,2]);
end

### 5.7误码率及误比特率计算函数

function [errbit,errsymbol]=Count(result,I,sourceSeq,sourceSym)
% result: the sequence after the decision
% I: symbol after decision
% sourceSeq: original bit sequence
% sourceSym: original symbol sequence
errbit=0;
errsymbol=0;
for i=1:length(sourceSeq)
if(sourceSeq(i)~=result(i))
errbit=errbit+1;
end
end
for i=1:length(I)
if(I(i)~=sourceSym(i))
errsymbol=errsymbol+1;
end
end
end

1.对二进制序列格雷编码的问题

2.关于MPSK误符号率的问题

3.关于星座图绘制的问题