clear all;
close all;
clc;
%----------------------------------------------%
% 参数设置
%----------------------------------------------%
num= 1e6;
Rb = 100; %比特速率,bps
fs = Rb;
Ts = 1/fs;
for Eb_N0 = 0:0.5:14 %dB,信噪比递增,便于作图
data_src = randint(1,num);
a = data_src(1:4:num-3);
b = data_src(2:4:num-2);
c = data_src(3:4:num-1);
d = data_src(4:4:num);
I = zeros(1,num/4);
Q = zeros(1,num/4);
for index = 1:num/4%星座点映射
switch a(index)*2^3+b(index)*2^2+c(index)*2^1+d(index)*2^0
case 0
I(index) = 3;
Q(index) = 3;
case 1
I(index) = 1;
Q(index) = 3;
case 2
I(index) = 3;
Q(index) = 1;
case 3
I(index) = 1;
Q(index) = 1;
case 4
I(index) = -1;
Q(index) = 3;
case 5
I(index) = -3;
Q(index) = 3;
case 6
I(index) = -1;
Q(index) = 1;
case 7
I(index) = -3;
Q(index) = 1;
case 8
I(index) = 3;
Q(index) = -1;
case 9
I(index) = 1;
Q(index) = -1;
case 10
I(index) = 3;
Q(index) = -3;
case 11
I(index) = 1;
Q(index) = -3;
case 12
I(index) = -1;
Q(index) = -1;
case 13
I(index) = -3;
Q(index) = -1;
case 14
I(index) = -1;
Q(index) = -3;
case 15
I(index) = -3;
Q(index) = -3;
otherwise
end
end
Es_N0 = Eb_N0 + 6;%16QAM Es_N0与Eb_N0转换
SNR = Es_N0;
snr = 10^(SNR/10);
Ps = sum(I.^2+Q.^2)/(num/4);%信号功率
power_nosie = Ps*10^(-SNR/10);%噪声功率
mapped_signal=I+1j.*Q;
noise = sqrt(power_nosie/2)*(randn(1,num/4)+1j*randn(1,num/4));
mapped_signal_awgn = mapped_signal + noise;
rec_I = real(mapped_signal_awgn);
rec_Q = imag(mapped_signal_awgn);
demod_I = zeros(1,num/4);
demod_Q = zeros(1,num/4);
demod_a = zeros(1,num/4);
demod_b = zeros(1,num/4);
demod_c = zeros(1,num/4);
demod_d = zeros(1,num/4);
for index = 1:num/4%硬判决
if rec_I(index)>=2 && rec_Q(index)>=2
demod_I(index) = 3;
demod_Q(index) = 3;
demod_a(index) = 0;
demod_b(index) = 0;
demod_c(index) = 0;
demod_d(index) = 0;
elseif rec_I(index)>=2 && rec_Q(index)<2 && rec_Q(index)>=0
demod_I(index) = 3;
demod_Q(index) = 1;
demod_a(index) = 0;
demod_b(index) = 0;
demod_c(index) = 1;
demod_d(index) = 0;
elseif rec_I(index)>=2 && rec_Q(index)<0 && rec_Q(index)>=-2
demod_I(index) = 3;
demod_Q(index) = -1;
demod_a(index) = 1;
demod_b(index) = 0;
demod_c(index) = 0;
demod_d(index) = 0;
elseif rec_I(index)>=2 && rec_Q(index)<-2
demod_I(index) = 3;
demod_Q(index) = -3;
demod_a(index) = 1;
demod_b(index) = 0;
demod_c(index) = 1;
demod_d(index) = 0;
elseif rec_I(index)>=0 && rec_I(index) < 2 && rec_Q(index)>=2
demod_I(index) = 1;
demod_Q(index) = 3;
demod_a(index) = 0;
demod_b(index) = 0;
demod_c(index) = 0;
demod_d(index) = 1;
elseif rec_I(index)>=0 && rec_I(index) < 2 && rec_Q(index)<2 && rec_Q(index)>=0
demod_I(index) = 1;
demod_Q(index) = 1;
demod_a(index) = 0;
demod_b(index) = 0;
demod_c(index) = 1;
demod_d(index) = 1;
elseif rec_I(index)>=0 && rec_I(index) < 2 && rec_Q(index)<0 && rec_Q(index)>=-2
demod_I(index) = 1;
demod_Q(index) = -1;
demod_a(index) = 1;
demod_b(index) = 0;
demod_c(index) = 0;
demod_d(index) = 1;
elseif rec_I(index)>=0 && rec_I(index) < 2 && rec_Q(index)<-2
demod_I(index) = 1;
demod_Q(index) = -3;
demod_a(index) = 1;
demod_b(index) = 0;
demod_c(index) = 1;
demod_d(index) = 1;
elseif rec_I(index)>=-2 && rec_I(index)<0 && rec_Q(index)>=2
demod_I(index) = -1;
demod_Q(index) = 3;
demod_a(index) = 0;
demod_b(index) = 1;
demod_c(index) = 0;
demod_d(index) = 0;
elseif rec_I(index)>=-2 && rec_I(index)<0 && rec_Q(index)<2 && rec_Q(index)>=0
demod_I(index) = -1;
demod_Q(index) = 1;
demod_a(index) = 0;
demod_b(index) = 1;
demod_c(index) = 1;
demod_d(index) = 0;
elseif rec_I(index)>=-2 && rec_I(index)<0 && rec_Q(index)<0 && rec_Q(index)>=-2
demod_I(index) = -1;
demod_Q(index) = -1;
demod_a(index) = 1;
demod_b(index) = 1;
demod_c(index) = 0;
demod_d(index) = 0;
elseif rec_I(index)>=-2 && rec_I(index)<0 && rec_Q(index)<0 && rec_Q(index)<-2
demod_I(index) = -1;
demod_Q(index) = -3;
demod_a(index) = 1;
demod_b(index) = 1;
demod_c(index) = 1;
demod_d(index) = 0;
elseif rec_I(index)<-2 && rec_Q(index)>=2
demod_I(index) = -3;
demod_Q(index) = 3;
demod_a(index) = 0;
demod_b(index) = 1;
demod_c(index) = 0;
demod_d(index) = 1;
elseif rec_I(index)<-2 && rec_Q(index)<2 && rec_Q(index)>=0
demod_I(index) = -3;
demod_Q(index) = 1;
demod_a(index) = 0;
demod_b(index) = 1;
demod_c(index) = 1;
demod_d(index) = 1;
elseif rec_I(index)<-2 && rec_Q(index)<0 && rec_Q(index)>=-2
demod_I(index) = -3;
demod_Q(index) = -1;
demod_a(index) = 1;
demod_b(index) = 1;
demod_c(index) = 0;
demod_d(index) = 1;
elseif rec_I(index)<-2 && rec_Q(index)<0 && rec_Q(index)<-2
demod_I(index) = -3;
demod_Q(index) = -3;
demod_a(index) = 1;
demod_b(index) = 1;
demod_c(index) = 1;
demod_d(index) = 1;
end
end
data_rec(1:4:num-3) = demod_a;
data_rec(2:4:num-2) = demod_b;
data_rec(3:4:num-1) = demod_c;
data_rec(4:4:num) = demod_d;
%----------------------------------%
M = 16;
Eb_N0_actual = 10^(Eb_N0/10);
a = sqrt(3*log2(M)*Eb_N0_actual/(2*(M-1))) ;
Q = 0.5*erfc(a);
Pm(Eb_N0*2+1) = 2*(1-1/sqrt(M))*Q;%计算理论误码率
%----------------------------------%
demod_signal = demod_I+1j*demod_Q;
cnt = 0;
for index = 1:num/4
if mapped_signal(index) ~= demod_signal(index)
cnt = cnt + 1;
end
end
ber(Eb_N0*2+1) = cnt/(num/4);%得到实际误码率
end
semilogy((0:0.5:14),ber,'black');grid on;
hold on;
semilogy((0:0.5:14),Pm,'red');grid on;%作图比较